虽然循环不起作用

时间:2017-07-31 19:58:28

标签: java eclipse while-loop

我正在制作一个程序,你输入学生的成绩,并计算谁通过和失败,然后Z应该意味着程序应该结束。但每当我输入“Z”时,它就会打印出来:

<Z is NOT an acceptable grade.>

所以有些事情阻止了while循环意识到我正在努力继续前进。有任何想法吗? 完整代码如下。

import java.lang.reflect.Array;

import java.util.Scanner;
import java.util.Arrays;
import java.util.List;

public class GCDProgram {
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
int numberOfGrades = 1;
System.out.println("Enter grade #" + numberOfGrades + ":");
String inputLetter = sc.nextLine();

int passed = 0;
int failed = 0;

String[] letterGrade = {"A", "B", "C", "D", "F"};
boolean result = Arrays.stream(letterGrade).anyMatch(inputLetter::equals);



while (inputLetter != "Z"){


if (result) {

    numberOfGrades++;
    System.out.println("Enter grade #" + numberOfGrades + ":");
    inputLetter = sc.nextLine();
    result = Arrays.stream(letterGrade).anyMatch(inputLetter::equals);
    if ((inputLetter == "D") || (inputLetter == "F")){
        failed++;
    }else{
        passed++;
    }
}else{
    System.out.println("<" + inputLetter + " is NOT an acceptable grade.>"); 
    System.out.println("Enter grade #" + numberOfGrades + ":");
    inputLetter = sc.nextLine();
    result = Arrays.stream(letterGrade).anyMatch(inputLetter::equals);
}
}
System.out.format("%-20s %3s", numberOfGrades + " students total", "");
System.out.format("%-20s %3s", passed + " students passed:", ((passed / numberOfGrades) * 100) + "%");
System.out.format("%-20s %3s", failed + " students failed:", ((failed / numberOfGrades) * 100) + "%");
 }


}

1 个答案:

答案 0 :(得分:1)

请尝试这个条件:

while (!"Z".equals(inputLetter)){