Sorry for this crap disguised as a code. Just about finished learning loops...
This is a simple calculator and I have to do one more calculation after the initial calculation (i.e. 120 + 23 = 123, - 33 = 90)
import java.util.Scanner; public class Calc { public static void main(String[] args) { Scanner input = new Scanner (System.in); double first, sec, third, result; String symbol; System.out.print("Enter the first value: "); first=input.nextDouble(); System.out.print("Enter your operator (+,-,*,/): "); symbol=input.next(); System.out.print("Enter the second value: "); sec=input.nextDouble(); Scanner input2 = new Scanner(System.in); switch(symbol){ case "+": result = first + sec; System.out.println(result); String another; System.out.print ("Enter another operator (+,-,*,/): "); another=input2.next(); System.out.print(("Enter the third value: ")); third=input2.nextDouble(); switch(another){ case"+": System.out.println(result + third); break; case"-": System.out.println(result-third); break; case "*": System.out.println(result*third); break; case "/": System.out.println(result/third); break; default: System.out.println("What the hell?"); break; } case "-": result = first - sec; System.out.println(result); System.out.print ("Enter another operator (+,-,*,/): "); another=input2.next(); System.out.print(("Enter the third value: ")); third=input2.nextDouble(); switch(another){ case"+": System.out.println(result+third); break; case"-": System.out.println(result-third); break; case "*": System.out.println(result*third); break; case "/": System.out.println(result/third); break; default: System.out.println("What the hell?"); break; } case "*": result = first * sec; System.out.println(result); System.out.print ("Enter another operator (+,-,*,/): "); another=input2.next(); System.out.print(("Enter the third value: ")); third=input2.nextDouble(); switch(another){ case"+": System.out.println(result+third); break; case"-": System.out.println(result-third); break; case "*": System.out.println(result*third); break; case "/": System.out.println(result/third); break; default: System.out.println("What the hell?"); break; } case "/": result=first/sec; System.out.println(result); System.out.print ("Enter another operator (+,-,*,/): "); another=input.next(); System.out.print(("Enter the third value: ")); third=input.nextDouble(); switch(another){ case"+": System.out.println(result+third); break; case"-": System.out.println(result-third); break; case "*": System.out.println(result*third); break; case "/": System.out.println(result/third); break; default: System.out.println("What the hell?"); break; } default: System.out.println("What the hell?"); break; } } }
I'll never look at a calculator the same way again after this...wow.
I can get the result, but:
(a) displays a weird result after the answer after the correct second calculation (i.e. 12 + 5 = 17.0 - 2, 15.0 7.0 OR 4 / 2 = 2.0 * 8 = 16.0 What the hell?)
(b) it doesn't break like it should after the second calculation. It doesn't end unless "what the hell" comes out.
I'm thinking both are resulting from the same mistake; bracket problem (in switch(another)), but I can't point exactly where and how to fix it...
A few other Q's:
(1) Is there any way to put numeric operations (i.e. +,-) on the same line as my numbers? For example, make 2 + 3 runnable with an intended outcome instead of asking for 2, 3, then +?
(2) How do I make this code better? I know using switch within a switch is 99.99% of the time a big No-No...but I just couldn't find an alternative to get the result at my current level of knowledge (toenail-deep).