因此,我希望我的程序读取输入“ A”,“ B”或“ C”,并通过显示星号来显示每个数字。但是我遇到了一个问题,它永远不会读取和显示我的输入。
我的代码如下:
if (command == 'A'){
System.out.println("Type the additional input in a single line.");
while(in.hasNext()){
String input = in.next().toUpperCase();
if(input.equals("A")){ numA++;}
if(input.equals("B")){ numB++;}
if(input.equals("C")){ numC++;}
}
System.out.println("---------------------------------------");
System.out.printf("\n%4s |", "A");
for (int a = 1; a <= numA; a++) {
System.out.print("*");
}
System.out.println();
System.out.printf("\n%4s |", "B");
for (int b = 1; b <= numB; b++) {
System.out.print("*");
}
System.out.println();
System.out.printf("\n%4s |", "C");
for (int c = 1; c <= numC; c++) {
System.out.print("*");}
System.out.println();
double gpa = ((numA*4)+(numB*3)+(numC*2)) / ((numA+numB+numC));
System.out.println("GPA: " + gpa);
System.out.println();
System.out.println("---------------------------------------");
}
答案 0 :(得分:1)
意识到我错过了一个break语句。我的老师希望输入任何数字后显示数据。 break语句是我以前从未了解或使用过的东西,但是这是我如何为有兴趣的人修复的:
if (command == 'A') {
System.out.println("Type the additional input in a single line.");
while (in.hasNext()) {
String input = in.next().toUpperCase();
if (input.equals("A")) {
numA++;
}
if (input.equals("B")) {
numB++;
}
if (input.equals("C")) {
numC++;
}
if (input.compareTo("A") < 0 || input.compareTo("Z") > 0)
break;
}
System.out.println("---------------------------------------");
System.out.printf("\n%4s |", "A");
for (int a = 1; a <= numA; a++) {
System.out.print("*");
}
System.out.println();
System.out.printf("\n%4s |", "B");
for (int b = 1; b <= numB; b++) {
System.out.print("*");
}
System.out.println();
System.out.printf("\n%4s |", "C");
for (int c = 1; c <= numC; c++) {
System.out.print("*");
}
System.out.println();
double gpa = ((numA * 4) + (numB * 3) + (numC * 2)) / ((numA + numB + numC));
System.out.println("GPA: " + gpa);
System.out.println();
System.out.println("---------------------------------------");
}
对不起,我是java的新手。