第二题,猜数字游戏,用Java中的循环咋做

如题所述

import java.util.Random;
import java.util.Scanner;
public class Guess {
public static void main(String args[])
{
int target = new Random().nextInt(100);
boolean succ = false;
int count = 0;
Scanner sc = new Scanner(System.in);
System.out.println("系统产生了下个随机数(0-100),请问是多少?");
while(!succ)
{
 count++;  
 int i = sc.nextInt();
 if(i<target)  System.out.println("你猜的数字太小了!再猜!");
 else if(i>target)   System.out.println("你猜的数字太大了!再猜!");
 else
 {
 System.out.println("恭喜你,猜对了!");
 System.out.printf("你一共猜了%d次\n", count);
 if(count == 1) System.out.println("你太聪明了,一次就猜对");
 else if(count>=2 && count <=6)  System.out.println("你很聪明了"); 
 else  System.out.println("你笨,猜这么多次");
 succ = true;
 } 
}
}
}

温馨提示:答案为网友推荐,仅供参考
第1个回答  2014-12-14
if(用户输入的数字>生成的)
else if
else if
else out.println("你太聪明了");追问

具体代码

具体

第2个回答  2014-12-14
以下运行就可以了

public class test {

public static void main(String[] args) {

Scanner s=new Scanner(System.in);
Random r=new Random();
int computer=r.nextInt(100);
int times=1;
System.out.println(computer);
System.out.println("系统已产生一个随机数字(0~99),请问是多少?");
while(true){

int you=Integer.parseInt(s.next());

if(you>computer){
System.out.println("你猜的数字太大了!再猜!");
times++;
}else if(you<computer){
System.out.println("你猜的数字太小了!再猜!");
times++;
}else {
if(times==1){
System.out.println("你太厉害了,一次就猜对。");
}else if(times>1 && times<7){
System.out.println("你很聪明!");
}else{
System.out.println("你笨死了,猜这么多次!");
}
break;
}
}

}
}
相似回答