《第13章 猜拳游戏》

import java.util.Scanner;


public class Game {
Scanner input=new Scanner(System.in);
boolean FLAG = true;
Computer computer = new Computer();
User user = new User();

public class Computer {

int cNum = 0;//电脑出拳的序号
String cChoose;//电脑出的拳
int score = 0;
String computerName=null;

/*
* 电脑出拳的方法
* */
public int startGame() {
cNum = (int) (Math.random() * (4 - 1)) + 1;// 电脑选择的出拳序号;
switch (cNum) {
case 1:

cChoose = "剪刀";
System.out.println("电脑出拳:" + cChoose);

break;
case 2:

cChoose = "石头";
System.out.println("电脑出拳:" + cChoose);

break;
case 3:

cChoose = "布";
System.out.println("电脑出拳:" + cChoose);

break;

default:
break;
}
return cNum;

}

}



public class User {

Scanner input = new Scanner(System.in);
int uNum = 0;// 选择的出拳序号
int score =0;//积分
String uChoose = null;// 出剪刀还是石头布
String userName=null;//用户名

/*
* 用户开始游戏方法
*/
public int startGame() {

System.out.println("请出拳:1、剪刀2、石头3、布(请输入相应数字)");
uNum = input.nextInt();

switch (uNum) {
case 1:

uChoose = "剪刀";
System.out.println("你出拳:" + uChoose);
System.out.println(uNum);

break;

case 2:

uChoose = "石头";
System.out.println("你出拳:" + uChoose);
System.out.println(uNum);

break;

case 3:

uChoose = "布";
System.out.println("你出拳:" + uChoose);
System.out.println(uNum);

break;

default:
break;
}
return uNum;

}

}



public void initial(){
System.out.println("-----------------欢迎进入游戏世界----------------");
System.out.println(" ******************");
System.out.println(" **猜拳,开始**");
System.out.println(" ******************");
System.out.println("出拳规则:1.剪刀 2.石头 3.布(输入相应的数字):");
}


public void startGame(){
int count=0; //对战次数
String juese=" ";
int JueSeNum=0;
System.out.println("请选择对方的角色(1. 孙悟空 2. 猪八戒 3. 沙僧");
JueSeNum=input.nextInt();
switch ( JueSeNum) {
case 1:
juese="孙悟空";
System.out.println("您选择了"+juese+"对战");

break;
case 2:
juese="猪八戒";
System.out.println("您选择了"+juese+"对战");

break;
case 3:
juese="沙僧";
System.out.println("您选择了"+juese+"对战");

break;

default:
break;
}
System.out.println("请输入你的昵称:");
user.userName=input.next();
System.out.println(user.userName+" "+"VS"+" "+juese);
System.out.println("要开始吗?(y/n)");
String isPlay = input.next();
if (isPlay.equals("y")) {


while (FLAG) {


int uNum = user.startGame();//接受用户开始游戏方法的出拳序号的返回值

int cNum = computer.startGame();

if (uNum==1&&cNum==3||
uNum==2&&cNum==1||
uNum==3&&cNum==2) {

System.out.println("结果说:^-^!你赢了!恭喜!!!");
user.score += 2;
count++;


} else if(uNum==cNum){

System.out.println("结果说:^-^!平局!加油!!!");
user.score += 1;
computer.score += 1;
count++;

}else {

System.out.println("结果说:-^-!你输了!真笨!!!");
computer.score += 2;
count++;
}

System.out.println("是否开启下一轮?(y/n)");
String is = input.next();

if (is.equals("y")) {

} else {

FLAG = false;

}

}



}else if (isPlay.equals("n")) {




} else {

System.out.println("你在想些什么啊,怎么选的啊……");

}

System.out.println("用户得分:"+user.score);
System.out.println("电脑得分:"+computer.score);
System.out.println("对战次数:"+count);
System.out.println("欢迎下次再来^_^!!");


}

}






public class aa {
public static void main(String[] args) {


Game game=new Game();
game.initial();
game.startGame();
}
}

原文地址:https://www.cnblogs.com/dqn-1996-02/p/5105916.html