Answer to Question #249735 in Java | JSP | JSF for Nguu

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 


1
Expert's answer
2021-10-12T05:14:26-0400
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!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS