Results 1 to 3 of 3
Thread: Create a Calculator in Java
 07042007, 08:56 AM #1Senior Member
 Join Date
 Jun 2007
 Posts
 114
 Rep Power
 0
Create a Calculator in Java
Hi, i just created a very simple calculator using what i learned so far.
Im not sure if its in the correct section so correct me if it isnt.
Java Code:import java.util.*; public class Calculator { public static void main(String[] args) { /*Scanner input = new Scanner(System.in); System.out.println("What are you doing?"); if (answer.equalsIgnorecase("multiplication")) { Multiplication(); }*/ System.out.println("Hello, If you are doing Multiplication Type 1."); System.out.println("If you are doing Addition, Type 2."); System.out.println("If you are doing Subtraction, Type 3"); System.out.println("If you are doing Division, Type 4"); Scanner input = new Scanner(System.in); int decision; decision = input.nextInt(); if (decision == 1) { Multiplication(); } if (decision == 2) { Addition(); } if (decision == 3) { Subtraction(); } if (decision == 4) { Division(); } else { System.out.println("Those aren't valid numbers."); wrongInput(); } } public static void wrongInput() { System.out.println("If you are doing Multiplication Type 1."); System.out.println("If you are doing Addition, Type 2."); System.out.println("If you are doing Subtraction, Type 3"); System.out.println("If you are doing Division, Type 4"); Scanner input = new Scanner(System.in); int decision; decision = input.nextInt(); if (decision == 1) { Multiplication(); } if (decision == 2) { Addition(); } if (decision == 3) { Subtraction(); } if (decision == 4) { Division(); } else { System.out.println("Those aren't valid numbers."); wrongInput(); } } public static void Multiplication() { int mInput1; int mInput2; Scanner input = new Scanner(System.in); System.out.print("What is the first number? "); mInput1 = input.nextInt(); System.out.print("What is the second number? "); mInput2 = input.nextInt(); System.out.println(mInput1 + " times " + mInput2 + " equals " + mInput1 * mInput2); } public static void Addition() { int aInput1; int aInput2; int aAnswer; Scanner input = new Scanner(System.in); System.out.println("What is the first number?"); aInput1 = input.nextInt(); System.out.println("What is the second number?"); aInput2 = input.nextInt(); aAnswer = aInput1 + aInput2; System.out.println(aInput1 + " plus " + aInput2 + " equals " + aAnswer); } public static void Subtraction() { int sInput1; int sInput2; int sAnswer; Scanner input = new Scanner(System.in); System.out.println("What is the first number?"); sInput1 = input.nextInt(); System.out.println("What is the second number?"); sInput2 = input.nextInt(); sAnswer = sInput1  sInput2; System.out.println(sInput1 + " minus " + sInput2 + " equals " + sAnswer); } public static void Division() { int dInput1; int dInput2; int dAnswer; Scanner input = new Scanner(System.in); System.out.println("What is the first number?"); dInput1 = input.nextInt(); System.out.println("What is the second number?"); dInput2 = input.nextInt(); dAnswer = dInput2 / dInput1; System.out.println(dInput1 + " divided by " + dInput2 + " equals " + dAnswer); } }
Albert:rolleyes:
 07042007, 08:58 AM #2Member
 Join Date
 Jun 2007
 Posts
 92
 Rep Power
 0
To trim down the code a bit, you can eliminate your wrongInput method, by using a loop in the main method of your code, along with another question like "Do you want to do more calculations?"
The basic idea is: answer = ""
Java Code:While answer does not equal "no" Calculations logic(if statements, method calls etc...) answer = result of question to continue
You can also look into how to pass parameters, and use only one method called doCalculation() and pass it a value that tells which type of calculation to do. That would take out all of the repetition of code.
Greetings
Marcus:cool:
 07042007, 09:01 AM #3Member
 Join Date
 Jun 2007
 Posts
 95
 Rep Power
 0
Similar Threads

Java Calculator
By aapanju in forum New To JavaReplies: 3Last Post: 04172008, 06:33 AM 
Java calculator decimal
By cart1443 in forum New To JavaReplies: 2Last Post: 04162008, 02:19 PM 
calculator not working
By Renegade85 in forum New To JavaReplies: 5Last Post: 03102008, 04:27 PM 
How to create ToolTip in Java 3d
By roshithmca in forum AWT / SwingReplies: 0Last Post: 02042008, 07:57 AM 
Swing Calculator
By nemo in forum AWT / SwingReplies: 1Last Post: 05282007, 12:07 PM
Bookmarks