Results 1 to 3 of 3
- 01-06-2013, 05:17 PM #1
Member
- Join Date
- Jan 2013
- Posts
- 4
- Rep Power
- 0
[Help] Basic math program help. A easy to understand thread.
Hi people, I am new to Java and I have been learning by way of books. Right now I am learning about loops. So to test my knowledge I decided to make a simple calculator. This calculator has no gui, as I don't know how to make them, but the code and logic should be easy to follow.
Compiler Error: NoneJava Code:import java.util.Scanner; public class main { public static void main(String[] args) { int a; int b; String calcType; // get input for var int a System.out.println("Enter your first number:"); Scanner input1 = new Scanner(System.in); a = input1.nextInt(); System.out.println("You entered " + a); //get the math operator to be use in the calculation - var string "calcType" System.out.println("Now choose your operator. (/*-+)"); Scanner input2 = new Scanner(System.in); calcType = input2.next(); System.out.println("You entered " + calcType); //get the input for var int b System.out.println("Now choose your last number."); Scanner input3 = new Scanner(System.in); b = input3.nextInt(); System.out.println("You entered " + b); // Begin calculations! //This code figures out the math operator to be used and then does the appropriate calculation. if(calcType == "/") { double answer1 = a / b; System.out.println("The answer is: " + answer1); } else if(calcType == "*") { int answer2 = a * b; System.out.println("The answer is: " + answer2); } else if(calcType == "-") { int answer3 = a - b; System.out.println("The answer is: " + answer3); } else if(calcType == "+") { int answer4 = a + b; System.out.println("The answer is: " + answer4); } else { // Return if the calculation could not be completed. System.out.println("Houston we have a problem!"); } } }
Works Correctly: No
Returns: Houston we have a problem!
I believe the problem is happening in the "Begin calculations section." I am trying to compare the string the user entered (eg /*-+) with a || operator to a typed out version of the entered operator.
This is probably very simple to all of you and I appreciate your time in reading my thread and helping me make this code work. Please tell me what is done wrong.
Thank you!
Last edited by MagicalPeppers; 01-06-2013 at 05:19 PM.
- 01-06-2013, 05:25 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,375
- Blog Entries
- 7
- Rep Power
- 17
Re: [Help] Basic math program help. A easy to understand thread.
Don't compare objects (such as Strings) for equality wth the == operator; use the .equals( ... ) method instead, e.g. s.equals("+").
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 01-06-2013, 05:32 PM #3
Member
- Join Date
- Jan 2013
- Posts
- 4
- Rep Power
- 0
Re: [Help] Basic math program help. A easy to understand thread.
Thank you Jos for the quick response! I really appreciate you pointing out the correct syntax for me. And best of all I have learned something, which is awesome!
The new IF statement bock of code (works perfect):
Java Code:// Begin calculations! //This code figures out the math operator to be used and then does the appropriate calculation. if(calcType.equals("/")) { double answer1 = a / b; System.out.println("The answer is: " + answer1); } else if(calcType.equals("*")) { int answer2 = a * b; System.out.println("The answer is: " + answer2); } else if(calcType.equals("-")) { int answer3 = a - b; System.out.println("The answer is: " + answer3); } else if(calcType.equals("+")) { int answer4 = a + b; System.out.println("The answer is: " + answer4); } else { // Return if the calculation could not be completed. System.out.println("Houston we have a problem!"); }
Thanks again! you rock!
Last edited by MagicalPeppers; 01-06-2013 at 05:39 PM.
Similar Threads
-
Help me write an easy program?
By esined93 in forum New To JavaReplies: 1Last Post: 10-17-2012, 03:22 PM -
Can you help me understand this basic GUI concept?
By EscSequenceAlpha in forum New To JavaReplies: 1Last Post: 04-08-2012, 08:33 PM -
Basic Thread Help
By armyson in forum Threads and SynchronizationReplies: 2Last Post: 12-28-2011, 08:56 AM -
Another easy program I should be able to do
By Goff256 in forum New To JavaReplies: 12Last Post: 04-16-2011, 07:40 PM -
How do I perform basic math with BigDecimals
By s0meb0dy in forum New To JavaReplies: 4Last Post: 09-26-2010, 12:32 PM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks