Results 1 to 5 of 5
- 11-03-2007, 03:32 PM #1
Member
- Join Date
- Nov 2007
- Posts
- 8
- Rep Power
- 0
Adding Variables and using Switch
Hey guys, made a basic calculator, with the following code:
Also we have the following error'sJava Code:import java.lang.*; import java.util.*; import java.io.*; import java.net.*; public class CalcApp extends Object { public static void main(String[] argStrings) throws Exception { Scanner calcInput = new Scanner (System.in); System.out.printf ("Please enter your first number\n"); int firstInt = calcInput.nextInt(); System.out.printf ("Please enter your second number\n"); int secondInt = calcInput.nextInt(); System.out.printf ("Your second number is " + secondInt + "\n"); System.out.printf ("Your first number is " + firstInt + "\n"); System.out.printf ("Please enter your operator (1 = +) (2 = -) (3= /) \n"); int calcOperatorInput = calcInput.nextInt(); switch (calcOperatorInput) { case 1: System.out.println("Your Operator is (+)"); int calcAnswer = ( firstInt + secondInt ); break; case 2: System.out.println("Your Operator is (-)"); int calcAnswer = ( firstInt - secondInt ); break; case 3: System.out.println("Your Operator is (/)"); int calcAnswer = ( firstInt / secondInt ); break; default: System.out.println("Sorry, You must enter a number beetween 1-3"); break; } System.out.printf ("Your answer is"+ calcAnswer +"\n"); } }
First of all calcAnswer is already defined, and also it cannot find symbol.
Thanks for the help
- 11-03-2007, 03:39 PM #2
You define calcAnswer variable inside scope of cases (check braces. Outside those braces calcAnswer is undefined.)
- 11-03-2007, 03:42 PM #3
Member
- Join Date
- Nov 2007
- Posts
- 8
- Rep Power
- 0
Have changed it,1 error down! But having a little problem, it says that calcAnswer variable has already been set, any ideas?Java Code:import java.lang.*; import java.util.*; import java.io.*; import java.net.*; public class CalcApp extends Object { public static void main(String[] argStrings) throws Exception { Scanner calcInput = new Scanner (System.in); System.out.printf ("Please enter your first number\n"); int firstInt = calcInput.nextInt(); System.out.printf ("Please enter your second number\n"); int secondInt = calcInput.nextInt(); System.out.printf ("Your second number is " + secondInt + "\n"); System.out.printf ("Your first number is " + firstInt + "\n"); System.out.printf ("Please enter your operator (1 = +) (2 = -) (3= /) \n"); int calcAnswer = 1; int calcOperatorInput = calcInput.nextInt(); switch (calcOperatorInput) { case 1: System.out.println("Your Operator is (+)"); int calcAnswer = ( firstInt + secondInt ); break; case 2: System.out.println("Your Operator is (-)"); int calcAnswer = ( firstInt - secondInt ); break; case 3: System.out.println("Your Operator is (/)"); int calcAnswer = ( firstInt / secondInt ); break; default: System.out.println("Sorry, You must enter a number beetween 1-3"); break; } System.out.printf ("Your answer is"+ calcAnswer +"\n"); } }
- 11-03-2007, 04:04 PM #4
Remove int before calcAnswer inside case scopes. You redefine same variable there. That is your error.
- 11-03-2007, 05:45 PM #5
Member
- Join Date
- Nov 2007
- Posts
- 8
- Rep Power
- 0
Similar Threads
-
Help with switch color
By Daniel in forum AWT / SwingReplies: 2Last Post: 09-18-2008, 07:54 AM -
Method in a Switch Statement
By cart1443 in forum New To JavaReplies: 6Last Post: 03-14-2008, 03:48 AM -
Switch Statement Help
By bluegreen7hi in forum New To JavaReplies: 6Last Post: 02-06-2008, 05:16 AM -
Switch Statemet
By Java Tip in forum Java TipReplies: 0Last Post: 11-30-2007, 09:16 AM -
Help with Switch string code
By toby in forum New To JavaReplies: 1Last Post: 08-07-2007, 06:00 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks