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

Write a program that asks the user for their name and then generates a random nu

ID: 3596725 • Letter: W

Question

Write a program that asks the user for their name and then generates a random number (0 to 100)and asks the user to guess what the number is. If the user's guess is higher than the random number, the program should display "Too high, try again." If the user's guess is lower than the random number, the program should display "Too Low, try again." The program should use a loop that repeats until the user correctly guesses the random number. Enhance the program so it keeps track of the number of guesses that the user makes. When the user correctly guesses the random number, the program should display the number of guesses and write to a file ("scores.txt") the user's name and the number of guesses they madee If another user plays append the file with their name and the number of guesses they did.

Explanation / Answer

You have not specified the programming language. Given below is the code for the game in Java. It also creates a file scores.txt . If using eclipse, please refresh the project so that you can find the file inside the project. Please do rate the answer if it helped. Thank you.

To indent code in eclipse, select code using Ctrl+A and then press Ctrl+i


import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Random;
import java.util.Scanner;
public class NumberGuess {
public static void main(String[] args) {
Scanner keybd = new Scanner(System.in);
String name;
int guess, randNum;
//get user's name
System.out.print("Enter your name: ");
name = keybd.next();
Random random = new Random(System.currentTimeMillis());
randNum = random.nextInt(101); //generate a number from 0 - 100
int numOfGuesses = 0;
//System.out.println(randNum);
do
{
System.out.print("Enter your guess: ");
guess = keybd.nextInt();
if(guess < randNum)
System.out.println("Too low, try again.");
else if(guess > randNum)
System.out.println("Too high, try again.");
numOfGuesses++;
}while(guess != randNum);
System.out.println("You guessed it ! It is " + randNum );
System.out.println("Number of guesses : " + numOfGuesses);
try {
//open the file scores.txt in append mode
PrintWriter outFile = new PrintWriter(new FileWriter("scores.txt", true));
outFile.append(name + " " + numOfGuesses);
outFile.close();
} catch (IOException e) {
System.out.println(e.getMessage());
}
}
}


output
Enter your name: raji
Enter your guess: 50
Too high, try again.
Enter your guess: 35
Too low, try again.
Enter your guess: 40
Too low, try again.
Enter your guess: 45
Too low, try again.
Enter your guess: 49
Too high, try again.
Enter your guess: 48
Too high, try again.
Enter your guess: 47
You guessed it ! It is 47
Number of guesses : 7

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote