私は初心者で、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: