猜随机数字的java代码,还能改进吗??

来源:互联网 发布:淘宝拍静物的相机 编辑:程序博客网 时间:2024/04/29 02:32
class Guessnumber {
 public static void main(String[]args) throws Exception {
  int guess = (int)(Math.random()*21);
  int ch = 0;
  int input = 0;
  int times = 0;
  boolean isEnd = false;
  while(true) {
   ch = System.in.read();
   switch(ch) {
    case'\r':
    break;
    case'\n':
    input/=10;
    if(input>guess) {
     System.out.println("Guess larger");
     times++;
    }
    if(input<guess) {
     System.out.println("Guess smaller");
     times++;
    }
    if(input==guess&&times<4) {
     System.out.println("cool,you win!");
     isEnd = true;
    }
    if(input!=guess&&times==4) {
     System.out.println("Sorry,you've guessed four times!you lose the game!");
     isEnd=true;
     System.out.println("the number is "+guess);
    }
    if(isEnd) {
     times = 0;
     guess = (int)(Math.random()*21);
     isEnd = false;
    }
    input = 0;
    break;
    default:
    input+=(ch-'0');
    input*=10;
    }
  }
 }
}