我正在尝试编写一个程序,要求6名学生的姓名和成绩,并计算它(A,B,C,F等..)
我有一个我无法解决的恼人的错误。 如果你运行它并看到输出你会知道我的意思:它跳过要求第二个学生名字。
import java.util.*;
public class grading {
public static void main (String [] args) {
Scanner kb = new Scanner (System.in);
String a, b, c, d, e, f = "";
int mid1, mid2, finl, total = 0;
int mid12, mid22, finl2, total2 = 0;
int mid13, mid23, finl3, total3 = 0;
int mid14, mid24, finl4, total4 = 0;
int mid15, mid25, finl5, total5 = 0;
int mid16, mid26, finl6, total6 = 0;
String g1, g2, g3, g4, g5, g6 = "0";
System.out.println ("Enter you first student name :");
a = kb.nextLine();
System.out.println ("His first midterm grade [out of 25] :");
mid1 = kb.nextInt();
System.out.println ("His second midterm grade [out of 25] :");
mid2 = kb.nextInt();
System.out.println ("His final test grade [out of 50] :");
finl = kb.nextInt();
total = mid1 + mid2 + finl;
///////////////////////// Student 1
System.out.println ("Enter you second student name :");
b = kb.nextLine();
System.out.println ("His first midterm grade [out of 25] :");
mid12 = kb.nextInt();
System.out.println ("His second midterm grade [out of 25] :");
mid22 = kb.nextInt();
System.out.println ("His final test grade [out of 50] :");
finl2 = kb.nextInt();
total2 = mid12 + mid22 + finl2;
////////////////////// Student 2
System.out.println ("Enter you third student name :");
c = kb.nextLine();
System.out.println ("His first midterm grade [out of 25] :");
mid13 = kb.nextInt();
System.out.println ("His second midterm grade [out of 25] :");
mid23 = kb.nextInt();
System.out.println ("His final test grade [out of 50] :");
finl3 = kb.nextInt();
total3 = mid13 + mid23 + finl3;
///////////////////// Student 3
System.out.println ("Enter you fourth student name :");
d = kb.nextLine();
System.out.println ("His first midterm grade [out of 25] :");
mid14 = kb.nextInt();
System.out.println ("His second midterm grade [out of 25] :");
mid24 = kb.nextInt();
System.out.println ("His final test grade [out of 50] :");
finl4 = kb.nextInt();
total4 = mid14 + mid24 + finl4;
//////////////////////// Student 4
System.out.println ("Enter you fifth student name :");
e = kb.nextLine();
System.out.println ("His first midterm grade [out of 25] :");
mid15 = kb.nextInt();
System.out.println ("His second midterm grade [out of 25] :");
mid25 = kb.nextInt();
System.out.println ("His final test grade [out of 50] :");
finl5 = kb.nextInt();
total5 = mid15 + mid25 + finl5;
/////////////////// Student 5
System.out.println ("Enter you sixth student name :");
f = kb.nextLine();
System.out.println ("His first midterm grade [out of 25] :");
mid16 = kb.nextInt();
System.out.println ("His second midterm grade [out of 25] :");
mid26 = kb.nextInt();
System.out.println ("His final test grade [out of 50] :");
finl6 = kb.nextInt();
total6 = mid16 + mid26 + finl6;
/////////////// Student 6
}
}
答案 0 :(得分:0)
您的输入包含整数和行。 nextInt
将为您提供下一个int
,如果需要,会使用换行符。但是,如果您使用nextInt
关注newLine
,则newline
会对用于输入int
的回车信息感到满意。
解决方案是添加
kb.nextLine();
阅读每个学生的最终成绩后。
答案 1 :(得分:0)
另一种解决方案是在每次阅读时使用新的扫描仪。
a = new Scanner (System.in).nextLine();
mid1 = new Scanner (System.in).nextInt();
mid2 = new Scanner (System.in).nextInt();
finl = new Scanner (System.in).nextInt();
b = new Scanner (System.in).nextLine();
...
答案 2 :(得分:0)
尝试使用kb.next()
代替kb.nextLine()
。这应该有用。
在输入int值后按Enter键,因为它不是整数,它会被用作kb.nextLine()
的值,因此会被跳过。