【问题标题】:I just wanted to know the functionality of readInput() in the code below我只是想知道下面代码中 readInput() 的功能
【发布时间】:2021-05-30 04:19:55
【问题描述】:

我想知道readInput() function的目的

import java.util.Scanner;

public class Rainbow {

    public static void main(String[] args) {
        String rainbowColors[] = {"Red", "Orange", "Yellow", "Green",
                                  "Blue", "Indigo", "Violet"};
        int choice = readInput(); // **whats this?
        System.out.println("Your choice is: "+rainbowColors[choice-1]);  
    }

    public static int readInput(){ *and this
        Scanner s = new Scanner(System.in);
        System.out.println("Please enter an integer from 1 - 7");
        int input = s.nextInt();
        return(input);
    }

}

【问题讨论】:

  • 它被称为方法(或其他编程语言中的 函数),其唯一目的是从运行您的应用程序的用户那里获取键盘输入关于rainbowColors[] 字符串数组中可用的特定颜色选择。用户需要一个文字值,然后将其减去 1 以获得相关颜色的实际数组索引值。

标签: java arrays string java.util.scanner


【解决方案1】:

这只是一个普通函数,而不是为每个输入编写扫描器类对象并获取指定的输入,我们可以创建这样的函数,以便我们可以重用代码。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-05-24
    • 2014-06-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多