为什么我的java数组是';s不允许用户输入每个值?
代码应该要求每个数组有三个输入:(ID,然后是Name,然后是Major) ID工作正常,但当它转到name时,会打印出: 请输入学生的姓名: 请输入学生的姓名: 并且该行只允许一个输入。然后它进入大调,再次正常工作。所以我最终得到了3个ID,2个名字和3个专业 这是我的密码:为什么我的java数组是';s不允许用户输入每个值?,java,arrays,java.util.scanner,Java,Arrays,Java.util.scanner,代码应该要求每个数组有三个输入:(ID,然后是Name,然后是Major) ID工作正常,但当它转到name时,会打印出: 请输入学生的姓名: 请输入学生的姓名: 并且该行只允许一个输入。然后它进入大调,再次正常工作。所以我最终得到了3个ID,2个名字和3个专业 这是我的密码: package STUDENT; import java.util.Scanner; public class StudentDisplayer { public static void main(Stri
package STUDENT;
import java.util.Scanner;
public class StudentDisplayer {
public static void main(String[] args) {
long[]studentId = {11, 22, 33};
String[] studentName = {"Value1", "Value2", "Value3"};
String[] studentMajor = {"Value1", "Value2", "Value3"};
Scanner inReader = new Scanner(System.in);
/* ----------------------------------------------
Print the information in the parallel arrays
---------------------------------------------- */
for (int i = 0; i < studentId.length; i++ ){
System.out.println("Please enter the student's id: ");
studentId[i] = inReader.nextLong();
}
for (int i = 0; i < studentName.length; i++){
System.out.println("Please enter the student's name: ");
studentName[i] = inReader.nextLine();
}
for (int i = 0; i < studentMajor.length; i++){
System.out.println("Please enter the student's major: ");
studentMajor[i] = inReader.nextLine();
}
for (int i = 0; i < studentId.length; i++ )
{
System.out.print( studentId[i] + "\t");
System.out.print( studentName[i] + "\t");
System.out.print( studentMajor[i] + "\t");
System.out.println();
}
}
}
package学生;
导入java.util.Scanner;
公共班级学生显示器{
公共静态void main(字符串[]args){
长[]学生ID={11,22,33};
字符串[]studentName={“Value1”、“Value2”、“Value3”};
字符串[]studentMajor={“Value1”、“Value2”、“Value3”};
扫描仪inReader=新扫描仪(System.in);
/* ----------------------------------------------
在并行数组中打印信息
---------------------------------------------- */
for(int i=0;i
发生的情况是,nextLong()
不使用新行字符\n
(在按“介绍”时输入)。因此,您必须先使用它,然后才能继续您的逻辑:
for (int i = 0; i < studentId.length; i++ ){
System.out.println("Please enter the student's id: ");
studentId[i] = inReader.nextLong();
}
inReader.nextLine(); // ADD THIS
for (int i = 0; i < studentName.length; i++){
System.out.println("Please enter the student's name: ");
studentName[i] = inReader.nextLine();
}
for(int i=0;i
注意:您可以阅读我不久前写的这篇文章:发生的事情是,
nextLong()
不使用新行字符\n
(按Intro时输入)。因此,您必须先使用它,然后才能继续您的逻辑:
for (int i = 0; i < studentId.length; i++ ){
System.out.println("Please enter the student's id: ");
studentId[i] = inReader.nextLong();
}
inReader.nextLine(); // ADD THIS
for (int i = 0; i < studentName.length; i++){
System.out.println("Please enter the student's name: ");
studentName[i] = inReader.nextLine();
}
for(int i=0;i
注意:您可以阅读我不久前写的这篇文章:而不是在reader.nextLine()中使用
在reader.nextLine()中使用进行字符串输入。而不是在reader.nextLine()中使用
使用reader.next()中的进行字符串输入