I am getting user inputs do some calculation and then repeatedly ask the user to take repeatedly until a Sentinel value (3 in my case) entered.
I am using do-while loop and it does not give my desired output as shown below,
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("1: Addition");
System.out.println("2: Multiplication");
System.out.println("3: Exit");
System.out.print("Please choose a number: ");
int userinput = input.nextInt();
// Generate two random numbers
int number1 = (int)(Math.random() * 10);
int number2 = (int)(Math.random() * 10);
do {
if (userinput == 1) {
System.out.print("What is " + number1 + " + " + number2);
int answer = input.nextInt();
int tureanswer = number1 + number2;
if (answer == tureanswer){
System.out.println("You're correct");
}
else
System.out.println("Wrong,correct answer is " + tureanswer);
}
if (userinput == 2) {
System.out.print("What is " + number1 + " * " + number2 + " : ");
int answer = input.nextInt();
int tureanswer = number1 * number2;
if (answer == tureanswer){
System.out.println("Correct");
}
else
System.out.println("Wrong. The correct answer is "+ tureanswer);
}
}while(userinput !=3);
}
}
I am getting the following output,
1: Multiplication
2: Addition
3: Exit
Please choose a number: 1
What is 9 + 1 12
Wrong,correct answer is 10
What is 9 + 1
However, I need something like this,(prompts the user to select the number not what is 9 + 1)
1: Addition
2: Multiplication
3: Exit
Please choose a number: 1
What is 9 + 1 12
Wrong,correct answer is 10
1: Addition
2: Multiplication
3: Exit
Please choose a number:
What am I doing wrong in my do statement? Any thought would be appreciated!
It looks to me like you need to move do { up a few lines.
Only the part between do { and while gets repeated, and you need that to include asking the user for which type of problem they want, and generating the random numbers.
However, you will need to declare userInput before do if you want to use it as the while condition.
int userInput;
do {
System.out.print("Please choose a number: ");
userinput = input.nextInt();
// Generate two random numbers
int number1 = (int)(Math.random() * 10);
int number2 = (int)(Math.random() * 10);
if (userinput == 1) {
// and so on ...
Add following lines in your do while
System.out.println("1: Multiplication");
System.out.println("2: Addition");
System.out.println("3: Exit");
System.out.print("Please choose a number: ");
int userinput = input.nextInt();
int number1 = (int)(Math.random() * 10);
int number2 = (int)(Math.random() * 10);
I also suggest you to use switch in do while because it's more practical than if else
Related
I am working on a simple/scientific calculator in java, and I am having trouble putting this in a while loop so the user can continuously use the calculator. I've tried putting it in different places in the code, but it either repeats the input section or doesn't repeat anything. Any tips? Here is my code below:
static Scanner s1 = new Scanner(System.in);
public static void main(String[] args) {
System.out.println("Welcome to my calculator:");
String operator = "";
Scanner op = new Scanner(System.in);
System.out.println("Type 1 if you wish to use the Standard calculator, 2 for the Scientific calculator, or QUIT if you wish to quit the program.");
operator = op.nextLine();
if (operator.equals("1")) {
System.out.println(standard());
}
if (operator.equals("2")) {
System.out.println(scientific());
}
if (operator.equals("QUIT")) {
System.out.print("System quit");
}
}
public static int standard() {
//The system will print 0 at the end to show that it's working
System.out.println("Standard Calculator chosen.");
System.out.println("Type 1 if you wish to use addition, 2 for subtraction, 3 for multiplication, 4 for exponent, 5 for division, or 6 for mod.");
int input2 = s1.nextInt();
int num1 = 0;
int num2 = 0;
//String loop = "";
switch (input2) {
case 1:
System.out.println("(Add chosen) Please enter the first value: ");
num1 = s1.nextInt();
System.out.println("Please enter the second value: ");
num2 = s1.nextInt();
System.out.println("Addition - (" + num1 + "+" + num2 + ") = " + addExact(num1, num2));
break;
case 2:
System.out.println("(Sub chosen) Please enter the first value: ");
num1 = s1.nextInt();
System.out.println("Please enter the second value: ");
num2 = s1.nextInt();
System.out.println("Subtration - (" + num1 + "-" + num2 + ") = " + subtractExact(num1, num2));
break;
case 3:
System.out.println("(Multi chosen) Please enter the first value: ");
num1 = s1.nextInt();
System.out.println("Please enter the second value: ");
num2 = s1.nextInt();
System.out.println("Multiplication - (" + num1 + "*" + num2 + ") = " + multiplyExact(num1, num2));
break;
case 4:
System.out.println("(Exp chosen) Please enter the first value: ");
num1 = s1.nextInt();
System.out.println("Please enter the exponent: ");
num2 = s1.nextInt();
System.out.println("Exponent - (" + num1 + "^" + num2 + ") = " + Math.pow(num1, num2));
break;
case 5:
System.out.println("(Div chosen) Please enter the first value: ");
num1 = s1.nextInt();
System.out.println("Please enter the second value: ");
num2 = s1.nextInt();
System.out.println("Division - (" + num1 + "/" + num2 + ") = " + floorDiv(num1, num2));
break;
case 6:
System.out.println("(Mod chosen) Please enter the first value: ");
num1 = s1.nextInt();
System.out.println("Please enter the second value: ");
num2 = s1.nextInt();
System.out.println("Mod - (" + num1 + "%" + num2 + ") = " + floorMod(num1, num2));
break;
}
return (0);
}
public static double scientific() {
//The system will print 0.0 at the end to show that it's working
System.out.println("Scientific Calculator chosen.");
System.out.println("Type 1 for sin, 2 for cos, 3 for tan, 4 for floor, 5 for ceil, 6 for square root, 7 for cube root, 8 for rounding, 9 for min, 10 for max.");
int input2 = s1.nextInt();
double val1 = 0.0;
double val2 = 0.0;
switch (input2) {
case 1:
System.out.println("(Sin chosen) Please enter the value :");
val1 = s1.nextDouble();
System.out.println("Sin - (" + val1 + ") = " + sin(val1));
break;
case 2:
System.out.println("(Cos chosen) Please enter the value :");
val1 = s1.nextDouble();
System.out.println("Cos - (" + val1 + ") = " + cos(val1));
break;
case 3:
System.out.println("(Tan chosen) Please enter the value :");
val1 = s1.nextDouble();
System.out.println("Tan - (" + val1 + ") = " + tan(val1));
break;
case 4:
System.out.println("(Floor chosen) Please enter the value :");
val1 = s1.nextDouble();
System.out.println("Floor - (" + val1 + ") = " + Math.floor(val1));
break;
case 5:
System.out.println("(Ceil chosen) Please enter the value :");
val1 = s1.nextDouble();
System.out.println("Ceil - (" + val1 + ") = " + Math.ceil(val1));
break;
case 6:
System.out.println("(Square root chosen) Please enter the value :");
val1 = s1.nextDouble();
System.out.println("Square root - (" + val1 + ") = " + sqrt(val1));
break;
case 7:
System.out.println("(Cube root chosen) Please enter the value :");
val1 = s1.nextDouble();
System.out.println("Cube root - (" + val1 + ") = " + cbrt(val1));
break;
case 8:
System.out.println("(Round chosen) Please enter the value :");
val1 = s1.nextDouble();
System.out.println("Round - (" + val1 + ") = " + round(val1));
break;
case 9:
System.out.println("(Min chosen) Please enter the 1st value :");
val1 = s1.nextDouble();
System.out.println("Enter the 2nd value: ");
val2 = s1.nextDouble();
System.out.println("Minimum - (" + val1 + "," + val2 + ") = " + min(val1,val2));
break;
case 10:
System.out.println("(Max chosen) Please enter the 1st value :");
val1 = s1.nextDouble();
System.out.println("Enter the 2nd value: ");
val2 = s1.nextDouble();
System.out.println("Maximum - (" + val1 + "," + val2 + ") = " + max(val1,val2));
break;
}
return val2;
}
}
}
You need to do something as follows, as you want to repeat all the process until the user choose to QUIT the calculator app:
public static void main(String[] args) {
System.out.println("Welcome to my calculator:");
String operator = "";
while (!operator.equals("QUIT")) {
Scanner op = new Scanner(System.in);
System.out.println("Type 1 if you wish to use the Standard calculator, 2 for the Scientific calculator, or QUIT if you wish to quit the program.");
operator = op.nextLine();
if (operator.equals("1")) {
System.out.println(standard());
}
if (operator.equals("2")) {
System.out.println(scientific());
}
if (operator.equals("QUIT")) {
System.out.print("System quit");
}
}
}
output:
Welcome to my calculator:
Type 1 if you wish to use the Standard calculator, 2 for the Scientific calculator, or QUIT if you wish to quit the program.
1
Standard Calculator chosen.
Type 1 if you wish to use addition, 2 for subtraction, 3 for multiplication, 4 for exponent, 5 for division, or 6 for mod.
1
(Add chosen) Please enter the first value:
1
Please enter the second value:
2
Addition - (1+2) = 3
0
Type 1 if you wish to use the Standard calculator, 2 for the Scientific calculator, or QUIT if you wish to quit the program.
Here's a solution for your "main" method that:
uses a "while" loop and a boolean value "keepGoing" to decide if it should loop again (or exit)
uses a "switch" statement to handle calling different functions based on input
if "QUIT" input, it sets "keepGoing = false" so that the "while" loop will exit
defines one Scanner, and names it clearly ("scanner")
passes that single Scanner object to the methods which need it (standard and scientific)
Two other changes worth making:
remove the global static Scanner s1 – don't use global variables, it will lead to hard-to-find problems in your code
edit those method signatures to accept a Scanner parameter:
public static int standard(Scanner s1)`
public static double scientific(Scanner s1)
Here's the code:
Scanner scanner = new Scanner(System.in);
System.out.println("Welcome to my calculator:");
String prompt = "Type 1 if you wish to use the Standard calculator, " +
"2 for the Scientific calculator, or " +
"QUIT if you wish to quit the program.";
boolean keepGoing = true;
while (keepGoing) {
System.out.println(prompt);
switch (scanner.nextLine()) {
case "1" -> System.out.println(standard(scanner));
case "2" -> System.out.println(scientific(scanner));
case "QUIT" -> {
System.out.print("System quit");
keepGoing = false; // this ejects from the while loop
}
}
}
Im trying to get the output for the number of times the loop was ran over, decided by the user. Im using while() but I'm not sure how to output the number of loops once the loop is over.
Here's my code:
public class RockPaperScissors {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Choose a number between 2 and 10, choose 1 to end it");
int number1 = input.nextInt();
System.out.println("You Chose Number " + number1);
while (number1 != 1) {
System.out.println("Choose a number between 2 and 10, choose 1 to end it");
number1 = input.nextInt();
System.out.println("You Chose Number " + number1);
}
System.out.println("Thank you for choosing. Goodbye");
}
}
please help me thank you !!
You just need an additional variable that increments each time the loop occurs. Then after the loop you can use that variable to show the number of times looped.
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Choose a number between 2 and 10, choose 1 to end it");
int number1 = input.nextInt();
int loopcount = 0;
System.out.println("You Chose Number " + number1);
while (number1 != 1){
System.out.println("Choose a number between 2 and 10, choose 1 to end it");
number1 = input.nextInt();
loopcount += 1;
System.out.println("You Chose Number " + number1);
}
System.out.println("Thank you for choosing. Goodbye");
System.out.println("Looped " + loopcount + " times";
}
By the way, you could also optimise the function by eliminating the input request before the "while()" loop. It's repeated, redundant code that doesn't need to be there.
For example:
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int number1 = 0;
int loopcount = -1;
while (number1 != 1) {
System.out.println("Choose a number between 2 and 10, choose 1 to end it");
number1 = input.nextInt();
loopcount += 1;
System.out.println("You Chose Number " + number1);
}
System.out.println("Thank you for choosing. Goodbye");
System.out.println("Looped " + loopcount + " times";
}
If there is only requirement to count the looping then taking an int variable is enough, but if you want to keep records the chosen number then take a List which can store the chosen numbers dynamically and its length would be the looping count.
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int choice = 0;
int count=0;
List<Integer> numbers = new ArrayList(); // if chosen num is needed
do{
System.out.print("Choose a number between 2 and 10, choose 1 to end it :");
choice = scanner.nextInt();
if(choice !=1 ) {
count++;
numbers.add(choice); // if chosen num is needed
}
}while (choice != 1);
System.out.println("Loop counts " + count);
System.out.println("Loop counts " + numbers.size() + " times and numbers are "+numbers);
}
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'm stuck at the part where I loop back into: "yes" - I want to continue or "no" - I'd like to exit.
I'm aware I would need to display an if? to display results only if you exit the program
//Input your display and questions:
int num1 = (int) (Math.random() * 10 +1);
int num2 = (int) (Math.random() * 10 + 1);
int answer = 0;
int correctCount=0;
int count= 0;
//Scanner object input
Scanner input = new Scanner(System.in);
//Swap numbers if number2 is bigger than number 1
if (num1 < num2) {
int temp = num1;
num1 = num2;
num2 = temp;
}
//OUTPUT DISPLAY QUESTION: What is num1 + num2 = ?
System.out.print("What is "+num1 + " * " + num2 + " = ");
answer = input.nextInt();
//If answer is correct display: Good Job! Your got it right
if (answer == (num1 * num2))
{
System.out.println("Good job! You got it right!\n");
correctCount++; //Increases correct count for answer correct!
}
else
System.out.println("You got it wrong, try again!\n" +num1 + " * " +num2+ " should be " + (num1 * num2));
count++;
System.out.print("Enter Y to continue or N to exit: \n"); //Do you want to continue
System.out.println("\nYou got " +correctCount+ " out of "+count + " answer correct"); //Displays Correct with total# of questions
}
}
You have just to use a while loop to make the app loop until the user enter a character other than y.
also, try to use try-with-resource to avoid source leak with scanner object
Like this:
public static void main(String[] args) {
// Input your display and questions:
int answer = 0;
int correctCount = 0;
int count = 0;
String continueStr = "yes";
try (Scanner input = new Scanner(System.in)) {
while (continueStr.equalsIgnoreCase("yes")) {
// Scanner object input
int num1 = (int) (Math.random() * 10 + 1);
int num2 = (int) (Math.random() * 10 + 1);
// Swap numbers if number2 is bigger than number 1
if (num1 < num2) {
int temp = num1;
num1 = num2;
num2 = temp;
}
// OUTPUT DISPLAY QUESTION: What is num1 + num2 = ?
System.out.print("What is " + num1 + " * " + num2 + " = ");
answer = input.nextInt();
input.nextLine();
// If answer is correct display: Good Job! Your got it right
if (answer == (num1 * num2)) {
System.out.println("Good job! You got it right!\n");
correctCount++; // Increases correct count for answer correct!
} else {
System.out.println("You got it wrong, try again!\n" + num1 + " * " + num2 + " should be " + (num1 * num2));
}
count++;
System.out.print("\nEnter Yes to continue or No to exit: \n"); // Do you want to continue
continueStr = input.nextLine();
}
System.out.println("\nYou got " + correctCount + " out of " + count + " answer correct"); // Displays Correct with total# of questions
}
}
So this is my code for a simple calculator. It works for the most part, and I'm trying to create a loop at the end of the operation to allow a user the chance to choose to do another operation or exit. So far I've tried adding a nested if loop but I keep getting an error. Any help would be helpful. I'm fairly new.
import java.util.Scanner;
public class SimpleCalculator {
public static void main(String[] args) {
// Introduction to Calculator providing instructions to user
Scanner input = new Scanner(System.in);
int num1, num2, ans, Y, N;
System.out.println("Welcome to Simple Calculator: Please enter \n" +
"which mathimatical operation you would like to \n "
+ "accomplish by imputing 1-addition, 2-subtraction \n"
+ "3-multiplication, 4-division, 5-modulo");
int Operation = input.nextInt();
//Operation sequence for user to input which operation they would like to accomplish
if (Operation == 1) {
System.out.println("You have Choosen Addition");
System.out.print("Enter your First number: ");
num1 = (int) input.nextDouble();
System.out.print("Enter your second number: ");
num2 = (int) input.nextDouble();
ans = num1 + num2;
System.out.print("Answer is: " + ans);
/*
* Right here in the following I want to put something in to make this loop
* so the user can make multiple calculation.
* So far nothing has worked.
*/
System.out.println("Would you like to do another calculation?\n " +
"Enter Y for Yes or N to Exit");
}else if (Operation == 2){
System.out.println("You have Choosen Subtraction");
System.out.print("Enter your First number: ");
num1 = (int) input.nextDouble();
System.out.print("Enter your second number: ");
num2 = (int) input.nextDouble();
ans = num1 - num2;
System.out.print("Answer is: " + ans);
}else if (Operation == 3){
System.out.println("You have Choosen Multiplication");
System.out.print("Enter your First number: ");
num1 = (int) input.nextDouble();
System.out.print("Enter your second number: ");
num2 = (int) input.nextDouble();
ans = num1 * num2;
System.out.print("Answer is: " + ans);
}else if (Operation == 4){
System.out.println("You have Choosen Division");
System.out.print("Enter your First number: ");
num1 = (int) input.nextDouble();
System.out.print("Enter your second number: ");
num2 = (int) input.nextDouble();
ans = num1 / num2;
System.out.print("Answer is: " + ans);
}else if (Operation == 5){
System.out.println("You have Choosen Modulo");
System.out.print("Enter your First number: ");
num1 = (int) input.nextDouble();
System.out.print("Enter your second number: ");
num2 = (int) input.nextDouble();
ans = num1 % num2;
System.out.print("Answer is: " + ans);
}else{
System.out.print("Invalid Operation, please try again.");
}
}
}
You're on the right track here. I would suggest using a do-while loop. So encase the entire part beginning with if (Operation == 1) { all the way down to your final else statement like this:
do {
/*
if (Operation == 1) {
...
...
...
else {
...
}
*/
System.out.print("Would you like to do another calculation? [Y/n] ");
} while (input.nextLine().toUpperCase().charAt(0) == 'Y');
I would put the entire thing in a while loop and at the end ask if they want to continue
yes - it goes back through the loop
no - you have it break out of the loop