猜拳小游戏

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了猜拳小游戏相关的知识,希望对你有一定的参考价值。

今天我们来编写一款猜拳小游戏

分析:要求键入剪刀,石头,布,用数字分别代表剪刀,石头,布。?

java中有一个Math.random();可以生成【0,1)之间的随机数,所以说这里用1,2,3代表剪刀,石头,布。 而电脑随机生成其中一个数字与用户输入的数字比较,当输入错误的时候会提示输入错误,请重新输入。

?每次猜拳有三种可能性,如果电脑赢,电脑积分+10,人的积分不变;

如果人赢,人积分+10,电脑积分不变;

如果打成平手,积分不变。

需要分别定义两个变量接收积分的变化。

 

最后要将这两个变量的?值做比较。

?

?import java.util.Scanner;

public class Problem8 {

public static void main(String[] args) {

Scanner scan = new Scanner(System.in);

System.out.println("欢迎来到猜拳小游戏");

System.out.println("======================================");

System.out.print("请输入游戏回合数:");

int num = scan.nextInt();

int scoreHuman = 0;

int scoreComputer = 0;

int i = 1;

for (; i <= num; i++) {

// System.out.println("======================================");

// System.out.println("第" + i + "回合,你的积分:" + scoreHuman + ",电脑的积分:"

// + scoreComputer);

//

// System.out.println("======================================");

// System.out.print("出拳(1:剪刀,2:石头,3:布):");

humanFightWithComputer(i, scoreHuman, scoreComputer);// 代替上部分代码功能

while (num > 0) {

int outFist = scan.nextInt();

int computerOutFist = (int) (Math.random() * 3 + 1);

if (outFist == 1 || outFist == 2 || outFist == 3) {

if (computerOutFist == 1 && outFist == 1) {

System.out.println("你:剪刀");

System.out.println("电脑:剪刀");

System.out.println("平均,积分不变");

scoreHuman += 0;

scoreComputer += 0;

break;

} else if (computerOutFist == 2 && outFist == 1) {

System.out.println("你:剪刀");

System.out.println("电脑:石头");

System.out.println("电脑赢,积分+10");

scoreHuman += 0;

scoreComputer += 10;

break;

} else if (computerOutFist == 3 && outFist == 1) {

System.out.println("你:剪刀");

System.out.println("电脑:布");

System.out.println("你赢,积分+10");

scoreHuman += 10;

scoreComputer += 0;

break;

} else if (computerOutFist == 1 && outFist == 2) {

System.out.println("你:石头");

System.out.println("电脑:剪刀");

System.out.println("你赢,积分+10");

scoreHuman += 10;

scoreComputer += 0;

break;

} else if (computerOutFist == 2 && outFist == 2) {

System.out.println("你:石头");

System.out.println("电脑:石头");

System.out.println("平局,积分不变");

scoreHuman += 0;

scoreComputer += 0;

break;

} else if (computerOutFist == 3 && outFist == 2) {

System.out.println("你:石头");

System.out.println("电脑:布");

System.out.println("电脑赢,积分+10");

scoreHuman += 0;

scoreComputer += 10;

break;

} else if (computerOutFist == 1 && outFist == 3) {

System.out.println("你:布");

System.out.println("电脑:剪刀");

System.out.println("电脑赢,积分+10");

scoreHuman += 0;

scoreComputer += 10;

break;

} else if (computerOutFist == 2 && outFist == 3) {

System.out.println("你:布");

System.out.println("电脑:石头");

System.out.println("你赢,积分+10");

scoreHuman += 10;

scoreComputer += 0;

break;

} else if (computerOutFist == 3 && outFist == 3) {

System.out.println("你:布");

System.out.println("电脑:布");

System.out.println("平局,积分不变");

scoreHuman += 0;

scoreComputer += 0;

break;

}

break;

} else {

System.out.println("出拳错误,请重新出拳");

}

}

// if(scoreHuman>scoreComputer){

// System.out.println("======================================");

// System.out.println("大获全胜!你的积分:"+scoreHuman+",电脑积分:"+scoreComputer);

// System.out.println("======================================");

//

// }else if(scoreHuman==scoreComputer){

// System.out.println("======================================");

// System.out.println("棋逢对手!你的积分:"+scoreHuman+",电脑积分:"+scoreComputer);

// System.out.println("======================================");

// }else{

// System.out.println("======================================");

// System.out.println("不幸败北!你的积分:"+scoreHuman+",电脑积分:"+scoreComputer);

// System.out.println("======================================");

// }

}

whoWin(scoreHuman, scoreComputer);//代替上部分代码功能

}

public static void humanFightWithComputer(int i, int scoreHuman,

int scoreComputer) {

System.out.println("======================================");

System.out.println("第" + i + "回合,你的积分:" + scoreHuman + ",电脑的积分:"

+ scoreComputer);

System.out.println("======================================");

System.out.print("出拳(1:剪刀,2:石头,3:布):");

}

public static void whoWin(int scoreHuman, int scoreComputer) {

if (scoreHuman > scoreComputer) {

System.out.println("======================================");

System.out.println("大获全胜!你的积分:" + scoreHuman + ",电脑积分:"

+ scoreComputer);

System.out.println("======================================");

} else if (scoreHuman == scoreComputer) {

System.out.println("======================================");

System.out.println("棋逢对手!你的积分:" + scoreHuman + ",电脑积分:"

+ scoreComputer);

System.out.println("======================================");

} else {

System.out.println("======================================");

System.out.println("不幸败北!你的积分:" + scoreHuman + ",电脑积分:"

+ scoreComputer);

System.out.println("======================================");

}

}

}

以上是关于猜拳小游戏的主要内容,如果未能解决你的问题,请参考以下文章

JS小游戏----猜拳小游戏

猜拳游戏

猜拳小游戏

Python猜拳小游戏

python实现猜数字和猜拳小游戏

猜拳游戏