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; }
Use Java to rewrite the following pseudo-code segment using a loop structure without goto, break, or...
Use Java to rewrite the following pseudo-code segment using a loop structure without goto, break, or any other unconditional branching statement: k = (j+13)/27; //assume i,j,k are integers properly declared. loop: if k > 10 then goto out k=k+1.2; i=3*k-1; goto loop; out: …
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 *...
Write a java code to ask the user for a string, and assuming the user enters...
Write a java code to ask the user for a string, and assuming the user enters a string containing character "a", your java program must convert all "a" to "b" in the string so for example if user types : "AMAZON" , your program must convert it to "BMBZON" the driver program must ask : enter a string containing "a" or "A" --------- (user enters string) then it should say; HERE IS YOUR NEW STRING ---------- (string with all a's...
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...
JAVA programming language Modify the following code. Make changes so that Movies can be sorted by...
JAVA programming language Modify the following code. Make changes so that Movies can be sorted by title ----------------------------------------------------------------- package Model; import java.time.Duration; import java.time.LocalDate; import java.util.ArrayList; import java.util.List; // TODO - Modify the movie class so that Java knows how to compare two Movies for sorting by title public class Movie extends DataStoreObj { private String title; private String description; private LocalDate releaseDate; private Duration runningTime; private Rating rating; private List<Genre> genres = new ArrayList<>(); private List<Actor> actors = new...
​​​​​​​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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT