Java completes simple guess number game v2.0 and java guess number v2.0

Source: Internet
Author: User

Java completes simple guess number game v2.0 and java guess number v2.0

Game v2.0 optimizes the code for obtaining random numbers and inputting data beyond the boundary value, and adds exception handling to provide reliable guidance when gamers enter incorrect data errors, hope to help new people like me,

Finally, I hope some experts will help me solve the code optimization problem. Thank you.

/* Requirement:
* 1. When a gamer inputs incorrect data types, the gamer must re-enter the data;
* 2. Optimize the code of the input integer that exceeds the specified range;
* 3. optimized the code for obtaining random numbers;
* Idea:
* 1. To create a function, you must enter an integer;
* 2. A clearer prompt is displayed after the boundary is exceeded, which correctly guides the player input;
* 3. Introduce a while loop with labels. After the inner loop ends, jump to the specified number to start again without obtaining a random number;
* Steps:
* 1. Create the getInteger function and introduce try-catch. If the input is not int, re-enter the function and use continue until the player inputs an integer;
* 2. Compare the gamer input data with the upper and lower limit of the range. If the upper limit or lower limit is exceeded, a prompt is displayed;
* 3. A correct error message is displayed when the maximum range for gamer input is lower than the lower limit, and the user is required to re-enter the upper limit;
* 4. Add a number outer in the while loop. After the player inputs yes, the outer loop continues. In other cases, the outer loop ends, that is, the game ends.

* ++
* Disadvantages
* Too many nested loops, which need to be further optimized.
* ++
*/

1 import java. util. *; 2 public class GuessGame 3 {4 public static void main (String [] args) 5 {6 outer: while (true) 7 {8 // check the lower limit of the data for the quiz 9 rows guessMin = new round (System. in); 10 System. out. println ("Welcome to guess digital game v2.0! \ N enter the lower limit of your quiz data: "); 11 int min = getInteger (); 12 // confirm the upper limit of the quiz data 13 while (true) 14 {15 Export guessMax = new partition (System. in); 16 System. out. println ("Enter the maximum data you want to guess:"); 17 int max = getInteger (); 18 if (max <min) 19 {20 System. out. println ("the maximum number of data you entered is lower than the upper limit. Please enter the data again. "); 21 continue; 22} 23 // generates a random number in the specified range. Math. random () generates a random number of the double type between [0, 1. 24 int givenNumber = (int) (Math. random () * (max-min + 1) + min; 25 // System. out. println ("random number generated by the System:" + givenNumber); // You Can uncomment 26 System during the test. out. println ("the system has generated a random number in the range [" + min + "," + max + "]. Come and guess! "); 27 // introduces count, used to calculate the number of inputs 28 int count = 0; 29 while (true) 30 {31 // The Player inputs the conjecture data 32 bytes SC = new bytes (System. in); 33 try 34 {35 System. out. println ("in this given interval, [" + min + "," + max + "]" + ", enter the data you suspect :"); 36 count ++; 37 int guessNumber = SC. nextInt (); 38 // greater than the judgment, the max-1 is intended to be more precise within the range of 39 if (guessNumber> givenNumber) 40 {41 // This is a judgment on the input value exceeding the upper limit, avoiding misleading players 42 if (guessNumber> max) 43 {44 System. out. pri Ntln ("it is a pity that the data you entered in the" + count + "is" + guessNumber + ", which exceeds the upper limit of the range. "); 45} 46 else 47 {48 max = guessNumber-1; 49 System. out. println ("it's a pity that you guessed it again in the" + count + ", please continue:"); 50} 51} 52 // less than to judge, the purpose of min + 1 is to provide a more accurate range of 53 else if (guessNumber <givenNumber) 54 {55 // This is a judgment for the input value exceeding the upper limit, avoid misleading players 56 if (guessNumber <min) 57 {58 System. out. println ("it is a pity that the data you input in the" + count + "is" + guessNumber + ", which exceeds the lower limit of the interval. "); 59} 60 else 61 {62 min = guessNumber + 1; 63 System. out. println ("it's a pity that you guessed it again at the" + count + "time. Please continue:"); 64} 65} 66 // equals to the judgment, ask if you want to continue the game 67 else 68 {69 System. out. println ("awesome, it took you" + count + "to guess the random number," + givenNumber + ". "); 70 // whether to continue the game 71 System. out. println (" \ n enter yes to continue the challenge, press any key to exit. \ N "); 72 SC = new partition (System. in); 73 String str = SC. nextLine (); 74 if ("yes ". equals (str) 75 {76 continue outer; 77} 78 else 79 {80 System. out. println ("welcome to the guessing digital game v2.0, goodbye! \ N "); 81 break outer; 82} 83} 84} 85 catch (InputMismatchException e) 86 {87 System. out. println ("the data you entered is incorrect. Please enter an integer in the interval again. \ N "); 88} 89} 90} 91} 92} 93 public static int getInteger () // analyze the obtained data, which is an integer to continue, if it is not an integer, you must re-enter 94 {95 while (true) 96 {97 bytes in = new bytes (System. in); 98 int getNum = 0; 99 String str = in. nextLine (); 100 boolean B = true; 101 try102 {103 getNum = Integer. parseInt (str); // If the input is of the int type, convert the value to an integer and assign it to getNum104} 105 catch (Exception e) // if it is not an integer, Set B to false106 {107 B = false; 108} 109 if (B) // if B is false, re-enter 11. 0 {111 System. out. println ("the integer you entered is:" + getNum); 112} 113 else114 {115 System. out. println ("incorrect input. Please enter an integer again. "); 116 continue; 117} 118 return getNum; // function returns the integer 119} 120} 121}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.