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
Sample Run1
Enter two numbers: 3 15
Do you want another operation: Yes
Enter two numbers: 45 56
Do you want another operation: No
Output1: Sum = 119
Sample Run2
Enter two numbers: 33 150
Do you want another operation: Yes
Enter two numbers: -56 56
Do you want another operation: Yes
Enter two numbers: 58 15
Do you want another operation: Yes
Enter two numbers: 123 87
Do you want another operation: No
Output2: Sum = 466
import java.util.Scanner;
public class App {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String answer = "";
int sum = 0;
do {
System.out.print("Enter two numbers: ");
int number1 = input.nextInt();
int number2 = input.nextInt();
sum += number1 + number2;
input.nextLine();
System.out.print("Do you want another operation: ");
answer = input.nextLine();
} while (answer.compareToIgnoreCase("yes") == 0);
System.out.println("Sum = " + sum);
input.close();
}
}
Comments
You guys are the best.
Leave a comment