U sing your JSFiddle account you are going to create a guessing game, only it wi
ID: 664805 • Letter: U
Question
Using your JSFiddle account you are going to create a guessing game, only it will be the computer doing the guessing. Here is how it works - the computer will ask you for a number between 1 and 1000, it will check first to make sure your input is within the bounds.
Once you enter the number it will guess the number and do a comparison with the number you entered. It will output the results of the guess and continue to do this until it gets the correct answer. This is what the output of the program will look like (if I enter 329)
Guessed 500 - too high.
Guessed 250 - too low.
Guessed 375 - too high.
Guessed 313 - too low.
Guessed 344 - too high.
Guessed 329 - Got It!
It took me 6 Tries.
You can probably figure out how my algorithm works. You will want to create an algorithm that is efficient (lowest possible O).
Explanation / Answer
// The source code in Java tested OK and produced expected output
import java.util.Random;
import java.util.Scanner;
public class HelloWorld{
//public class GuessGame {
public static void main(String []args){
Random guessRand = new Random();
int numGuessedByComp = guessRand.nextInt(1000);
int ipNum; // number entered by user input
int numOfAttempts = 0;
Scanner input = new Scanner(System.in);
System.out.println(" Please Enter a number for the Guess Game ");
ipNum = input.nextInt();
System.out.println(" You have entered "+ ipNum);
numGuessedByComp = guessRand.nextInt(1000);
System.out.println(" The computer have guessed "+ numGuessedByComp);
while (numGuessedByComp != ipNum){
numOfAttempts++;
if (numGuessedByComp < ipNum){
System.out.println("Computer have guessed " + numGuessedByComp + " - " + "too low");
}
else if (numGuessedByComp > ipNum){
System.out.println("Computer have guessed " + numGuessedByComp + " - " + "too high");
}
numGuessedByComp = guessRand.nextInt(1000);
} // end while (numGuessedByComp != ipNum)
if (numGuessedByComp == ipNum){
System.out.println("Computer have guessed " + numGuessedByComp + " - " + "Got it");
System.out.println("It took the computer " + numOfAttempts + " Tries to guess the number entered by the user");
}
}
}
The above is an out put with a sample Run of Input number of 975
It took 1338 tries by the computer to guess it correctly - after testing in the above compiler the game is ported to JSFiddle When I posted the screen shot the Chegg.com said answer can not be longer than 65,000 characters
Will rerun many times untill the computer guesses with lesser tries so that the image will be smaller to fit 65,000 limit
// Output Screen Shot:
Got an output where the computer have guessed it with in 31 tries - but still when I pasted that screen the same problem of 65,000 characters limit - hence I had to type the out put I got from the compiler below:
Please Enter a number for the Guess Game 329
You have entered 329
Computer have guessed 919 - too high
Computer have guessed 293 - too low
Computer have guessed 43 - too low
Computer have guessed 244 - too low
Computer have guessed 2 - too low
Computer have guessed 302 - too low
Computer have guessed 677 - too high
etc up to 31 tries:
Computer have guessed 329 - Got it
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.