【发布时间】:2017-12-10 18:10:18
【问题描述】:
package assign;
import java.util.Scanner;
public class tests {
public static void main(String[] args) {
Scanner s = new Scanner (System.in);
System.out.println("How many sous chef do we have?");
int two = s.nextInt();
if(two <= 0) {
do {
System.out.println("Please enter a valid number:");
two = s.nextInt();
}while (two <= 0);
}
String [] souschef = new String[two];
for (int m = 0; m < souschef.length; m++) {
System.out.println("Enter the name of sous chef"+ m + ":");
String f = s.next();
f = souschef [m];
}
for (int i = 0; i < souschef.length; i++) {
System.out.println(souschef[i]);
}
}
}
不管我输入多少名字,输出都是空的。 for 循环没有填充数组吗?例如,我输入 3 个名字:John、Jack 和 Bob。不应该
for (int i = 0; i < souschef.length; i++) {
System.out.print(souschef[i]);
}
循环给我
约翰·杰克·鲍勃
但是输出是
null null null
谢谢!!
【问题讨论】:
-
请take the tour 了解该网站的运作方式以及此处的主题有哪些问题,并edit 相应地提出您的问题。另见:How to Debug Small Programs
标签: java arrays loops for-loop