Ask Question
8 July, 16:21

Complete the do-while loop to output 0 to the value of countLimit using printVal. Assume the user will only input a positive number. For example, if countLimit is 5 the expected output will be

+2
Answers (1)
  1. 8 July, 18:24
    0
    Question not complete

    Complete the do-while loop to output every number form 0 to countLimit using printVal. Assume the user will only input a positive number. For example, if countLimit is 5 the expected output will be 0 1 2 3 4 5

    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;

    }

    }

    Answer:

    Replace while ( / * Your solution goes here * / );

    With

    while (printVal < = countLimit);

    This will check if the value of variable printVal is still within print range of variable countLimit.

    The full program becomes

    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 (printVal < = countLimit);

    System. out. println ("");

    return;

    }

    }
Know the Answer?
Not Sure About the Answer?
Find an answer to your question ✅ “Complete the do-while loop to output 0 to the value of countLimit using printVal. Assume the user will only input a positive number. For ...” in 📘 Mathematics if you're in doubt about the correctness of the answers or there's no answer, then try to use the smart search and find answers to the similar questions.
Search for Other Answers