Question #249735

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 


Expert's answer

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();
	}
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

LATEST TUTORIALS
APPROVED BY CLIENTS