Consider the following code segment. Scanner scan = new Scanner(System.in); char userResponse = 'Y'; int sum =
Fantastic news! We've Found the answer you've been seeking!
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 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?
Related Book For
Computer organization and architecture designing for performance
ISBN: 978-0136073734
8th edition
Authors: william stallings
Posted Date: