Question: transform this code to be c++ (OOP) ~ For task 1 in this lab, you will create program for guessing the random number from 1
transform this code to be c++ (OOP)
~
For task 1 in this lab, you will create program for guessing the random number from 1 to 100 game. First, the program must generate the random number between 1 to 100 using library and commands that are provided in the lab. The player should enter the guess number on console. If guess number equals random number, the program will be stop and display the player win as shown in
If the player cannot guess the random number, the program should display message the player is lose and show the correct random number as shown in:
The program should guide player enters appropriate number. If the player enters the number that is not in the range. The program MUST detect the error and let player to reenter the guess number again as show in:
~
import java.util.Random;
import java.util.Scanner;
import jdk.nashorn.internal.parser.Lexer;
/**
*
* Program to guess random number
*/
public class RandomGuess {
public static void main(String arg[]) {
/*
input variable stores scanner object
min stores minimum value of random number
max stores maximum value of ranadom number
randomNo stores random number generated by system
userNumber stores guessed number by user
fnumber stores mimimum value to choose within range
lnumber stores max value to choose within range
*/
Scanner input = new Scanner(System.in);
int min = 1, max = 100, randomNo, userNumber;
int fnumber = 1, lnumber = 100;
boolean flag = true;
Random r = new Random();
randomNo = r.nextInt(max) + min;//generates random nubmer between 1 to 100
System.out.println("Enter guess number between 1 to 100:");
userNumber = input.nextInt();//input random number from user
input.nextLine();
while (flag) {
if (userNumber > randomNo || userNumber < randomNo) {//if user entered number less than or greater than system generated number
//change minimum,maximum range to take input from user
if (userNumber > randomNo) {
lnumber = userNumber - 1;
} else if (userNumber < randomNo) {
fnumber = userNumber + 1;
}
if (fnumber == lnumber) {
//if required number not found
System.out.println("You lose!!The random number is " + randomNo);
break;
}
System.out.println("Enter random number between " + fnumber + " to " + lnumber);
userNumber = input.nextInt();
input.nextLine();
} else {
System.out.println("You win!!The random number is " + randomNo);
flag = false;
}
}
}
}
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
