I have made the basic calculator app which can add, subtract multiply or divide just two numbers. What I am trying to do improve the program to be able to '+' '-' '*' or '/' more than just two numbers. Here is the basic java calculator program I have down so far:
import java.util.Scanner;
public class Calculator {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("
Enter first number:
");
double fnum = input.nextDouble();
System.out.println("
Enter an operation sign such as, '+', '-', '*', or '/', '=':
");
char operator = input.next().charAt(0);
System.out.println("
Enter second number:
");
double snum = input.nextDouble();
input.close();
switch(operator) {
case '+':
double answer = fnum + snum;
System.out.println("
" + fnum + " " + operator + " " + snum + " = " + answer);
break;
case '-':
double answer1 = fnum - snum;
System.out.println("
" + fnum + " " + operator + " " + snum + " = " + answer1);
break;
case '*':
double answer2 = fnum * snum;
System.out.println("
" + fnum + " " + operator + " " + snum + " = " + answer2);
break;
case '/':
double answer3 = fnum / snum;
System.out.println("
" + fnum + " " + operator + " " + snum + " = " + answer3);
break;
default:
System.out.println("Wrong choice for operator. ");
break;
}
}
}
To achieve this I was thinking that there has to be a loop probably before the sysout "Enter operator" line and I have tried to incorporate a do while loop with the while part being (operator != '=') and have had no success. Oh yeah and of coarse I need to reword the "Enter second number" sysout. Thanks in advance for any advice!
**Here's an example output of my current calculator program followed by an example of the I output of my desired calculator program.
current calculator output:
8.0 + 2.0 = 10.0
what i'm looking for calculator program to do:
8.0 - 4.0 * 10.0 = 40.0
Note: I am actively working for a solution myself when I have time to do so. If you dont feel like helping me that's perfectly fine. I think my question is clear, valid, and not necessary to delete according to the community guidelines. thanks
question from:
https://stackoverflow.com/questions/65886614/how-to-get-calculator-program-to-add-subtract-multiply-divide-more-than-two-n 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…