0

由于某种原因,我的计算器不会等待用户输入来完成 do while 循环。我对 java 编码很陌生(目前只做了几个小时)。我希望用户能够在程序关闭之前做更多的数学运算,而不是每次他们想使用它时都必须重新打开它(显然我并不是说任何严肃的事情,我只是想学习,我认为这会有所帮助.

这是我的代码

import java.util.Scanner;


public class calculator {
public static void main(String[] args){
    double Answer;
    String op;
    double num1;
    double num2;
    String again;
    boolean yesorno = true;

            Scanner input = new Scanner(System.in);

             while (yesorno = true){
                System.out.print("What opperation would you like to preform? +,-,*,/, :");
                op = input.nextLine();
                System.out.print("What is the first number? : ");
                num1 = input.nextDouble();
                System.out.print("And the seccond number? : ");
                num2 = input.nextDouble();
                if (op.equals("+")) {
                    Answer = (num1 + num2);
                    System.out.println(Answer);

                } else if (op.equals("-")) {
                    Answer = num1 - num2;
                    System.out.println(Answer);

                } else if (op.equals("*")) {
                    Answer = num1 * num2;
                    System.out.println(Answer);

                } else if (op.equals("/")) {
                    Answer = num1 / num2;
                    System.out.println(Answer);

                }
                System.out.println("Would you like to do any more math?");
                again = input.nextLine();
                if (again.equals("yes")) {
                    yesorno = true;
                } else if (again.equals("no")) {
                    yesorno = false;
                    System.out.print("have a good day!");

                }
            } while (yesorno = true);       
}
}

请忽略此代码开头和结尾的错误格式。

4

4 回答 4

1

1)while(yesorno = true )您正在进行分配 更改以 while(yesorno == true)防止这种情况发生,您可以使用 yoda 样式while(true = yesorno),然后会引发编译错误,因为您无法将某些内容分配给值。或者更简单,只需使用while(yesorno)

2) 遵循Java Code Convention,变量名是小写的。

3)如果这个块被执行while (yesorno = true);,你将有一个无限循环。

4)如果您使用的是 java 7 ,您可以切换字符串

             switch(op){
             case "+":answer = num1 + num2;break;
             case "-":answer = num1 - num2;break;
             case "*":answer = num1 * num2;break;
             case "/":answer = num1 / num2;break;
             default: throw new IllegalArgumentException("Invalid operation "+ op);
             }
             System.out.println(answer);
于 2013-07-26T02:15:09.570 回答
1

您正在分配,而不是测试. while (yesorno = true){您应该使用while (yesorno == true){, 因为双等号 ( ==)测试是否相等。

于 2013-07-26T01:35:05.497 回答
0

有很多错误,我已修复并为您评论。希望能帮助到你:

import java.util.Scanner;

// KK: by general convention class names should always start with an upper case letter
public class calculator {
    public static void main(String[] args) {
        double Answer; //KK: local variable should be lower case
        String op;
        double num1;
        double num2;
        String again;
        boolean yesorno = true;

        Scanner input = new Scanner(System.in);

        //KK: for comparing you need the double-equals
        //KK: the loop will be executed as long as the expression is true, so for a boolean you don't need it at all
        //KK: you can use either of the following:
        // while (yesorno == true)
        // while (yesorno)
        while (yesorno) {
            System.out.print("What opperation would you like to preform? +,-,*,/, :");
            op = input.nextLine();
            System.out.print("What is the first number? : ");
            num1 = input.nextDouble();
            System.out.print("And the seccond number? : ");
            num2 = input.nextDouble();
            if (op.equals("+")) {
                Answer = (num1 + num2);
                System.out.println(Answer);

            } else if (op.equals("-")) {
                Answer = num1 - num2;
                System.out.println(Answer);

            } else if (op.equals("*")) {
                Answer = num1 * num2;
                System.out.println(Answer);

            } else if (op.equals("/")) {
                Answer = num1 / num2;
                System.out.println(Answer);

            }
            System.out.println("Would you like to do any more math?");
            //KK: you need to call nextLine twice because you printed 2 lines here
            //KK: otherwise again will be empty, so yesorno will always be true and you have an endless loop
            again = input.nextLine();
            again = input.nextLine();
            if (again.equals("yes")) {
                yesorno = true;
            } else if (again.equals("no")) {
                yesorno = false;
                System.out.print("have a good day!");
            }
        }
        // KK: the following line was an empty loop, that didn't do anything, so I commented it
        // while (yesorno = true);

    }
} //KK: this one was missing at the end too ;)

(我的评论是用 KK 开始的,所以你可以看到它们,以后可以删除它们。)

于 2013-07-26T02:20:58.900 回答
0

尝试这个....

公共类主要{

public static void main(String[] args) {
    // TODO Auto-generated method stub

    boolean status = true;
    while(status){

    String answer = "";
    String choise = "";

    System.out.println("\"WELCOME TO JAVA CALCULATOR\"");

    Scanner scn = new Scanner(System.in);
    Scanner cal = new Scanner(System.in);
    Scanner cho = new Scanner(System.in);

    System.out.println("Enter the numers one by one that you want to calculate..");
    int numA = scn.nextInt();
    int numB = scn.nextInt();
    int result = 0;
    System.out.println("What you want to calculate...?");
    answer = cal.nextLine();

    if(answer.equals("+")){
        result = numA+numB;}
    if(answer.equals("-")){
        result = numA-numB;}
    if(answer.equals("*")){
        result = numA*numB;}
    if(answer.equals("/")){
        result = numA/numB;}
    System.out.println( "The result of " + numA + " and " + numB + " is : " + result);
    System.out.println("Do you want to continue.....(y) or (n)?");
    choise = cho.nextLine();

    if(choise.equalsIgnoreCase("y")){
        System.out.println("Welcome back.....:)\n\"Make By Saikat Halder\"");
        status = true;}
    if(choise.equalsIgnoreCase("n")){
        System.out.println("Good bye....Thanks for useing java Calculator......:)");
        System.exit(0);

        }
    }
}

}

于 2016-05-22T12:48:41.973 回答