Skip to content

Instantly share code, notes, and snippets.

@wdfx100
Created December 6, 2012 03:56
Show Gist options
  • Save wdfx100/4221657 to your computer and use it in GitHub Desktop.
Save wdfx100/4221657 to your computer and use it in GitHub Desktop.
猜数字
input=new java.util.Scanner(System.in);
random=new java.util.Random();
//取随机数
int num=random.nextInt(100);
System.out.println(num);
boolean flag=true;
int count=0;
while(flag){
System.out.println("请输入你的数字:");
int i=input.nextInt();
if(i>num){
System.out.println("你猜的数字太大了!");
count++;
}else if(i<num){
System.out.println("你猜的数字太小了!");
count++;
}else if(i==num){
System.out.println("恭喜你猜对了!");
System.out.println("你一共猜了"+count+"次!");
flag=false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment