基本Java程序:字符串对象赢得';甚至不能输入 我是一个C++程序员,对java很陌生,并且把字符串作为字段(或属性或任何东西)都有问题。 import java.util.Scanner; class Student{ String name; static int count=0; int regno; double marks; Student(){ count++; } static void getCount(){ System.out.println("Count is "+count); } void setName(String n1){ name=n1; } void setMarks(double m1){ marks=m1; } void setRegNo(int rno){ regno=rno; } String getName(){ return name; } int getRegNo(){ return regno; } double getMarks(){ return marks; } } class Demo{ int i; Student s[]=new Student[3]; Scanner in=new Scanner(System.in); Demo(){ for(i=0;i<3;i++){ s[i]=new Student(); System.out.println("Enter name of student "+(i+1)+" :"); s[i].setName(in.nextLine()); System.out.println("Enter Reg. No. of student "+(i+1)+" :"); s[i].setRegNo(in.nextInt()); System.out.println("Enter marks of student "+(i+1)+" :"); s[i].setMarks(in.nextDouble()); } } public void Display(){ System.out.println("Students with marks >70 : "); System.out.println("RegNo\tName\tMarks\t"); for(i=0;i<3;i++){ System.out.println(s[i].getRegNo()+"\t"+s[i].getName()+"\t"+s[i].getMarks()); } } } class P4{ public static void main(String args[]){ Demo d=new Demo(); d.Display(); } }
问题是:基本Java程序:字符串对象赢得';甚至不能输入 我是一个C++程序员,对java很陌生,并且把字符串作为字段(或属性或任何东西)都有问题。 import java.util.Scanner; class Student{ String name; static int count=0; int regno; double marks; Student(){ count++; } static void getCount(){ System.out.println("Count is "+count); } void setName(String n1){ name=n1; } void setMarks(double m1){ marks=m1; } void setRegNo(int rno){ regno=rno; } String getName(){ return name; } int getRegNo(){ return regno; } double getMarks(){ return marks; } } class Demo{ int i; Student s[]=new Student[3]; Scanner in=new Scanner(System.in); Demo(){ for(i=0;i<3;i++){ s[i]=new Student(); System.out.println("Enter name of student "+(i+1)+" :"); s[i].setName(in.nextLine()); System.out.println("Enter Reg. No. of student "+(i+1)+" :"); s[i].setRegNo(in.nextInt()); System.out.println("Enter marks of student "+(i+1)+" :"); s[i].setMarks(in.nextDouble()); } } public void Display(){ System.out.println("Students with marks >70 : "); System.out.println("RegNo\tName\tMarks\t"); for(i=0;i<3;i++){ System.out.println(s[i].getRegNo()+"\t"+s[i].getName()+"\t"+s[i].getMarks()); } } } class P4{ public static void main(String args[]){ Demo d=new Demo(); d.Display(); } },java,Java,问题是: 从for循环的第二次迭代开始,程序直接要求我输入regno并标记跳过名称字符串。我不明白它在第一次迭代中是如何工作的,但其他的就不明白了。请解释一下这里出了什么问题。我猜这与许多人在C中遇到的问题相同,例如scanf,也就是说,前一个输入的结束换行符仍在输入缓冲区中,这导致nextLine函数将此换行符视为空行 在输入字符串之前,您需要一种方法来丢弃前导空格。在s[i]之后键入in.nextLine()。设置标记(in.nextDouble()) 正在发生的事情: for(i=0;i&
从for循环的第二次迭代开始,程序直接要求我输入regno并标记跳过名称字符串。我不明白它在第一次迭代中是如何工作的,但其他的就不明白了。请解释一下这里出了什么问题。我猜这与许多人在C中遇到的问题相同,例如
scanf
,也就是说,前一个输入的结束换行符仍在输入缓冲区中,这导致nextLine
函数将此换行符视为空行
在输入字符串之前,您需要一种方法来丢弃前导空格。在s[i]之后键入in.nextLine()
。设置标记(in.nextDouble())代码>
正在发生的事情:
for(i=0;i<3;i++){
s[i]=new Student();
System.out.println("Enter name of student "+(i+1)+" :");
s[i].setName(in.nextLine()); // second time around, you read the newline character
System.out.println("Enter Reg. No. of student "+(i+1)+" :");
s[i].setRegNo(in.nextInt());// once you press "enter" here, the newline
System.out.println("Enter marks of student "+(i+1)+" :");
s[i].setMarks(in.nextDouble());// once you press "enter" here, the newline character is added to the input stream
}
for(i=0;i问题在于您将nextLine()
/nextDouble()
与nextLine()
混合使用
当您输入您的int
/double
时,它后面会有一个新行,当您在下一次迭代中调用nextLine()
时,它会读取这一新行
解决方案是在调用nextLine()
/nextDouble()
之后调用nextLine()
(并放弃结果)
下面是一个简单的代码,展示了问题(和解决方案):
太好了!!这很有效,只需将in.nextLine()替换为in.next()。(我希望扫描仪的缓冲区会自动清除,但它就是没有)很好的解释,但是为什么Java不关闭并为每个输入启动一个新的缓冲区呢?这可能会使事情变得更实际。@Shridharshan它不会为每个输入重新创建一个新的缓冲区,因为您正在重用中的。如果您使用中的重新创建一个新的扫描仪,则=新的扫描仪(System.in)
每次,它都会重新创建一个新的缓冲区。@Shridharshan需要考虑的另一件事是,有时,您不希望它重新创建一个新的缓冲区(例如:当您从文件/另一个命令管道输入时),而且“默认情况下”这样做不是一个好的解决方案。EDIT:enter键不是应该以in.nextInt()结尾吗(或在.nextDouble()中)并简单地在.nextLine()中开始一个新的,而不是在.nextInt()中同时结束,并插入一个“\n”。这就像在一个字符之后添加两个“\n”字符一样another@Shridharshan-最后一个“\n”将在流中。nextInt()
跳过第一个。
for(i=0;i<3;i++){
s[i]=new Student();
System.out.println("Enter name of student "+(i+1)+" :");
s[i].setName(in.nextLine()); // second time around, you read the newline character
System.out.println("Enter Reg. No. of student "+(i+1)+" :");
s[i].setRegNo(in.nextInt());// once you press "enter" here, the newline
System.out.println("Enter marks of student "+(i+1)+" :");
s[i].setMarks(in.nextDouble());// once you press "enter" here, the newline character is added to the input stream
}
try (Scanner in = new Scanner(System.in)) {
System.out.println("Int:");
in.nextInt();
// in.nextLine(); // uncomment to fix the problem
System.out.println("Line:");
in.nextLine();
}