Create a do-while 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
import java.util.Scanner;
public class App {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String answer = "";
do {
System.out.print("Enter the first number: ");
int number1 = input.nextInt();
System.out.print("Enter the second number: ");
int number2 = input.nextInt();
double sum = number1 + number2;
System.out.println("Sum = " + sum);
input.nextLine();
System.out.print("Do you wish to perform the operation again? (y/n): ");
answer = input.nextLine();
} while (answer.compareToIgnoreCase("y") == 0);
input.close();
}
}
Comments
Leave a comment