Answer to Question #250356 in Java | JSP | JSF for guru

Question #250356

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


1
Expert's answer
2021-10-12T07:39:36-0400
import java.util.Scanner;


public class App {


	public static void main(String[] args) {
		Scanner keyBoard = new Scanner(System.in);
		String ans = "";
		int sumResult = 0;
		do {
			System.out.print("Enter two numbers: ");
			int number1 = keyBoard.nextInt();
			int number2 = keyBoard.nextInt();
			sumResult += number1 + number2;
			keyBoard.nextLine();
			System.out.print("Do you want another operation: ");
			ans = keyBoard.nextLine();
		} while (ans.compareToIgnoreCase("yes") == 0);
		System.out.println("Sum = " + sumResult);
		keyBoard.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