Skip to content

Instantly share code, notes, and snippets.

@KingSirLee
Created April 2, 2013 14:24
Show Gist options
  • Save KingSirLee/5292597 to your computer and use it in GitHub Desktop.
Save KingSirLee/5292597 to your computer and use it in GitHub Desktop.
猜数游戏。。
public class GuessNum{
public static void main(String[]args){
java.util.Scanner input=new java.util.Scanner(System.in);
java.util.Random random=new java.util.Random();
int num=random.nextInt(100);
System.out.println("系统已经给出一个随机数x(0<=x<100),开始猜数!");
int errorNum=0;
while(true){
System.out.println("请输入您猜的数字:");
int answer=input.nextInt();
if(answer>num){
System.out.println("您猜的值太大了!");
errorNum++;
}else if(answer<num){
System.out.println("您猜的值太小了!");
errorNum++;
}else{
System.out.println("恭喜您猜对了!");
break;
}
}
if(errorNum<=3){
System.out.println("您猜了"+errorNum+"次,天才呀!");
}else if(errorNum>3&&errorNum<=6){
System.out.println("您猜了"+errorNum+"次,您很聪明哟!");
}else if(errorNum>6&&errorNum<=10){
System.out.println("您猜了"+errorNum+"次,还可以,下次努力哟!");
}else{
System.out.println("您猜了"+errorNum+"次,我能说你是一个笨蛋么!!!");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment