Ask Question
5 March, 11:28

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

+3
Answers (1)
  1. 5 March, 11:33
    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 the answers seem to be not correct or there’s no answer. Try a smart search to find answers to similar questions.
Search for Other Answers