簡単な足し算、引き算、掛け算、割り算ができる計算機を作りました。私はJavaの経験があまりなく、オンラインでいくつかのチュートリアルを完了しただけであることを念頭に置いて、私のプログラムを簡素化できるかどうかを知りたいです。これが私の単純な電卓のコードです。
import java.util.Scanner;
public class Attempt1 {
public static void main (String []arg){
double fnum, snum, tnum;
double answer = 0;
int turn = 0;
String operator = "nothing";
Scanner number = new Scanner(System.in);
Scanner Symbol = new Scanner(System.in);
System.out.println("Enter number");
if (number.hasNextDouble()){
fnum = number.nextDouble();
operator = Symbol.nextLine();
switch (operator){
case "+":
if (number.hasNextDouble()){
snum = number.nextInt();
answer = fnum + snum;
System.out.println(answer);
}
break;
case "-":
if (number.hasNextDouble()){
snum = number.nextInt();
answer = fnum - snum;
System.out.println(answer);
}
break;
case "*":
if (number.hasNextDouble()){
snum = number.nextInt();
answer = fnum * snum;
System.out.println(answer);
}
break;
case "/":
if (number.hasNextDouble()){
snum = number.nextInt();
answer = fnum / snum;
System.out.println(answer);
}
break;
default:
System.out.println("You did not enter a number:");
}
}else{
System.out.println("Error");
}
do {
operator = Symbol.nextLine();
switch (operator){
case "+":
if (number.hasNextDouble()){
tnum = number.nextInt();
answer = answer + tnum;
System.out.println(answer);
}break;
case "-":
if (number.hasNextDouble()){
tnum = number.nextInt();
answer = answer - tnum;
System.out.println(answer);
}
break;
case "*":
if (number.hasNextDouble()){
tnum = number.nextInt();
answer = answer * tnum;
System.out.println(answer);
}
break;
case "/":
if (number.hasNextDouble()){
tnum = number.nextInt();
answer = answer / tnum;
System.out.println(answer);
}
break;
default:
System.out.println("You did not enter a number:");
}
}while (turn < 10);
}
}
ありがとう