Results 1 to 5 of 5
  1. #1
    notnumber6 is offline Member
    Join Date
    Nov 2007
    Posts
    8
    Rep Power
    0

    Default Adding Variables and using Switch

    Hey guys, made a basic calculator, with the following code:

    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 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");
    		
    		
    	}
    }
    Also we have the following error's

    First of all calcAnswer is already defined, and also it cannot find symbol.

    Thanks for the help

  2. #2
    JavaBean's Avatar
    JavaBean is offline Moderator
    Join Date
    May 2007
    Posts
    1,270
    Rep Power
    9

    Default

    You define calcAnswer variable inside scope of cases (check braces. Outside those braces calcAnswer is undefined.)

  3. #3
    notnumber6 is offline Member
    Join Date
    Nov 2007
    Posts
    8
    Rep Power
    0

    Default

    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");
    		
    		
    	}
    }
    Have changed it,1 error down! But having a little problem, it says that calcAnswer variable has already been set, any ideas?

  4. #4
    JavaBean's Avatar
    JavaBean is offline Moderator
    Join Date
    May 2007
    Posts
    1,270
    Rep Power
    9

    Default

    Remove int before calcAnswer inside case scopes. You redefine same variable there. That is your error.

  5. #5
    notnumber6 is offline Member
    Join Date
    Nov 2007
    Posts
    8
    Rep Power
    0

    Default

    Java bean,

    Legend, all working!

    Also i couldnt have the answer variable set as a string, at the top... when i was also trying to use it as an INT

Similar Threads

  1. Help with switch color
    By Daniel in forum AWT / Swing
    Replies: 2
    Last Post: 09-18-2008, 08:54 AM
  2. Method in a Switch Statement
    By cart1443 in forum New To Java
    Replies: 6
    Last Post: 03-14-2008, 04:48 AM
  3. Switch Statement Help
    By bluegreen7hi in forum New To Java
    Replies: 6
    Last Post: 02-06-2008, 06:16 AM
  4. Switch Statemet
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-30-2007, 10:16 AM
  5. Help with Switch string code
    By toby in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 07:00 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •