Question: Consider the following code segment. Scanner scan = new Scanner(System.in); char userResponse = 'Y'; int sum = 0; while (userResponse == 'Y'){ System.out.print(Please enter an
Consider the following code segment.
Scanner scan = new Scanner(System.in); char userResponse = 'Y'; int sum = 0; while (userResponse == 'Y'){ System.out.print("Please enter an integer: "); sum += scan.nextInt(); System.out.print("Continue? Y/N "); userResponse = scan.next().charAt(0); } System.out.println ("The sum is " + sum); 1. How could you modify the code so that the user could enter either a lower case 'y' or an upper case 'Y'? 2. Could this loop be rewritten as a for loop? Why or why not? 3. What would happen if the line to initialize the sum was placed as the first line within the while loop body? What would be the output?
Step by Step Solution
3.48 Rating (155 Votes )
There are 3 Steps involved in it
1 Accepting both y and Y Java while userResponse Y userResponse y rest of the code 2 Re... View full answer
Get step-by-step solutions from verified subject matter experts
