Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 11-03-2007, 04:32 PM
Member
 
Join Date: Nov 2007
Posts: 7
notnumber6 is on a distinguished road
Adding Variables and using Switch
Hey guys, made a basic calculator, with the following code:

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
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 11-03-2007, 04:39 PM
JavaBean's Avatar
Moderator
 
Join Date: May 2007
Posts: 1,272
JavaBean is on a distinguished road
You define calcAnswer variable inside scope of cases (check braces. Outside those braces calcAnswer is undefined.)
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 11-03-2007, 04:42 PM
Member
 
Join Date: Nov 2007
Posts: 7
notnumber6 is on a distinguished road
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?
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 11-03-2007, 05:04 PM
JavaBean's Avatar
Moderator
 
Join Date: May 2007
Posts: 1,272
JavaBean is on a distinguished road
Remove int before calcAnswer inside case scopes. You redefine same variable there. That is your error.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 11-03-2007, 06:45 PM
Member
 
Join Date: Nov 2007
Posts: 7
notnumber6 is on a distinguished road
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
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Help with switch color Daniel AWT / Swing 2 09-18-2008 08:54 AM
Method in a Switch Statement cart1443 New To Java 6 03-14-2008 04:48 AM
Switch Statement Help bluegreen7hi New To Java 6 02-06-2008 06:16 AM
Switch Statemet Java Tip Java Tips 0 11-30-2007 10:16 AM
Help with Switch string code toby New To Java 1 08-07-2007 07:00 AM


All times are GMT +3. The time now is 03:16 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org