0

私は初心者で、while ループで if ステートメントをネストして実験していました。この方法で基本的な電卓を作成しましたが、少し問題があります。最初にコードを共有してから、問題を提起します。

コードは次のとおりです。

import java.util.Scanner;
class whileloop{
    public static void main(String args[]){
        Scanner scan = new Scanner(System.in);
    System.out.println("This is a basic calculator.");
    System.out.println("To exit, input exit when asked to input the method.");
    System.out.println("----------------------------------------------");
    while (true){
        System.out.println("to add, input '+', to subtract, input '-', to divide, input '/', to multiply, input '*' and to exit, input 'exit.'");
        System.out.println("----------------------------------------------------");
        System.out.print("Please enter a method here:");
        String method = scan.nextLine();
        if (method.equals("exit")){
            System.out.println("You chose to exit.");
            break;
        }
        else if (method.equals("+")){
            System.out.println("You chose to add.");
            System.out.print("Please enter first number here:");
            double fnum = scan.nextInt();
            System.out.print("Please enter second number here:");
            double snum = scan.nextInt();
            double ans = fnum + snum;
            System.out.print("The answer is");
            System.out.println(ans);
        }
        else if(method.equals("-")){
            System.out.println("You chose to subtract.");
            System.out.print("Please enter first number here:");
            double fnum = scan.nextInt();
            System.out.print("Please enter second number here:");
            double snum = scan.nextInt();
            double ans = fnum - snum;
            System.out.print("The answer is");
            System.out.println(ans);
        }
        else if(method.equals("*")){
            System.out.println("You chose to multiply.");
            System.out.print("Please enter first number here:");
            double fnum = scan.nextInt();
            System.out.print("Please enter second number here:");
            double snum = scan.nextInt();
            double ans = fnum * snum;
            System.out.print("The answer is");
            System.out.println(ans);
        }
        else if(method.equals("/")){
            System.out.println("You chose to divide.");
            System.out.print("Please enter first number here:");
            double fnum = scan.nextInt();
            System.out.print("Please enter second number here:");
            double snum = scan.nextInt();
            double ans = fnum / snum;
            System.out.print("The answer is");
            System.out.println(ans);
        }
        else{
            System.out.println("Invalid input. please select a valid input from the list of operators given.");
        }
    }
}
}

ご覧methodのとおり、入力を処理する変数をfnum要求し、snum

最後に、else ステートメントを追加しました。ユーザーが操作に有効な値を入力していないときにユーザーに通知することになっていますmethod

問題は、このコードを実行するたびに、入力に関係なくmethod、最初は完全に機能し、とを要求してfnumからsnum答えを出力しますが、else ステートメントのコードブロックも実行することです。

これを防ぐ方法は?助けてください!

また、これは私が得る出力の種類です: (もう少し整理されたように見えるように、コードブロックに入れました。)

to add, input '+', to subtract, input '-', to divide, input '/', to multiply, input '*'         and to exit, input 'exit.'
----------------------------------------------------
Please enter a method here:+
You chose to add.
Please enter first number here:10
Please enter second number here:10
The answer is20.0
to add, input '+', to subtract, input '-', to divide, input '/', to multiply, input '*'     and to exit, input 'exit.'
----------------------------------------------------
Please enter a method here:Invalid input. please select a valid input from the list of     operators given.
to add, input '+', to subtract, input '-', to divide, input '/', to multiply, input '*'   and to exit, input 'exit.'
----------------------------------------------------
Please enter a method here:
4

3 に答える 3

2
use scan.nextLine() immediately after scan.nextInt().

数値を入力してEnterキーを押すと、scan.nextInt()は「行末」ではなく数値のみを読み取ります。scan.nextLine();を実行する場合 最初の入力からまだバッファにある「行末(\ n)」を読み取ります。

import java.util.Scanner;
class whileloop{
    public static void main(String args[]){
        Scanner scan = new Scanner(System.in);
    System.out.println("This is a basic calculator.");
    System.out.println("To exit, input exit when asked to input the method.");
    System.out.println("----------------------------------------------");
    while (true){
        System.out.println("to add, input '+', to subtract, input '-', to divide, input '/', to multiply, input '*' and to exit, input 'exit.'");
        System.out.println("----------------------------------------------------");
        System.out.print("Please enter a method here:");
        String method = scan.nextLine();
        if (method.equals("exit")){
            System.out.println("You chose to exit.");
            break;
        }
        else if (method.equals("+")){
            System.out.println("You chose to add.");
            System.out.print("Please enter first number here:");
            double fnum = scan.nextInt();
        scan.nextLine();
            System.out.print("Please enter second number here:");
            double snum = scan.nextInt();
        scan.nextLine();
            double ans = fnum + snum;
            System.out.print("The answer is");
            System.out.println(ans);
        }
        else if(method.equals("-")){
            System.out.println("You chose to subtract.");
            System.out.print("Please enter first number here:");
            double fnum = scan.nextInt();
        scan.nextLine();
            System.out.print("Please enter second number here:");
            double snum = scan.nextInt();
        scan.nextLine();
            double ans = fnum - snum;
            System.out.print("The answer is");
            System.out.println(ans);
        }
        else if(method.equals("*")){
            System.out.println("You chose to multiply.");
            System.out.print("Please enter first number here:");
            double fnum = scan.nextInt();
        scan.nextLine();
            System.out.print("Please enter second number here:");
            double snum = scan.nextInt();
        scan.nextLine();
            double ans = fnum * snum;
            System.out.print("The answer is");
            System.out.println(ans);
        }
        else if(method.equals("/")){
            System.out.println("You chose to divide.");
            System.out.print("Please enter first number here:");
            double fnum = scan.nextInt();
        scan.nextLine();
            System.out.print("Please enter second number here:");
            double snum = scan.nextInt();
        scan.nextLine();
            double ans = fnum / snum;
            System.out.print("The answer is");
            System.out.println(ans);
        }
        else{
            System.out.println("Invalid input. please select a valid input from the list of operators given.");
        }
    }
}
}
于 2012-07-27T10:32:28.827 に答える
1

String method = scan.nextLine();""計算後に返します。空の文字列はどの if ステートメントにも一致しないため、else評価されます。

于 2012-07-27T10:33:40.223 に答える
0

これは、計算後にscan.nextLine()返されるためです。""したがって、この行を追加するだけです。

     System.out.println("to add, input '+', to subtract, input '-', to divide, input '/', to multiply, input '*' and to exit, input 'exit.'");
     System.out.println("----------------------------------------------------");
     System.out.print("Please enter a method here:");        
     String method = scan.nextLine();
     // add this line
     if (method.equals(""))
     {
        continue; // no "" will be there 
     }
于 2012-07-27T10:40:02.527 に答える