Java While循环创建永无止境的输入循环
因此,我希望我的程序读取输入“A”、“B”或“C”,并通过显示星号来显示每个输入的编号。但我遇到了一个问题,它从不读取和显示我的输入 我的代码如下:Java While循环创建永无止境的输入循环,java,loops,input,while-loop,nested-loops,Java,Loops,Input,While Loop,Nested Loops,因此,我希望我的程序读取输入“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
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("---------------------------------------");
}
if(命令=='A'){
System.out.println(“在单行中键入附加输入”);
while(在.hasNext()中){
字符串输入=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意识到我丢失了一个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("---------------------------------------");
}
if(command='A'){
System.out.println(“在单行中键入附加输入”);
while(in.hasNext()){
字符串输入=in.next().toUpperCase();
if(input.equals(“A”){
numA++;
}
if(input.equals(“B”){
麻木++;
}
if(input.equals(“C”){
numC++;
}
if(input.compareTo(“A”)<0 | | input.compareTo(“Z”)>0)
打破
}
System.out.println(“---------------------------------------------------”;
System.out.printf(“\n%4s |”和“A”);
对于(int a=1;a什么是中的,一个InputStreamReader?一个扫描器?如果你说这是不起作用的,我想这在这里是相关的。对不起,是的,“in”是扫描器的名称。你能不能把放在声明和实例化中。