猜数游戏

import java.util.Scanner;

public class GuessNum {

 public static void main(String[] args) {
  Scanner s = new Scanner(System.in);  //接受输入(猜数)
  int user = 0;       //用户输入的数(小)
  int user2 = 100;      //用户输入的数(大)
  int no = -1 ;       //存放输入的数
  
  /*生成随机数,给用户猜*/
  double d = Math.random();    
  int guess = (int)(d*100)/1;
  
  /*对比用户猜到数和生成的数*/
  for(;no!=guess;){
   System.out.println("please input a number in "+user+" to "+user2+"!");
   System.out.print("please input a number which you guess:");
   no = s.nextInt();
   if(no>guess){
    System.out.println("too big!"); 
    user2 = no;
   }
   if(no<guess){
    System.out.println("too small!");
    user = no;
   }
  }
  System.out.println("congratulation!");
  
 }

}

原文地址:https://www.cnblogs.com/HuangWj/p/4275076.html