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

Respuesta :

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;

}

}

Following are the Java program that uses the do-while loop:

Program:

import java.util.*;//import package

public class Main//defining the class

{

public static void main (String [] args)//defining the main method

{

int countLimit = 0, printVal = 0;//defining integer variable that initializes with value 0

Scanner obd= new Scanner(System.in);//creating the Scanner class object

countLimit = obd.nextInt();//input value into countLimit variable

do //defining do-while loop

{

System.out.print(printVal + " ");//printing the printVal value

printVal++;//incrementing the printVal value by 1

}

while (printVal<=countLimit);//using loop that check printVal value less than equal to countLimit value

}

}

Program Explanation:

  • Import package.
  • Defining the class "Main".
  • Inside the class defining the main method, inside this method two integer variables "countLimit, printVal" is declared that initializes with 0.
  • In the next step, "countLimit" is declared that inputs value from the user-end.
  • After input value, a do-while loop is declared that checks "printVal<=countLimit" and prints the "printVal" value and increment its value by 1.

Output:

Please find the attached file.

Note:

  • Your question was incomplete, in the attached file the rest of the question is given please find it.

Find out more information about the do-while loop here:

brainly.com/question/25916519

Ver imagen codiepienagoya
Ver imagen codiepienagoya