Change program from a while loop to a do while loop - when


Change program from a while loop to a do while loop.

import java.util.Random;

import java.util.Scanner;

import java.lang.System;

public class Game {


public static void main(String[] args){

System.out.println("Hello and welcome to my number guessing game.");

System.out.println("Pick a number: ");


Scanner inputnum = new Scanner(System.in);

int maxnum;

maxnum = inputnum.nextInt();


Random rand = new Random();

int number = rand.nextInt(maxnum);

int tries = 0;

Scanner input = new Scanner(System.in);

int guess;

boolean win = false;


while (win == false){


System.out.println("Guess a number between 1 and "+ maxnum +": ");

guess = input.nextInt();

tries++;

if (guess == number){

win = true;

}


else if(guess < number){

System.out.println("C'mon yourNumber is to low, tray again");


}


else if(guess > number){

System.out.println("Nope Number is to high, try again");


}


}


System.out.println("Way to go You win!");

System.out.println("It took you "+ tries + " tries.");


}

}

Solution Preview :

Prepared by a verified Expert
JAVA Programming: Change program from a while loop to a do while loop - when
Reference No:- TGS02709877

Now Priced at $10 (50% Discount)

Recommended (95%)

Rated (4.7/5)