Question

Write an expression that executes the loop while the user enters a number greater than or...

Write an expression that executes the loop while the user enters a number greater than or equal to 0.

Note: These activities may test code with different test values. This activity will perform three tests, with user input of 9, 5, 2, -1, then with user input of 0, -17, then with user input of 0 1 0 -1.

Also note: If the submitted code has an infinite loop, the system will stop running the code after a few seconds, and report "Program end never reached." The system doesn't print the test case that caused the reported message.

import java.util.Scanner;

public class NonNegativeLooper {

public static void main (String [] args) {

Scanner scnr = new Scanner(System.in);

int userNum;

userNum = scnr.nextInt();

while (/* Your solution goes here */) {

System.out.println("Body");

userNum = scnr.nextInt();

}

System.out.println("Done.");

}

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1
import java.util.Scanner;

public class NonNegativeLooper {
    public static void main(String[] args) {
        Scanner scnr = new Scanner(System.in);
        int userNum;
        userNum = scnr.nextInt();
        while (userNum >= 0) {
            System.out.println("Body");
            userNum = scnr.nextInt();
        }
        System.out.println("Done.");
    }
}
Add a comment
Know the answer?
Add Answer to:
Write an expression that executes the loop while the user enters a number greater than or...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write an expression that executes the loop while the user enters a number greater than or...

    Write an expression that executes the loop while the user enters a number greater than or equal to 0. Note: These activities may test code with different test values. This activity will perform three tests, with userNum initially 9 and user input of 5, 2, -1, then with userNum initially 0 and user input of -17, then with userNum initially -1. See "How to Use zyBooks". . Also note: If the submitted code has an infinite loop, the system will...

  • Write a while loop that prints userNum divided by 2 (integer division) until reaching 1. Follow...

    Write a while loop that prints userNum divided by 2 (integer division) until reaching 1. Follow each number by a space. Example output for userNum = 40: 20 10 5 2 1 Note: These activities may test code with different test values. This activity will perform four tests, with userNum = 40, then with userNum = 2, then with userNum = 0, then with userNum = -1. See "How to Use zyBooks". Also note: If the submitted code has an...

  • Complete the do-while loop to output 0 to countLimit. Assume the user will only input a...

    Complete the do-while loop to output 0 to countLimit. Assume the user will only input a positive number. import java.util.Scanner; public class CountToLimit { public static void main (String [] args) { Scanner scnr = new Scanner(System.in); int countLimit = 0; int printVal = 0; // Get user input countLimit = scnr.nextInt(); printVal = 0; do { System.out.print(printVal + " "); printVal = printVal + 1; } while ( /* Your solution goes here */ ); System.out.println(""); return; } }

  • Write a do-while loop that continues to prompt a user to enter a number less than...

    Write a do-while loop that continues to prompt a user to enter a number less than 100, until the entered number is actually less than 100. End each prompt with newline Ex: For the user input 123, 395, 25, the expected output is: Enter a number (<100): Enter a number (<100): Enter a number (<100): Your number < 100 is: 25 import java.util.Scanner; public class NumberPrompt { public static void main (String [] args) { Scanner scnr = new Scanner(System.in);...

  • 7.2.3: Printing array elements with a for loop. Write a for loop to print all elements...

    7.2.3: Printing array elements with a for loop. Write a for loop to print all elements in courseGrades, following each element with a space (including the last). Print forwards, then backwards. End each loop with a newline. Ex: If courseGrades = {7, 9, 11, 10}, print: 7 9 11 10 10 11 9 7 Hint: Use two for loops. Second loop starts with i = courseGrades.length - 1. (Notes) Also note: If the submitted code tries to access an invalid...

  • 4.2.3: Basic while loop expression. Write a while loop that prints userNum divided by 4 (integer...

    4.2.3: Basic while loop expression. Write a while loop that prints userNum divided by 4 (integer division) until reaching 2. Follow each number by a space. Example output for userNum = 160: 40 10 2 Note: These activities may test code with different test values. This activity will perform four tests, with userNum = 160, then with userNum = 8, then with userNum = 0, then with userNum = -1. See "How to Use zyBooks". Also note: If the submitted...

  • What will the code look like for this? (Python) Write an expression that executes the loop...

    What will the code look like for this? (Python) Write an expression that executes the loop body as long as the user enters a non-negative number. Note: If the submitted code has an infinite loop, the system will stop running the code after a few seconds and report "Program end never reached." The system doesn't print the test case that caused the reported message. Sample outputs with inputs: 9 5 2 -1 Body Body Body Done.

  • Write three statements to print the first three elements of array runTimes. Follow each statement with...

    Write three statements to print the first three elements of array runTimes. Follow each statement with a newline. Ex: If runTime = {800, 775, 790, 805, 808}, print: 800 775 790 Note: These activities will test the code with different test values. This activity will perform two tests, both with a 5-element array. See "How to Use zyBooks". Also note: If the submitted code tries to access an invalid array element, such as runTimes[9] for a 5-element array, the test...

  • Print 'Censored if userinput contains the word"darn', else print userinput End with newline. Ex iIf userinput...

    Print 'Censored if userinput contains the word"darn', else print userinput End with newline. Ex: if userinput is "That darn cat.:. then output is: Censored Ex: If userinput is Dang, that was scaryl", then output is: Dang, that was scary Note: If the submitted code has an out-of-range access, the system will stop running the code after a few seconds, and report Program end never reached The system doesn't print the test case that caused the reported message. import java.util.Scanner; public class Censoredwords {public static...

  • CHALLENGE ACTIVITY 3.5.1: Detect specific values. Write an expression that prints "Special number if specialNum is-99,...

    CHALLENGE ACTIVITY 3.5.1: Detect specific values. Write an expression that prints "Special number if specialNum is-99, 0, or 44. 1 import java.util.Scanner; 3 public class Find SpecialValue { public static void main (String [] args) { Scanner scnr = new Scanner(System.in); int specialNum; specialNum - scnr.nextInt(); POSLOVOU if (/* Your solution goes here */) { System.out.println("Special number"); else { System.out.println("Not special number"); Run View your last submission

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT