I created a calculator that can do simple addition, subtraction, multiplication and division. I want to know if my program can get simplified bearing in mind that I am not that experienced with Java, I only completed a few tutorials online. Here is my code for my simple calculator:
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);
}
}
Thanks