How can I get a calculation in this Calculation Code? - java

I need a little help here. The question is the output (PS! the last output line).The program only prints only number 2 on the last line. How can I get the calculation. Thank You!
Output is here:
Choose from the following calculations:
1: subtraction
2: addition
3: multiplication
4: division
5: remainder
Make your choice: 1
Type in the first number: 9
Type in the second number: 7
9 - 7 = 2
import java.util.Scanner;
public class Calculation
{
public static void main(String[] args)
{
System.out.println("Choose from the following calculations:");
System.out.println("1: subtraction");
System.out.println("2: addition");
System.out.println("3: multiplication");
System.out.println("4: division");
System.out.println("5: remainder");
Scanner input = new Scanner(System.in);
System.out.print("\nMake your choice:");
int choice = input.nextInt();
if( 1 <= choice && choice <= 5 )
{
System.out.print("\nType the first number: ");
int first = input.nextInt();
System.out.print("Type the second number: ");
int second = input.nextInt();
switch (choice)
{
case 1:
System.out.println(+ (first - second));
break;
case 2:
System.out.println(+ (first + second));
break;
case 3:
System.out.println(+ (first * second));
break;
case 4:
System.out.println(+ ((double)first / (double)second));
break;
case 5:
System.out.println(+ (first % second));
break;
default:
break;
}
}
else
{
System.out.println("Invalid choice");
}
}
}

try this
System.out.println(first+"-"+second+"="+(first-second));
System.out.println(first+"+"+second+"="+(first+second));
System.out.println(first+"*"+second+"="+(first*second));
System.out.println(first+"/"+second+"="+((double)first / (double)second));
System.out.println(first+"%"+second+"="+(first%second));
P.S you are not considering Divide by zero error for your division.

Related

I'm trying to have a message pop up for if an invalid menu item is chosen multiple times

The goal is if the user puts an invalid option in three times, the program will tell the user to try again later and end the program. The code I have is below, I hope this makes sense, I am sorry if it doesn't. If you have any questions please let me know. I've also never asked a question here before so it may not be uploaded correctly.
public static void main(String[] args) {
int num1 = 0, num2 = 0, total = 0, option = 0, ex; // Creates integer variables
do {
Scanner sc = new Scanner(System.in);
System.out.println("\tBasic Math Calculator");// Title
System.out.println("\t---------------------");
System.out.println("\tEnter your choice from the following menu:");
System.out.println("\t------------------------------------------");
System.out.println("1.\tAddition");// All the menu options
System.out.println("2.\tSubtraction");
System.out.println("3.\tMultiplication");
System.out.println("4.\tDivision");
System.out.println("5.\tGenerate Random number");
System.out.println("6.\tQuit");
boolean valid;
do {
valid = true;
try {
option = Integer.parseInt(sc.nextLine());// Stores the users answers
if (option < 1 || option > 6) {
System.out.println("Invalid input. Please try again.");
valid = false;
}
} catch (NumberFormatException e) {
System.out.println("Invalid input. Please try again.");
valid = false;
}
} while (!valid);
switch (option) {// The math and titles for every option
case 1:
System.out.println("You chose to add two numbers: ");
System.out.println("Enter your first number:");
num1 = sc.nextInt();
System.out.println("Enter your second number:");
num2 = sc.nextInt();
total = num1 + num2;
System.out.println("The two numbers you chose added together is " + total);
break;
case 2:
System.out.println("You chose to subtract two numbers: ");
System.out.println("Enter your first number:");
num1 = sc.nextInt();
System.out.println("Enter your second number:");
num2 = sc.nextInt();
total = num1 - num2;
System.out.println("The two numbers you chose subtracted together is " + total);
break;
case 3:
System.out.println("You chose to multiply two numbers: ");
System.out.println("Enter your first number:");
num1 = sc.nextInt();
System.out.println("Enter your second number:");
num2 = sc.nextInt();
total = num1 * num2;
System.out.println("The two numbers you chose multiplied together is " + total);
break;
case 4:
System.out.println("You chose to divide two numbers: ");
System.out.println("Enter your first number:");
num1 = sc.nextInt();
System.out.println("Enter your second number:");
num2 = sc.nextInt();
total = num1 / num2;
if (num2 == 0) {
System.out.println("You can't divide by 0");
} else {
System.out.println("The two numbers you chose divided together is " + total + "with a quotient of "
+ (num1 % num2));
}
break;
case 5:
System.out.println("You chose to get two random numbers: ");
System.out.println("Enter your lower limit:");
num1 = sc.nextInt();
System.out.println("Enter your upper limit:");
num2 = sc.nextInt();
total = num1 + num2;
Random rand = new Random();
int rand_int1 = rand.nextInt(num1 + num2);
System.out.println("The random intigers is: " + rand_int1);
break;
case 6:// If the user wants to quit
ex = 2;
break;
default:// Tells their option was incorrect
System.out.println("Invalid choice, choice " + option + " was not an option");
}
System.out.println("Do you want to continue?1.Yes 2.No");// Asks the user if they want to proceed
ex = sc.nextInt(); // A thank you message for the user for running the program
} while (ex == 1);
System.out.println("-----------------------------------------");
System.out.println("Thank you for using the basic calculator!");
}
You can do so by creating a nested loop before the start if switch statement:
//...
System.out.println("6.\tQuit");
boolean valid;
do {
valid = true;
try {
option = Integer.parseInt(sc.nextLine());// Stores the users answers
if (option < 1 || option > 6) {
System.out.println("Invalid input. Please try again.");
valid = false;
}
} catch (NumberFormatException e) {
System.out.println("Invalid input. Please try again.");
valid = false;
}
} while (!valid);
switch (option) {// The math and titles for every option
//...
A sample run after this change:
Basic Math Calculator
---------------------
Enter your choice from the following menu:
------------------------------------------
1. Addition
2. Subtraction
3. Multiplication
4. Division
5. Generate Random number
6. Quit
a
Invalid input. Please try again.
9
Invalid input. Please try again.
4
You chose to divide two numbers:
Enter your first number:
Check the full code at ideone.
You don't have to use the valid and do-while for checking times of invalid input
If you want to quit the program after 3 invalid option entered by user , you have to increment the count of ex and change the condition at while loop.
Initialize ex=1
Change case 6 and default as below
case 6:// If the user wants to quit
System.exit(0);
break;
default:
ex = ex+1;
break;
and
while condition as below
while (ex <4); //
Following is the full code
public static void main(String[] args) {
int num1 = 0, num2 = 0, total = 0, ex= 1; // Creates integer variables
do {
Scanner sc = new Scanner(System.in);
System.out.println("\tBasic Math Calculator");// Title
System.out.println("\t---------------------");
System.out.println("\tEnter your choice from the following menu:");
System.out.println("\t------------------------------------------");
System.out.println("1.\tAddition");// All the menu options
System.out.println("2.\tSubtraction");
System.out.println("3.\tMultiplication");
System.out.println("4.\tDivision");
System.out.println("5.\tGenerate Random number");
System.out.println("6.\tQuit");
switch (option) {// The math and titles for every option
case 1:
System.out.println("You chose to add two numbers: ");
System.out.println("Enter your first number:");
num1 = sc.nextInt();
System.out.println("Enter your second number:");
num2 = sc.nextInt();
total = num1 + num2;
System.out.println("The two numbers you chose added together is " + total);
break;
case 2:
System.out.println("You chose to subtract two numbers: ");
System.out.println("Enter your first number:");
num1 = sc.nextInt();
System.out.println("Enter your second number:");
num2 = sc.nextInt();
total = num1 - num2;
System.out.println("The two numbers you chose subtracted together is " + total);
break;
case 3:
System.out.println("You chose to multiply two numbers: ");
System.out.println("Enter your first number:");
num1 = sc.nextInt();
System.out.println("Enter your second number:");
num2 = sc.nextInt();
total = num1 * num2;
System.out.println("The two numbers you chose multiplied together is " + total);
break;
case 4:
System.out.println("You chose to divide two numbers: ");
System.out.println("Enter your first number:");
num1 = sc.nextInt();
System.out.println("Enter your second number:");
num2 = sc.nextInt();
total = num1 / num2;
if (num2 == 0) {
System.out.println("You can't divide by 0");
} else {
System.out.println("The two numbers you chose divided together is " + total + "with a quotient of "
+ (num1 % num2));
}
break;
case 5:
System.out.println("You chose to get two random numbers: ");
System.out.println("Enter your lower limit:");
num1 = sc.nextInt();
System.out.println("Enter your upper limit:");
num2 = sc.nextInt();
total = num1 + num2;
Random rand = new Random();
int rand_int1 = rand.nextInt(num1 + num2);
System.out.println("The random intigers is: " + rand_int1);
break;
case 6:// If the user wants to quit
System.out.println("Thank you for using the basic calculator!");
System.exit(0);
break;
default:// Tells their option was incorrect
ex=ex+1;
System.out.println("Invalid choice, choice " + option + " was not an option");
}
System.out.println("Do you want to continue?1.Yes 2.No");// Asks the user if they want to proceed
//ex = sc.nextInt(); // A thank you message for the user for running the program
} while (ex<4);
System.out.println("-----------------------------------------");
System.out.println("Thank you for using the basic calculator!");
}
Edited
Below is the sample output
Basic Math Calculator
---------------------
Enter your choice from the following menu:
------------------------------------------
1. Addition
2. Subtraction
3. Multiplication
4. Division
5. Generate Random number
6. Quit
7
Invalid choice, choice 7 was not an option
Please try again
Basic Math Calculator
---------------------
Enter your choice from the following menu:
------------------------------------------
1. Addition
2. Subtraction
3. Multiplication
4. Division
5. Generate Random number
6. Quit
7
Invalid choice, choice 7 was not an option
Please try again
Basic Math Calculator
---------------------
Enter your choice from the following menu:
------------------------------------------
1. Addition
2. Subtraction
3. Multiplication
4. Division
5. Generate Random number
6. Quit
7
Invalid choice, choice 7 was not an option
Please try again
-----------------------------------------
Thank you for using the basic calculator!

I am creating a simple calculator in switch statement using methods but it straight goes to default, even if i press correct case

I am making this simple calculator program using switch statements and methods. But when the user presses any option (which is correct), it does not read the case and goes straight to the default case.
I've done the same thing before, but without any class file or methods. I'm new to methods so I just tried to do the same thing but it's not working properly.
NewClass cal = new NewClass();
Scanner in = new Scanner(System.in);
System.out.println("Press 1 for Division.\nPress 2 for Multiplication.\nPress 3 for Addition.\nPress 4 for Substraction.");
char c = in.next().charAt(0);
System.out.println("Enter First Value : ");
int a = in.nextInt();
System.out.println("Enter Second Value : ");
int b = in.nextInt();
switch (c){
case 1:
cal.division(a, b);
break;
case 2:
cal.division(a, b);
break;
case 3:
cal.add(a, b);
break;
case 4:
cal.sub(a, b);
break;
default:
System.out.println("Invalid entry.");
break;
}
output:
Press 1 for Division.
Press 2 for Multiplication.
Press 3 for Addition.
Press 4 for Substraction.
Enter your selection : 3
Enter First Value :
4
Enter Second Value :
5
Invalid entry.
BUILD SUCCESSFUL (total time: 9 seconds)
There is no need to convert user input to a char, as java switches can handle ints. The following code will fix your issue:
NewClass cal = new NewClass();
Scanner in = new Scanner(System.in);
System.out.println("Press 1 for Division.\nPress 2 for Multiplication.\nPress 3 for Addition.\nPress 4 for Substraction.");
int c = in.nextInt();
System.out.println("Enter First Value : ");
int a = in.nextInt();
System.out.println("Enter Second Value : ");
int b = in.nextInt();
switch (c){
case 1:
cal.division(a, b);
break;
case 2:
cal.division(a, b);
break;
case 3:
cal.add(a, b);
break;
case 4:
cal.sub(a, b);
break;
default:
System.out.println("Invalid entry.");
break;
}

Use switch case within while loop

I'm trying to code simple calculator (all in one) using Switch cases in java. I came up with following code so far. However I'm stuck with while loop. I want to keep showing main menu after each case execution until user decides to exit the program.
public static void main(String[] args)
{
Scanner s=new Scanner(System.in);
System.out.println("Main Menu:");
System.out.println("1. Addition");
System.out.println("2. Substraction");
System.out.println("3. Multipication");
System.out.println("4. Division");
System.out.println("Enter your choice: ");
int i=s.nextInt();
System.out.println("ENTER FIRST NUMBER ");
int a=s.nextInt();
System.out.println("ENTER SECOND NUMBER ");
int b=s.nextInt();
int result=0;
switch(i)
{
case 1:
result=a+b;
break;
case 2:
result=a-b;
break;
case 3:
result=a*b;
break;
case 4:
result=a/b;
break;
default:
System.out.println("Wrong Choice.");
}
System.out.println("Answer is "+result);
}
}
Above code works fine. Program ends itself after execution of user selected choice. I want to put main menu on a repeat.
Add a while loop like this:
public static void main(String[] args) {
// Moved this outside the while loop as davidxxx pointed out +1
Scanner s = new Scanner(System.in);
while (true) {
System.out.println("Main Menu:");
System.out.println("1. Addition");
System.out.println("2. Substraction");
System.out.println("3. Multipication");
System.out.println("4. Division");
System.out.println("Enter your choice: ");
int i = s.nextInt();
System.out.println("ENTER FIRST NUMBER ");
int a = s.nextInt();
System.out.println("ENTER SECOND NUMBER ");
int b = s.nextInt();
int result = 0;//'result' will store the result of operation
switch (i) {
case 1:
result = a + b;
break;
case 2:
result = a - b;
break;
case 3:
result = a * b;
break;
case 4:
result = a / b;
break;
default:
System.out.println("Wrong Choice.");
}
System.out.println("Answer is " + result);
System.out.println("Go again?");
String goAgain = s.next();
if (!goAgain.equals("y")) {
break;
}
}
}
Try this:
import java.util.Scanner;
public class Calculator {
private static final String EXIT = "EXIT";
public static void main(String[] args) {
Calculator calc = new Calculator();
Scanner s = new Scanner(System.in);
while (true) {
String res = calc.runCalc(s);
if (res.equals(EXIT)) {
break;
} else {
System.out.println(res);
}
}
}
private String runCalc(Scanner s) {
System.out.println("Main Menu:");
System.out.println("1. Addition");
System.out.println("2. Substraction");
System.out.println("3. Multipication");
System.out.println("4. Division");
System.out.println("5. Exit");
System.out.println("Enter your choice: ");
int i = s.nextInt();
if (i == 5) {
return EXIT;
}
System.out.println("ENTER FIRST NUMBER ");
int a = s.nextInt();
System.out.println("ENTER SECOND NUMBER ");
int b = s.nextInt();
int result = 0;// 'result' will store the result of operation
switch (i) {
case 1:
result = a + b;
break;
case 2:
result = a - b;
break;
case 3:
result = a * b;
break;
case 4:
result = a / b;
break;
default:
return "Wrong Choice.";
}
return "Answer is " + result;
}
}
There is more than one way to achieve this, you can use
while loop.
do-while loop.
for loop.
I think do-while loop is better for your situation. Because either user wants to continue or not you have to proceed one time(before loop false). And you do not want to use another variable for quit the loop.
public static void main(String[] args)
{
Scanner s=new Scanner(System.in);
int result=0;
do{
System.out.println("Main Menu:");
System.out.println("-1. complete and calculate");
System.out.println("1. Addition");
System.out.println("2. Substraction");
System.out.println("3. Multipication");
System.out.println("4. Division");
System.out.println("Enter your choice: ");
int i=s.nextInt();
if(i ==-1){
System.out.println("Answer is "+result);
return;
}
System.out.println("ENTER FIRST NUMBER ");
int a=s.nextInt();
System.out.println("ENTER SECOND NUMBER ");
int b=s.nextInt();
switch(i)
{
case 1:
result=a+b;
break;
case 2:
result=a-b;
break;
case 3:
result=a*b;
break;
case 4:
result=a/b;
break;
default:
System.out.println("Wrong Choice.");
break;
}
}while(true);
}

Query related input of char/int

I'm not able to understand that why is the compiler just shows running(which is forever) when I change char to int in this program. By changing I mean using just int to take the option number and hence using int numbers itself in switch.
This one is normal char 1 which is working-
public static void main(String args[])
throws java.io.IOException{
char option; int i=0;
do{
if(i==1)
System.out.println("\nNotice: Wrong option chosen, pick again.");
i=1;
System.out.println("Help on:");
System.out.println("1. if");
System.out.println("2. switch");
System.out.println("3. while");
System.out.println("4. do-while");
System.out.println("5. for");
System.out.println("Pick any option for brief informatrion.");
option= (char)System.in.read();
}while(option<'1' || option>'5');
switch(option){
case '1':
System.out.println("The If:\n");
System.out.println("If(condition) statement;");
System.out.println("else statement;");
break;
case '2':
System.out.println("The Switch:\n");
System.out.println("switch(expression){");
System.out.println(" case constant:");
System.out.println(" statement sequence");
System.out.println(" break;");
System.out.println(" // ...");
System.out.println("}");
break;
case '3':
System.out.println("The While:\n");
System.out.println("while(condition statement;)");
break;
case '4':
System.out.println("The Do-While:\n");
System.out.println("do{");
System.out.println(" statement;");
System.out.println("}while(condition);");
break;
case '5':
System.out.println("The For:\n");
System.out.println("for(init; condition; iteration){");
System.out.println(" statement;");
System.out.println("}");
break;
}
}
}
This is int 1 which just keeps running forver
public static void main(String args[])
throws java.io.IOException{
int option; int i=0;
do{
if(i==1)
System.out.println("\nNotice: Wrong option chosen, pick again.");
i=1;
System.out.println("Help on:");
System.out.println("1. if");
System.out.println("2. switch");
System.out.println("3. while");
System.out.println("4. do-while");
System.out.println("5. for");
System.out.println("Pick any option for brief informatrion.");
option= System.in.read();
}while(option<1 || option>5);
switch(option){
case 1:
System.out.println("The If:\n");
System.out.println("If(condition) statement;");
System.out.println("else statement;");
break;
case 2:
System.out.println("The Switch:\n");
System.out.println("switch(expression){");
System.out.println(" case constant:");
System.out.println(" statement sequence");
System.out.println(" break;");
System.out.println(" // ...");
System.out.println("}");
break;
case 3:
System.out.println("The While:\n");
System.out.println("while(condition statement;)");
break;
case 4:
System.out.println("The Do-While:\n");
System.out.println("do{");
System.out.println(" statement;");
System.out.println("}while(condition);");
break;
case 5:
System.out.println("The For:\n");
System.out.println("for(init; condition; iteration){");
System.out.println(" statement;");
System.out.println("}");
break;
}
}
}
See https://docs.oracle.com/javase/7/docs/api/java/io/InputStream.html#read%28%29
The System.in.read return an int represent for character.
Let's say you type 1, it will read as 49.
And if you use file to be stdin when it reach EOF it will return -1
Even if the char version, the same error happens.
See this demo: https://ideone.com/Q83qxn

make a basic calculator [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
So I am new to Java and to practice my skills I decided to make a basic calculator. So this is my code:
/*Basic calculator.*/
package calculator;
import java.util.Scanner;
public class no_gui {
public static void main(String[] args) {
System.out.println("What math operation do you want to do?");
System.out.println("1. Addition, 2. Subtraction, 3. Multipilcation, 4. Division");
System.out.println("Enter number to corresponding math operation.");
Scanner calc = new Scanner(System.in);
double choice = calc.nextDouble();
double add1, add2, addAnswer, sub1, sub2, subAnswer, mult1, mult2, multAnswer, div1, div2, divAnswer;
if (choice == 1){
System.out.println("Addition");
System.out.println("Enter first number: ");
add1 = calc.nextDouble();
System.out.println("Enter second number: ");
add2 = calc.nextDouble();
addAnswer = add1 + add2;
System.out.println("Your answer is " + addAnswer);
if(choice == 2){
System.out.println("Subtraction");
System.out.println("Enter first number: ");
sub1 = calc.nextDouble();
System.out.println("Enter second number: ");
sub2 = calc.nextDouble();
subAnswer = sub1 + sub2;
System.out.println("Your answer is " + subAnswer);
if(choice == 3){
System.out.println("Multipilcation");
System.out.println("Enter first number: ");
mult1 = calc.nextDouble();
System.out.println("Enter second number: ");
mult2 = calc.nextDouble();
multAnswer = mult1 + mult2;
System.out.println("Your answer is " + multAnswer);
if(choice == 4){
System.out.println("Divsion");
System.out.println("Enter first number: ");
div1 = calc.nextDouble();
System.out.println("Enter second number: ");
div2 = calc.nextDouble();
divAnswer = div1 + div2;
System.out.println("Your answer is " + divAnswer);
}else{
System.out.println("Re-run to use again.");
}
}
}
}
}
}
Every time I run it and choose addition it'll do what ever is inside the if statement. But when I try subtraction it just prints out 2 and terminates the program. It worked before but today it stopped. I even tried a different method of coding and it still doesn't work.
EDIT: Thanks to everyone who responded. My main issue was resolved. But I also have this issue with switch statements and I forgot to mention that in my main question. Do I have to space everything? Does that cause the problem?
You have nested if clauses. Look at your code and think about it logically.
if (choice == 1) {
//add
if (choice == 2) {
//subtract
}
}
You will never enter subtract or any other option for that matter because the outer choice == 1 will never be true. Use this structure instead.
if (choice == 1) {
//add
}else if (choice == 2) {
//subtract
}
Or better, use switch statment. An example is given below:
int month = 8;
String monthString;
switch (month) {
case 1: monthString = "January";
break;
case 2: monthString = "February";
break;
case 3: monthString = "March";
break;
case 4: monthString = "April";
break;
case 5: monthString = "May";
break;
case 6: monthString = "June";
break;
case 7: monthString = "July";
break;
case 8: monthString = "August";
break;
case 9: monthString = "September";
break;
case 10: monthString = "October";
break;
case 11: monthString = "November";
break;
case 12: monthString = "December";
break;
default: monthString = "Invalid month";
break;
}
Example source : http://docs.oracle.com/javase/tutorial/java/nutsandbolts/switch.html
Your problem is that your checks of choice are all nested:
if (choice == 1){
// ...
if(choice == 2){
// ...
if(choice == 3){
// ...
if(choice == 4){
// ...
}else{
System.out.println("Re-run to use again.");
}
}
}
}
If choice is 1, then that block will be executed. If it's not 1, then the blocks that should be executed are all unreachable.
Try re-arranging your program like this instead:
if (choice == 1){
// ...
} else if(choice == 2){
// ...
} else if(choice == 3){
// ...
} else if(choice == 4){
// ...
} else {
System.out.println("Re-run to use again.");
}
Your problem would be self-evident if you clicked "format code" in your IDE...
You have (inappropriately) nested your if blocks, like this:
if (choice == 1) {
// do addition
if (choice == 2) {
// etc
Clearly, if choice is anything other than 1, the corresponding if block(s) will never get entered.
Instead, make the if blocks siblings:
if (choice == 1) {
// do addition
} else if (choice == 2) {
// etc
Because you are not closing your if blocks properly. you should:
if (choice == 1) {
System.out.println("Addition");
System.out.println("Enter first number: ");
add1 = calc.nextDouble();
System.out.println("Enter second number: ");
add2 = calc.nextDouble();
addAnswer = add1 + add2;
System.out.println("Your answer is " + addAnswer);
} else if (choice == 2) {
System.out.println("Subtraction");
System.out.println("Enter first number: ");
sub1 = calc.nextDouble();
System.out.println("Enter second number: ");
sub2 = calc.nextDouble();
subAnswer = sub1 + sub2;
System.out.println("Your answer is " + subAnswer);
} else if (choice == 3) {
System.out.println("Multipilcation");
System.out.println("Enter first number: ");
mult1 = calc.nextDouble();
System.out.println("Enter second number: ");
mult2 = calc.nextDouble();
multAnswer = mult1 + mult2;
System.out.println("Your answer is " + multAnswer);
} else if (choice == 4) {
System.out.println("Divsion");
System.out.println("Enter first number: ");
div1 = calc.nextDouble();
System.out.println("Enter second number: ");
div2 = calc.nextDouble();
divAnswer = div1 + div2;
System.out.println("Your answer is " + divAnswer);
} else {
System.out.println("Re-run to use again.");
}
It is better in this case to use a switch/case:
import java.util.Scanner;
public class no_gui {
public static void main(String[] args) {
Scanner calc = new Scanner(System.in);
//double choice = calc.nextDouble();
int choice = 0;
boolean getOut=false;
double add1, add2, addAnswer, sub1, sub2, subAnswer, mult1, mult2, multAnswer, div1, div2, divAnswer;
while(!getOut)
{
System.out.println("What math operation do you want to do?");
System.out.println("1. Addition, 2. Subtraction, 3. Multipilcation, 4. Division, else: stop");
System.out.println("Enter number to corresponding math operation:");
choice = 0;
choice = calc.nextInt();
switch(choice)
{
case 1:
System.out.println("Addition");
System.out.println("Enter first number: ");
add1 = calc.nextDouble();
System.out.println("Enter second number: ");
add2 = calc.nextDouble();
addAnswer = add1 + add2;
System.out.println("Your answer is " + addAnswer);break;
case 2:
System.out.println("Subtraction");
System.out.println("Enter first number: ");
sub1 = calc.nextDouble();
System.out.println("Enter second number: ");
sub2 = calc.nextDouble();
subAnswer = sub1 - sub2;
System.out.println("Your answer is " + subAnswer);break;
case 3:
System.out.println("Multipilcation");
System.out.println("Enter first number: ");
mult1 = calc.nextDouble();
System.out.println("Enter second number: ");
mult2 = calc.nextDouble();
multAnswer = mult1 + mult2;
System.out.println("Your answer is " + multAnswer);break;
case 4:
System.out.println("Divsion");
System.out.println("Enter first number: ");
div1 = calc.nextDouble();
System.out.println("Enter second number: ");
div2 = calc.nextDouble();
divAnswer = div1 + div2;
System.out.println("Your answer is " + divAnswer);break;
default:System.out.println("Good Bye");getOut = true;break;
}
}
}}

Categories

Resources