Results 1 to 5 of 5
Thread: Need help with Calculator
- 12-10-2011, 12:14 AM #1
Member
- Join Date
- Dec 2011
- Posts
- 3
- Rep Power
- 0
Need help with Calculator
So some days ago I started to actually learn Java. I learned a few commands and syntax and decided to try my hand at making a calculator without following an online guide.
Java Code:import java.util.Scanner; public class Calculator { public static void main(String[] args) { //Calculator //Final formula: firstvalue + secondvalue = answer //Display message asking for first number. System.out.println("Please input first value: "); Scanner scan = new Scanner(System.in); int firstvalue = scan.nextInt(); //Pick what mathematics function. System.out.println("Type: \n '1' for Addition \n '2' for Subtraction \n '3' for Multiplication \n '4' for Division "); //Based on what function you chose, the function will then be carried out. //Addition. int addition = scan.nextInt(); if (addition == 1) { System.out.println("Addition selected: Input second number please: " + firstvalue + " + ?\n"); int secondvalue = scan.nextInt(); int answer = firstvalue + secondvalue; System.out.println(firstvalue + " + " + secondvalue + " = " + answer); } //Subtraction. int subtraction = scan.nextInt(); if (subtraction == 2) { System.out.println("Subtraction selected: Input second number please: " + firstvalue + " - ?\n"); int secondvalue = scan.nextInt(); int answer = firstvalue - secondvalue; System.out.println(firstvalue + " - " + secondvalue + " = " + answer); } //Multiplication. int multiplication = scan.nextInt(); if (multiplication == 3) { System.out.println("Multiplication selected: Input second number please: " + firstvalue + " x ?\n"); int secondvalue = scan.nextInt(); int answer = firstvalue * secondvalue; System.out.println(firstvalue + " x " + secondvalue + " = " + answer); } //Division. int division = scan.nextInt(); if (division == 4) { System.out.println("Division selected: Input second number please: " + firstvalue + " / ?\n"); int secondvalue = scan.nextInt(); int answer = firstvalue / secondvalue; System.out.println(firstvalue + " / " + secondvalue + " = " + answer); } } }
The code works, but my dilemma is that only the addition section works. This is probably due to the fact that code works it's way down, and since the addition condition is not met, none of the other conditions work. How would I make it so that all of the Addition, Subtraction, Multiplication, and Division conditions are activated at the same time?
Thanks in advance for your help. :)
-Joshua
- 12-10-2011, 12:49 AM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,546
- Rep Power
- 11
Re: Need help with Calculator
Yes - you ask about addition, then you ask about subtraction etc.only the addition section works. This is probably due to the fact that code works it's way down, and since the addition condition is not met, none of the other conditions work.
A better approach, since you prompt the user once you should get their response once and then deal with it:
Note that whatever they enter it will be 1 OR 2 OR 3 etc but never more than one thing. So we could write:Java Code:// prompt user and get response System.out.println("Type: \n '1' for Addition etc"); int response = scan.nextInt(); // deal with the response they give if(response == 1) { // addition code here } if(response == 2) { // subtraction code here } // etc - what happens if they enter 42?
Have you come across "switch" statements, because that's what we really have here.Java Code:if(response == 1) { } else if(response == 2) { } else //etc
- 12-10-2011, 02:37 AM #3
Member
- Join Date
- Dec 2011
- Posts
- 3
- Rep Power
- 0
Re: Need help with Calculator
Okay, thanks. I made that change and also changed the "int" to "double". Thanks again. :)
Now how would I reset the entire code so that once they get their answer it starts again at the top with "Please input first value: "?
- 12-10-2011, 02:39 AM #4
Member
- Join Date
- Oct 2011
- Posts
- 79
- Rep Power
- 0
Re: Need help with Calculator
use a loop
- 12-10-2011, 02:44 AM #5
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,546
- Rep Power
- 11
Re: Need help with Calculator
I agree, use a loop.
To elaborate just a little, it would be a good idea if the main() method were not allowed to grow like topsy. main() might just consist of a loop whose body consists of a call to another method to run the calculator and a prompt asking if the user wants to continue.
Similar Threads
-
calculator
By rithish in forum AWT / SwingReplies: 10Last Post: 04-25-2011, 11:24 PM -
Help with AWT CALCULATOR
By Megan Dosnueve in forum AWT / SwingReplies: 2Last Post: 04-04-2011, 05:49 PM -
Help in a calculator
By Ayannie in forum New To JavaReplies: 6Last Post: 01-04-2011, 08:21 PM -
help with calculator
By kalibballer in forum New To JavaReplies: 8Last Post: 04-01-2009, 12:57 PM -
Calculator help.
By madkidd02 in forum New To JavaReplies: 2Last Post: 10-25-2008, 07:42 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks