有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

do-while循环中的java语义问题

我的程序由解方程和询问用户是否想解更多方程组成,因此,即使我输入yes继续,但循环终止,这是我的代码

 public static void main(String[] args) {

    String goAgain;

    double A, B, C, solution;
    Scanner scanner = new Scanner(System.in);
    System.out.println("This program will print a solution of an equation");
    System.out.println("of the form A*X*X + B*X + C = 0, where A, B, and");
    System.out.println("C are values that you enter.");

    do {
        System.out.println();
        System.out.println("Enter values of A, B and C : ");
        System.out.print(" A = ");
        A = scanner.nextDouble();
        System.out.print(" B = ");
        B = scanner.nextDouble();
        System.out.print(" C = ");
        C = scanner.nextDouble();
        System.out.println();
        try {

            solution = root(A, B, C);
            System.out.println("your solution is : " + solution);


        } catch (IllegalArgumentException e) {
            System.out.println("Sorry, I can't find a solution.");
            System.out.println(e.getMessage());

        }
        System.out.println("Do you want to solve another equation ? ");
        goAgain = scanner.next();
        System.out.println(goAgain);

    } while (goAgain == "yes");
}

共 (1) 个答案

  1. # 1 楼答案

    while (goAgain == "yes");中使用.equals()函数而不是==
    应该是while (goAgain.equals("yes");
    记住goAgain == "yes"总是返回false