In: Computer Science
In JAVA answer has to be able to do exact Sample Output shown
Write 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. Name your class as NumberSum and add to it your header and sample output as a block comments. Upload the NumberSum.java file to this link.
Sample output:
Enter a number: 1 Enter another number: 2 Their sum is 3.0 Do you wish to do this again? (Y/N) y Enter a number: 5 Enter another number: 9 Their sum is 14.0 Do you wish to do this again? (Y/N) n
SOURCE CODE:
*Please follow the comments to better understand the code.
**Please look at the Screenshot below and use this code to copy-paste.
***The code in the below screenshot is neatly indented for better understanding.
import java.util.Scanner; public class NumberSum { public static void main(String[] args) { Scanner input=new Scanner(System.in); /* Start the do - while loop */ char choice; do { // read two numbers System.out.print("Enter a number: "); int num1=input.nextInt(); System.out.print("Enter another number: "); int num2=input.nextInt(); /* Print the sum */ double sum=num1+num2; System.out.println("Their sum is "+sum); // ask if the user wishes to continue again. System.out.print("Do you wish to do this again? (Y/N) "); choice=input.next().charAt(0); }while ((choice=='y' || choice=='Y')); // repeat till he enters no } }
=====================