Home>

The specific code is as follows:

import java.util.inputmismatchexception;
import java.util.scanner;
</p>
<p>
public class main {
 public static void main (string [] args) {
 //generate a random number
 int number=(int) (math.random () * 100) + 1;
</p>
<p>
//join count
 int count=0;
</p>
<p>
//add the maximum value here,And minimum
 int max=100;
 int min=1;
</p>
<p>
while (true) {
 //keyboard input data
 scanner sc=new scanner (system.in);
 system.out.println ("Please enter the data i want to guess:(" + min + "~" + max + ")");
 try {
 count ++;
 int guessnumber=sc.nextint ();
 //judge
 if (guessnumber>number) {
 max=guessnumber;
 system.out.println ("You guessed it big");
 } else if (guessnumber<number) {
 min=guessnumber;
 system.out.println ("You guessed it too small");
 } else {
 system.out.println ("Congratulations, you spent" + count + "You guessed it");
 //ask if continue
 system.out.println ("Do i want to continue?(yes)");
 sc=new scanner (system.in);
 string str=sc.nextline ();
 if ("yes" .equals (str)) {
 //rewrite assignment random number
 number=(int) (math.random () * 100) + 1;
 count=0;
 max=100;
 min=1;
 } else {
 break;
 }
 }
 } catch (inputmismatchexception e) {
 system.out.println ("The data you entered is incorrect");
 }
 }
 }
}

The results are shown in the following figure:

  • Previous PHP merge the same elements in an array
  • Next Analysis of explode function usage in PHP
  • Trends