石头剪刀布小游戏
问题描述:玩家与电脑的小游戏。
import java.util.Random;
import java.util.Scanner;
public class GuessGame {
public static void main(String[] args) {
int player;
String[] arr = {"石头","剪刀","布"};
Scanner sr = new Scanner(System.in);
System.out.print("请输入:");
player = sr.nextInt();
while(true) {
if(player>3 || player <=0) {
System.out.println("输入的数字不对,请重新输入:");
player = sr.nextInt();
}
else break;
}
Random rd = new Random();
int computerSave = (int)(Math.random()*3+1);
System.out.println("电脑出的是:"+arr[computerSave-1]+"("+computerSave+")");
System.out.println("玩家出的是:"+arr[player-1]+"("+player+")");
if((computerSave == 1 && player == 2) || (computerSave == 2 && player == 3) || (computerSave == 3 && player == 1)) {
System.out.println("恭喜电脑获胜!");
}
if((computerSave == 1 && player == 3) || (computerSave == 2 && player == 1) || (computerSave == 3 && player == 2)) {
System.out.println("恭喜玩家获胜!");
}
if((computerSave == 1 && player == 1) || (computerSave == 2 && player == 2) || (computerSave == 3 && player == 3)) {
System.out.println("平手!");
}
}
}
|