没有合适的资源?快使用搜索试试~ 我知道了~
首页用java编写的火柴游戏
资源详情
资源评论
资源推荐

班级:计科 1071 姓名:何钊杰 学号:200711621111
实验名称:火柴游戏
实验内容:
用 Math.random()函数产生一堆火柴(20-50 根之间)然后人与计算机轮流抽火柴,每方每
次最多抽取 3 根(计算机抽取的数目可用随机数产生),谁抽到最后一根火柴为胜者。
实验要求:
屏幕上应提示总的火柴数是多少,剩余火柴数是多少,计算机每次抽的火柴数。即次抽取
的状态显示出来,并显示胜者是谁。
实验代码:
import java.util.*;
public class monthshow {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int num=(int)((50-20)*Math.random()+20);
System.out.println("随机产生了"+num+"根火柴");
int a=0,b=0;
int step=0;
while(num>=0)
{
System.out.println("请抽取 1~3 根火柴");
Scanner sana=new Scanner(System.in);
a=sana.nextInt();
b=(int)((3-1)*Math.random()+1);
if(a>3||a<=0)
{
System.out.println("input error");
}
else{
num=num-a;
step++;
if (step==1&&num<=3&&num>0)
{
b=num;
System.out.println("电脑抽了"+b+"根火柴");
System.out.println("your lost");break;
}
if (num<=0)
安全验证
文档复制为VIP权益,开通VIP直接复制

评论0