展开全部

package table;

import java.util.Scanner;

public class Game21

{

public static void main ( String[] args )

{

System.out.println ("游戏规则:");

System.out.println ("开始游戏后屏幕上将出现一个32313133353236313431303231363533e59b9ee7ad9431333337386631随机数字");

System.out.println ("按a可以随机增加一个1-10范围内的一个数字");

System.out.println ("按s则揭晓你和电脑对决的结果!");

System.out.println ("如果你的数字大于21点,则游戏直接结束!");

int rand = (int) ( Math.random () * 10 + 1 );

int sum1 = rand, sum2 = 0, which = 0; // 0电脑,1我

System.out.println ("系统先给出1个数字: " + rand);

System.out.print ("轮到你了: ");

Scanner scanner = new Scanner (System.in);

while (true)

{

String input = "";

if (which == 0)

{

input = scanner.next ();

}

if (which == 0 && "a".equals (input))

{

which = 1;

int r = (int) ( Math.random () * 10 + 1 );

sum2 += r;

System.out.println ("您抽到的数字是: " + r);

if (sum2 > 21)

{

System.out.println ("你的点数为 " + sum2 + " , 超过了最大限制 21 点,游戏提前结束!");

break;

}

System.out.print ("该电脑了: a\n");

}

else if (which == 1)

{

which = 0;

int r = (int) ( Math.random () * 10 + 1 );

sum1 += r;

System.out.println ("电脑抽到的数字是: " + r);

if (sum1 > 21)

{

System.out.println ("电脑点数为 " + sum1 + " , 超过了最大限制 21 点,游戏提前结束!");

break;

}

System.out.print ("轮到你了: ");

}

else if ("s".equals (input))

{

System.out.println ("电脑共有:  " + sum1 + " 点");

System.out.println ("你共有: " + sum2 + " 点");

if (sum1 > sum2)

{

System.out.println ("YOU LOSE !!!");

}

else if (sum1 

{

System.out.println ("YOU WIN !!!");

}

else

{

System.out.println ("DRAW GAME !!!");

}

System.out.print ("轮到你了: ");

which = 0;

}

}

scanner.close ();

}

}

Logo

DAMO开发者矩阵,由阿里巴巴达摩院和中国互联网协会联合发起,致力于探讨最前沿的技术趋势与应用成果,搭建高质量的交流与分享平台,推动技术创新与产业应用链接,围绕“人工智能与新型计算”构建开放共享的开发者生态。

更多推荐