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

the answer for this question is posted on chegg twice but it is wrong. please us

ID: 3865188 • Letter: T

Question


the answer for this question is posted on chegg twice but it is wrong. please use file type reader to find the existing file. thanks.

Lo Game (with Dice) In this homewo rk, you will be writing a program, which repeatedly asks the user if the next roll will be higher or lower than the last dice rol You wil continually play until the user types quit on a line by remember how many times you were successf The game will and how many times you tried and report the ratio of success to tries after the user quits. In addition, the game will maintain persistence: remembering you and how many times you've played even after exiting (by maintaining a file). You will need to ask the user for their user name prior to playing and use that to record eir results in a file. If the user has played before, the number of tries and the number of tries is in a file named after the user with a txt extension If not, we start at rero for that user. Once the user quits, you'll need to save the new results into the correct file 10s Your dialog should be clear each time you ask for something what you are asking forandoutpu he result. Note: you should also inform the user what you consider correct and what will end the game for the user ining the hilla/quit (209) Logic providing the dice rolls (note: you need a dice rol o begin) 4. (303) Logic providing the persistence which includes: Determine if the file exists b opening the file, reading the number correct and the number of tries (each on their own line) c. Once finished, rewriting the file so that it contains current data 20 The logic providing the rest of the result (managing the play, keeping score calculation 6 Name your program: LOJava

Explanation / Answer

import java.util.Random;
import java.util.Scanner;
public class HiLo
{
   public static void main (String[] args)
   {
       final int MAX = 10;
       int answer, guess;
       int numberOfTries = 0 ;
       String again;

       Scanner Keyboard = new Scanner(System.in);

       do

       {
           System.out.print (" I'm thinking of a number between 0 and "
           + MAX + ". Guess what it is: ");
           guess = Keyboard.nextInt();
           Random generator = new Random();
           answer = generator.nextInt(MAX) +1;

           if (guess > 10)
           {
           System.out.println ("ERROR – Your guess is out of the range 0 to 10.");
           }
           if (guess < 0)
           {
       System.out.println ("ERROR – Your guess is out of the range 0 to 10.");
           }

           while (guess != answer )           {

           if (guess > answer )
           {
           System.out.println ("You guessed too high! Try again:");
           guess = Keyboard.nextInt();
           }
          
           if ( guess == answer)
{
numberOfTries += 1; // <--- This adds the final guess
System.out.println ("YOU WIN!");
System.out.println("It took you " + numberOfTries + " tries!") ;
System.out.println();
System.out.print( "Do you want to play again(Y/N)?");
}