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;

}

}

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);

int userInput = 0;

/* Your solution goes here */

System.out.println("Your number < 100 is: " + userInput);

return;

}

}

Respuesta :

ijeggs

Answer:

PART ONE

  1. import java.util.Scanner;
  2. public class CountToLimit {
  3.    public static void main(String[] args) {
  4.        Scanner scnr = new Scanner(System.in);
  5.        int countLimit = 0;
  6.        int printVal = 0;
  7.        // Get user input
  8.        System.out.println("Enter Count Limit");
  9.        countLimit = scnr.nextInt();
  10.        do {
  11.            System.out.print(printVal + " ");
  12.            printVal = printVal + 1;
  13.        } while ( printVal<=countLimit );
  14.        System.out.println("");
  15.        return;
  16.    }
  17. }

PART TWO

  1. import java.util.Scanner;
  2. public class NumberPrompt {
  3.    public static void main (String [] args) {
  4.        Scanner scnr = new Scanner(System.in);
  5.        System.out.print("Your number < 100: ");
  6.       int  userInput = scnr.nextInt();
  7.      do {
  8.           System.out.print("Your number < 100: ");
  9.           userInput = scnr.nextInt();
  10.       }while (userInput>=100);
  11.        System.out.println("Your number < 100 is: " + userInput);
  12.        return;
  13.    }
  14. }

Explanation:

In Part one of the question, The condition for the do...while loop had to be stated this is stated on line 14

In part 2, A do....while loop that will repeatedly prompt user to enter a number less than 100 is created. from line 7 to line 10

RELAXING NOICE
Relax