Question: JAVA programming Could you slove this problems from my code! thank you~:) 1) random between 0 and 1000 (not 100) 2) the close option is
JAVA programming
Could you slove this problems from my code! thank you~:)
1) random between 0 and 1000 (not 100)
2) the "close" option is annoying
package numberguess;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Random;
import java.util.Scanner;
/**
*
*
*/
//Class definition
public class NumberGuess
{
//Main method
public static void main(String st[])throws IOException
{
//Random class Object created
Random rn = new Random();
//2D array to store game number and 10 guess
int myNo[][] = new int[5][10];
//count for number of guess and gameNo for game number
int count, gameNo = -1;
//Scanner classes object created to accept data
Scanner sc = new Scanner(System.in);
//Loops till user choice is n
do
{
//Increases the game number by 1
gameNo++;
//Generates a random number between 1 and 100
System.out.println("_____________________________________");
System.out.println(" WELCOME TO NUMBER GUESS");
System.out.println("_____________________________________");
int randomNo = (int)(Math.random() * 100) + 1;
System.out.println("<"+"Number " + randomNo+">");
System.out.println("");
//Loops 10 times
for(count = 0; count < 10; count++)
{
//Asks the user to enter a number
System.out.println("Guess Your Number: ");
//stores the number in myNo
myNo[gameNo][count] = sc.nextInt();
//Checks if both the numbers are equal
if(randomNo == myNo[gameNo][count])
{
//Displays success
System.out.println("Correct Guess ! ");
//Write the data to file
writeFile(myNo, gameNo, count);
//Comes out of inner for loop
break;
}//End of if
//Checks if the difference between entered number and generated random number is 10 then display Too Low
else if((randomNo - myNo[gameNo][count]) > 10 )
{
System.out.println("Too Low");
}//End of else if
//Checks if the difference between entered number and generated random number is -10 then display Too High
else if((randomNo - myNo[gameNo][count]) < -10 )
{
System.out.println("Too High");
}//End of else if
//close to the number
else
System.out.println("Close to the number");
}//End of for loop
//Checks if count is 10 then display the random number generated
if(10 == count)
System.out.println("Exceed 10 counts! Correct number is " + randomNo);
//Asks the user choice to play game or not
System.out.println("Would you like to play another one? (Y / N)");
//Accepts user choice
char choice = sc.next().charAt(0);
//If choice is N or n break
if(choice == 'N' || choice == 'n')
break;
}while(true);//End of loop
//Calls the method to display statistic
System.out.println("");
System.out.println("
printStatistic(myNo, gameNo, count);
}//End of main
//Method to write data
public static void writeFile (int[][]score, int gameNo, int count) throws IOException
{
//Creates a bufferedWriter object to write onto the file
//true for append mode
BufferedWriter outputWriter = new BufferedWriter(new FileWriter("Statistic.txt", true));
//Writes Game number to file
outputWriter.write("Game - " + Integer.toString(gameNo+1));
//For next line
outputWriter.newLine();
//Loops till count
for (int i = 0; i <= count; i++)
{
//Writes the guess numbers entered by the user
outputWriter.write(Integer.toString(score[gameNo][i]));
//For next line
outputWriter.newLine();
}//End of for loop
//Cleans the stream
outputWriter.flush();
//Closes the file
outputWriter.close();
}//End of method
//Method ot print statistic
public static void printStatistic(int [][]myNo, int gameNo, int count)
{
//To count number of attempts made by the user
int num;
//Loops till number of games played
for(int c = 0; c <= gameNo ; c++)
{
//Initializes the number of attempts for each game to zero
num = 0;
//Loops till length of the attempts made in each game
for(int d = 0; d < myNo[c].length; d++)
//Checks if number of attempts stored in myNo is not zero
if(myNo[c][d] != 0)
//Increase the counter by 1
num++;
//Displays the information
System.out.println("Game " + (c + 1) + " took " + num);
}//End of for loop
}//End of method
}//End of class
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
