【发布时间】: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