Guess the number game v1.0 to achieve a custom range of game play, and in the game after the end of the next round, this is the other program does not take into account, I hope to help, and finally hope that someone is willing to help me solve the problem of code optimization, thank you.
/* Requirements:
* *. Complete a guess number game, the data range is customized by the player, the game starts after the player input values, give a narrow version of the guidance interval;
* *. After the end of the game, the player can choose whether to continue playing;
Ideas
* *. Creates a random number of the specified interval int, and the interval is specified by the player;
* *. For the player to guess the data, compared to give a judgment, big, small can give a guide interval, until guess right, record the number of guesses;
. After the game is over, ask the player whether to continue;
Steps
* *. Using scanner, the player creates the game range;
* *. The results are judged by using nested for in while, and a new guidance interval is given.
. After the game is over, prompt the player to enter the Yes string to continue the game;
*========= New ========= for optimization
* *. In the program, getting the random number part is duplicated, if there is a great God can write a getrandom to get the random number, and can continue to use min, max value is better;
* *. In a while loop, there should be a more concise way to determine if the input data is out of bounds.
*========= rookie begging for help =========
*/
1 ImportJava.util.*;2 Public classGuessgame {3 Public Static voidMain (string[] args)4 {5 //confirm the lower limit of guessing data6Scanner guessmin =NewScanner (system.in);7System.out.println ("Welcome to guessing the number game v1.0!\n Please enter the lower limit of the data you want to guess:");8 intMin =guessmin.nextint ();9 //confirm the upper limit of the quiz dataTenScanner Guessmax =NewScanner (system.in); OneSystem.out.println ("Please enter the upper limit of the data you want to guess:"); A intMax =guessmax.nextint (); - //Generates a random number of a specified interval, math.random () generates a random number of type double between [0,1]. - intGivennumber = (int) (Math.random () * (max-min+1)) +min; the //System.out.println ("The system has generated a random number:" +givennumber);//Uncomment when testing -SYSTEM.OUT.PRINTLN ("The system has generated a random number of [" + min + "," + Max + "] intervals to guess! "); - //count is introduced to calculate the number of inputs - intCount = 0; + while(true) - { + //player input of conjecture data AScanner sc =NewScanner (system.in); at Try - { -System.out.println ("In this given interval, [" + min + "," + Max + "]" + ", enter the data you suspect:"); -count++; - intGuessnumber =sc.nextint (); - //greater than judgment, will max-1 the purpose for more precise range in if(Guessnumber >givennumber) - { to //++++++++++++++ + //to be optimized, here is a judgment on when the input value exceeds the upper limit, avoiding misleading the player - if(Guessnumber >max) the {} * Else ${max = GuessNumber-1;}Panax Notoginseng //++++++++++++++ -System.out.println ("Too bad, you are in the first" + Count + "times to guess big, please continue:"); the } + //less than judgment, will min+1 the purpose for a more precise range A Else if(Guessnumber <givennumber) the { + //++++++++++++++ - //to be optimized, here is a judgment on when the input value is above the lower limit to avoid misleading the player $ if(Guessnumber <min) $ {} - Else -{min = guessnumber+1;} the //++++++++++++++ -System.out.println ("Too bad, you are in the first" + Count + "times to guess small, please continue:");Wuyi } the //equals judgment and asks whether to continue the game - Else Wu { -System.out.println ("Awesome, you've spent" + count + "times guessed this random number," + Givennumber + ".) "); About //do you want to continue playing $System.out.println ("\ n Enter Yes to continue the challenge, press any key to exit.) \ n "); -sc =NewScanner (system.in); -String str =sc.nextline (); - if("Yes". Equals (str)) A { + //++++++++++++++ the //To be optimized, rewrite the assigned random number, if you can write a getrandom to get the random number, and can continue to use min, max value is better. - //confirm the lower limit of guessing data $Scanner _guessmin =NewScanner (system.in); theSystem.out.println ("Welcome to guessing the number game v1.0! \ n Please enter the lower limit of the data you want to guess: "); theMin =_guessmin.nextint (); the //confirm the upper limit of the quiz data theScanner _guessmax =NewScanner (system.in); -System.out.println ("Please enter the upper limit of the data you want to guess:"); inMax =_guessmax.nextint (); the //Generates a random number of a specified interval, math.random () generates a random number of type double between [0,1]. theGivennumber = (int) (Math.random () * (max-min+1)) +min; About //System.out.println ("The system has generated a random number:" +givennumber);//Uncomment when testing theSYSTEM.OUT.PRINTLN ("The system has generated a random number for [" + min + "," + Max + "] interval, quiz bar! "); the //count is introduced to calculate the number of inputs theCount = 0; + //++++++++++++++ - } the ElseBayi { theSystem.out.println ("Welcome to the Guess number game v1.0, bye!")); the Break; - } - } the } the Catch(inputmismatchexception e) the { theSYSTEM.OUT.PRINTLN ("You entered the wrong data, please re-enter an integer.") "); - } the } the } the}
Java complete simple guess number game