程序没有记录第一个数组,设置为null--Java
我这里有一个代码,它创建了一个数组,并将人名输入其中。但是,不会记录第一个数组程序没有记录第一个数组,设置为null--Java,java,arrays,java.util.scanner,Java,Arrays,Java.util.scanner,我这里有一个代码,它创建了一个数组,并将人名输入其中。但是,不会记录第一个数组 Scanner newscan = new Scanner(System.in); System.out.println("How many people in the group?"); int groupnum = newscan.nextInt(); String[] people = new String[groupnum]; System.out.println("Pu
Scanner newscan = new Scanner(System.in);
System.out.println("How many people in the group?");
int groupnum = newscan.nextInt();
String[] people = new String[groupnum];
System.out.println("Put in the names of the people in the group");
String name;
int i = 0;
do{
System.out.println("Input Name");
name = newscan.nextLine();
people[i] = name;
i++;
}while(i<groupnum);
我尝试在IntelliJ上使用调试器,它显示我在第一个数组中输入的任何值都会被忽略,并设置为“”null值
我不知道如何处理这个问题。
如果您能告诉我是哪部分代码导致了此问题,以及如何解决此问题,将非常有帮助。请将您的代码修改为:
int groupnum = newscan.nextInt();
newscan.nextLine();
String[] people = new String[groupnum];
记住newscan.nextInt();没有使用换行符,这就是代码不起作用的原因,也是您得到以下结果的原因:
Input Name
Input Name
连续两次将代码修改为:
int groupnum = newscan.nextInt();
newscan.nextLine();
String[] people = new String[groupnum];
记住newscan.nextInt();没有使用换行符,这就是代码不起作用的原因,也是您得到以下结果的原因:
Input Name
Input Name
连续两次读取nextInt()方法后,新行将作为输入。为了避免这种情况,请使用scanner类skip方法。阅读nextInt()方法后,新行将作为输入。要避免这种情况,请使用scanner类跳过方法。检查scanner.nextLine()javadoc。它是nextInt()输入的返回行分隔符。
你可以在骑车前使用它
int i = 0;
newscan.nextLine();
do{
System.out.println("Input Name");
name = newscan.nextLine();
people[i] = name;
i++;
}while(i<groupnum);
inti=0;
newscan.nextLine();
做{
System.out.println(“输入名称”);
name=newscan.nextLine();
人名,;
i++;
}(i检查Scanner.nextLine()javadoc。它是来自nextInt()输入的返回行分隔符。
你可以在骑车前使用它
int i = 0;
newscan.nextLine();
do{
System.out.println("Input Name");
name = newscan.nextLine();
people[i] = name;
i++;
}while(i<groupnum);
inti=0;
newscan.nextLine();
做{
System.out.println(“输入名称”);
name=newscan.nextLine();
人名,;
i++;
}而(i只需添加这一行即可
newscan.nextLine();
int groupnum = newscan.nextInt();
在这条线以下
newscan.nextLine();
int groupnum = newscan.nextInt();
只需添加这一行
newscan.nextLine();
int groupnum = newscan.nextInt();
在这条线以下
newscan.nextLine();
int groupnum = newscan.nextInt();
“”与null不同。这是因为您没有在nextInt()中使用换行符。
的可能重复项“”与null不同。这是因为您没有在nextInt()中使用换行符。
的可能重复项