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 DoWhile {
public static void main(String[] args) {
int condition = 0;
Scanner scan = new Scanner(System.in);
do {
int firstNumber = 0;
int secondNumber = 0;
int sumOfNumbers = 0;
System.out.println("Please provide first numbler and press Enter: ");
firstNumber = scan.nextInt();
System.out.println("Please provide second numbler and press Enter: ");
secondNumber = scan.nextInt();
sumOfNumbers = firstNumber + secondNumber;
System.out.println("Sum of entered numbers: " + sumOfNumbers);
System.out.println("Wont try again?Yes- press 1 or no-press 2 and press Enter: ");
condition = scan.nextInt();
} while (condition != 2);
scan.close();
}
}
Comments
Leave a comment