Question

In: Computer Science

Modify the following java code, utilizing a loop mechanism to enable the user to use the...

Modify the following java code, utilizing a loop mechanism to enable the user to use the calculator more than once.
The program does the following:
   It prompts the user to enter 2 numbers.
   It prompts the user to choose an operation to perform on those numbers:
   Operation 1: Addition.
   Operation 2: Subtraction.
   Operation 3: Multiplication.
   Operation 4: Division.
   It outputs the result of the operation.
   It asks the user if they want to perform another calculation, using “-1" as a stop value.

The code:

package udh;
import java.util.Scanner;
public class Udh {


public static void main(String[] args) {
Scanner input = new Scanner(System.in);
  

System.out.println("Enter two numbers");
int num1 = input.nextInt();
int num2 = input.nextInt();

System.out.println("Choose the operator; *, /, +, - ");
char choice = input.next().charAt(0);
switch (choice)
{
case '+':
int add= num1+num2;
System.out.println(add);
break;

case '-':
int sub = num1-num2;
System.out.println(sub);
break;

case '*':
int mul= num1*num2;
System.out.println(mul);
break;

case '/':
double div= num1/num2;
System.out.println(div);
break;

default :
System.out.println("Invalid operator");
}
  

}
}

Solutions

Expert Solution

To enable the user to do multiple calculations we use do-while loop. We take a variable "next" and if the user does not wants to calculate further, then next = -1.


The code is as follows:

package udh;

import java.util.Scanner;
public class Udh {

public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int next; //next variable decides whether to calculate more based on user choice
        do{
                System.out.println("Enter two numbers");
                int num1 = input.nextInt();
                int num2 = input.nextInt();
                
                System.out.println("Choose the operator; *, /, +, - ");
                char choice = input.next().charAt(0);
                switch (choice){
                        case '+':
                        int add= num1+num2;
                        System.out.println(add);
                        break;
                        
                        case '-':
                        int sub = num1-num2;
                        System.out.println(sub);
                        break;
                        
                        case '*':
                        int mul= num1*num2;
                        System.out.println(mul);
                        break;
                        
                        case '/':
                        double div= (double)num1/num2;
                        System.out.println(div);
                        break;
                        
                        default :
                        System.out.println("Invalid operator");
                }
                        //Taking input for stopping
                        System.out.println("Another calculation? (Type -1 to stop)");
                        next = input.nextInt(); //Assignment of value to next
        }while(next!=-1); //Do while loop for multiple calculations. It iterates if next is not -1
        System.out.println("Bye Bye!!");
        input.close();
}
}

Output:

Hope this helps
If you have any doubt feel free to comment
Thank You!!


Related Solutions

in java code Modify your program as follows: Ask the user for the number of hours...
in java code Modify your program as follows: Ask the user for the number of hours worked in a week and the number of dependents as input, and then print out the same information as in the initial payroll assignment. Perform input validation to make sure the numbers entered by the user are reasonable (non-negative, not unusually large, etc). Let the calculations repeat for several employees until the user wishes to quit the program. Remember: Use variables or named constants...
Re-write following while loop into Java statements that use a Do-while loop. Your final code should...
Re-write following while loop into Java statements that use a Do-while loop. Your final code should result in the same output as the original code below. int total = 0; while(total<100) { System.out.println("you can still buy for"+(100-total)+"Dollars"); total=total+5; }
Modify the previous program to use the Do-While Loop instead of the While Loop. This version...
Modify the previous program to use the Do-While Loop instead of the While Loop. This version of the program will ask the user if they wish to enter another name and accept a Y or N answer. Remove the "exit" requirement from before. Output: Enter the full name of a person that can serve as a reference: [user types: Bob Smith] Bob Smith is reference #1 Would you like to enter another name (Y or N)? [user types: y] Enter...
JAVA CODE, USE FOR LOOP PLEASE Using the PurchaseDemo program and output as your guide, write...
JAVA CODE, USE FOR LOOP PLEASE Using the PurchaseDemo program and output as your guide, write a program that uses the Purchase class to set the following prices, and buy the number of items as indicated. Calculate the subtotals and total bill called total. Using the writeOutput() method display the subtotals as they are generated as in the PurchaseDemo program. Then print the total bill at the end Use the readInput() method for the following input data Oranges: 10 for...
java code Write a program that gives the user a menu of six choices (use integers)...
java code Write a program that gives the user a menu of six choices (use integers) to select from. The choices are circle, triangle, cone, cylinder, sphere, and quit. (The formulas are given below.) Once the figure is calculated, an informative message should be printed and the user shown the menu again, so that another choice can be made. The formulas are: Area of circle: a = 3.14 * radius * radius Area of triangle: a = ½ base *...
Java Program. Sentinel While Loop Lab Do the following: Prompts the user to enter a grade...
Java Program. Sentinel While Loop Lab Do the following: Prompts the user to enter a grade or a -1 to quit. IF the user entered a -1 THEN Display a message that the User is done entering grades ELSE Count each grade as it is entered. Compute a running total of the grades entered. END IF After the user enters the sentinel of -1, calculate the average of the grades entered. When computing the average, make sure that there is...
​​​​​​​For java program. Write a while loop that will let the user enter a series of...
​​​​​​​For java program. Write a while loop that will let the user enter a series of integer values and compute the total values and number of values entered. An odd number will stop the loop. Display the number of iterations and the total of the values after the loop terminates. for Loop Write a for loop to display all numbers from 13 - 93 inclusive, ending in 3. Write a for loop to display a string entered by the user...
Introduction to Java Programing Using Loop Create a simple calculator program using loop Ask user to...
Introduction to Java Programing Using Loop Create a simple calculator program using loop Ask user to input two numbers using scanner class Print the instruction of the menu for the calculator program Ask user to press 0 to Quit Ask user to press 1 to Add Ask user to press 2 to Substract Ask user to press 3 to Multiply Ask user to press 4 to Divide Perform correct calcuation based on user inputs and print the result Print error...
Java program Use Do-while Write a do-wile loop that asks the user to enter two numbers....
Java program Use Do-while Write a do-wile loop that asks the user to enter two numbers. The numbers should be added and the sum displayed. The loop should ask the user whether he or she wishes to perform the operation again. If so, the loop should repeat, otherwise, it should terminate. Use Continue branching statement Write a program that reads an integer and display all the positive ODD numbers from 0 to n (integer entered by the user). Use CONTINUE...
modify the code below to create a GUI program that accepts a String from the user...
modify the code below to create a GUI program that accepts a String from the user in a TextField and reports whether or not there are repeated characters in it. Thus your program is a client of the class which you created in question 1 above. N.B. most of the modification needs to occur in the constructor and the actionPerformed() methods. So you should spend time working out exactly what these two methods are doing, so that you can make...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT