看看吧!你可能也不会
在一个山洞里住满了妖怪,他们看守着一件稀世珍宝,但是任何一个人都没有勇气进入这个山洞,只有五个以上的人同时进入山洞才能打败妖怪得到宝物。使用JAVA程序模拟这个过程,必须让每一个人都知道山洞中一共有多少人了。
提示:定义一个方法,名为勇气,每个人都调用这个勇气方法,当勇气值大于等于5时,输出可以进入山洞,勇气值小于5时,输出人数不够!
2011-09-23 10:22
2011-09-23 20:02
程序代码:
static int s=0;
static boolean flag=false;
public static void courage(){
++s;
System.out.println("现在人数是"+s);
if(s>=5){
System.out.println("为了月亮!灭了他们~~");
flag=true;
}
}
public static void main(String[] args) throws InterruptedException {
while(true){
sleep(5000);
courage();
if(flag){
System.out.println("杀呀!");
break;
}
}
}
2011-09-24 12:52
2011-09-25 00:15
新手路过
2011-09-25 15:28
2011-09-27 22:37
2011-09-28 15:46
2011-11-02 18:35
2011-11-02 23:07