我正在尝试使用for循环打印出以下所有字符串以进入下面的println语句,但无法弄清楚如何让for循环来打印数组。我一直收到错误'找不到符号'。每个println语句后都会有一个扫描程序允许用户输入。
public static void Real() {
String[] sequence;
sequence = new String[4];
sequence[0] = ("first");
sequence[1] = ("second");
sequence[2] = ("third");
sequence[3] = ("fourth");
int number;
for (number = 0; number <= sequence(); number++ ) {
System.out.println("Input your " + sequence + " lap time");
}
}
答案 0 :(得分:0)
您的代码中存在一些语法错误,java中的方法名称也应该以低语录开头。
以下代码应该开展工作。
public static void real() {
String[] sequence;
sequence = new String[4];
sequence[0] = "first";
sequence[1] = "second";
sequence[2] = "third";
sequence[3] = "fourth";
int number;
for (number = 0; number < sequence.length; number++ ) {
System.out.println("Input your " + sequence[number] + " lap time");
}
}
答案 1 :(得分:0)
您不能仅通过在print语句中引用其名称来打印数组,您需要遍历每个索引处的所有元素和print元素。您可以使用语法 array [index] 访问元素。
您需要从0循环到最后一个索引,我认为您尝试从 sequence()获取的是数组的大小。要获得数组的大小,您需要使用 array.length
所以你的代码应该是这样的:
public static void Real() {
String[] sequence;
sequence = new String[4];
sequence[0] = ("first");
sequence[1] = ("second");
sequence[2] = ("third");
sequence[3] = ("fourth");
int number;
for (number = 0; number < sequence.length; number++) {
System.out.println("Input your " + sequence[number] + " lap time"); } }
答案 2 :(得分:0)
使用number<sequence.length
代替number <= sequence()
和
在 system.out.println()
中使用sequence[number]
代替sequence
答案 3 :(得分:0)
public static void Real(String a[]) {
String[] sequence;
sequence = new String[4];
Scanner input= new Scanner(System.in);
sequence[0] = ("first");
sequence[1] = ("second");
sequence[2] = ("third");
sequence[3] = ("fourth");
int number;
for (number = 0; number <4 ; number++ ) {
System.out.println("Input your " + sequence[number] + " lap time");
System.out.println("Input");
variable = inupt.nextDatatype();
}
}
这是我理解后的答案。 我不知道顺序是顺序();
答案 4 :(得分:0)
如何使用for-each循环?
for (String s : sequence)
System.out.println("Input your " + s + " lap time");