Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

CS 231 A-Computer Progr. -CS231-Programming-L × O wildcat cook an eduncscsfs/CS2

ID: 3590351 • Letter: C

Question

CS 231 A-Computer Progr. -CS231-Programming-L × O wildcat cook an eduncscsfs/CS23 1 Programming-I FALL-2017 Lecture 15.pdf target-b112d . 0 JAVA Bonus HW Write programs to: 1- Number guessing game. The program will pick a random number (See class Random) and ask the user to guess that number. The program output 'Too low' or Too high' based on the guess being less than or more than the number. Once the correct guess is entered, the program will print a success message along with the number of iterations the user needed to guess the number. Additionally, The user is to be informed of the range of possible values the program draws from, e.g: 1-20, or 1-100.

Explanation / Answer

solution:

package com.as.test;

import java.util.Random;

import java.util.Scanner;

public class RandomTest {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

Random random = new Random();

int number = random.nextInt(21);

int count = 0;// declare the count variable

System.out.println(number);

System.out.println("enter your guess 1-50");

int guess = scanner.nextInt();

do {

if (guess > number) {

System.out.println("the guess is too high,guess again");

} else

System.out.println("too low guess again");

guess = scanner.nextInt();

count++;

} while (guess != number);

System.out.println("correct guess " + number + " after " + count

+ " attempts");

}

}

output


enter your guess 1-50
17
too low guess again
25
the guess is too high,guess again
17
correct guess 17 after 2 attempts

//error

you didn,t declare the count variable .declare the variable before you use.That is the reason the error cannot find variable came.