Results 1 to 5 of 5
Thread: Need help with Calculator
 12102011, 01:14 AM #1Member
 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
 12102011, 01:49 AM #2Moderator
 Join Date
 Feb 2009
 Location
 New Zealand
 Posts
 4,643
 Rep Power
 13
Re: Need help with Calculator
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:
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?
Java Code:if(response == 1) { } else if(response == 2) { } else //etc
 12102011, 03:37 AM #3Member
 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: "?
 12102011, 03:39 AM #4Member
 Join Date
 Oct 2011
 Posts
 79
 Rep Power
 0
Re: Need help with Calculator
use a loop
 12102011, 03:44 AM #5Moderator
 Join Date
 Feb 2009
 Location
 New Zealand
 Posts
 4,643
 Rep Power
 13
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: 04252011, 11:24 PM 
Help with AWT CALCULATOR
By Megan Dosnueve in forum AWT / SwingReplies: 2Last Post: 04042011, 05:49 PM 
Help in a calculator
By Ayannie in forum New To JavaReplies: 6Last Post: 01042011, 09:21 PM 
help with calculator
By kalibballer in forum New To JavaReplies: 8Last Post: 04012009, 12:57 PM 
Calculator help.
By madkidd02 in forum New To JavaReplies: 2Last Post: 10252008, 07:42 AM
Bookmarks