猜拳入口
public class Mora {
public static void main(String[] args) {
int Scoresum = 0;
for (int a = 1;true;a++){
Mora mora = new Mora();
System.out.println("第"+a+"局");
Scoresum = mora.scoreSum(Scoresum);
System.out.println(Scoresum);
}
}
public int scoreSum(int Scoresum){
Person per1 = new Person();
Robot rob1 = new Robot();
per1.sayToNum();
per1.getPersonNum();
int a = rob1.getRobotNum();
int b = per1.getPersonNum();
VS vs1 = new VS();
int c = vs1.vs(b,a);
Scoresum = c + Scoresum;
return Scoresum;
}
}
对比计分
//把人和机器猜拳比对计分
public class VS{
private int score = 0;
public int vs(int a,int b){
if(a == 1&b == 3){
score++;
}else if(a == 1&b == 2){
score--;
}
if(a == 2&b == 1){
score++;
}else if(a == 2&b == 3){
score--;
}
if(a == 3&b == 2){
score++;
}else if(a == 3&b == 1){
score--;
}
return score;
}
}
建立猜拳人的类
import java.util.Scanner;
//剪刀 = 1, 石头 = 2, 布 = 3;
public class Person{
//人猜拳对应的数字
private int personNum = 0;
public int getPersonNum() {
return personNum;
}
//人说的文字
private String personSay;
//文字转换为数字
public int sayToNum() {
System.out.println("剪刀石头步");
Scanner scan = new Scanner(System.in);
personSay = scan.next();
if (personSay.equals("剪刀")) {
personNum = 1;
}
else if (personSay.equals("石头")){
personNum = 2;
}
else if (personSay.equals("布")){
personNum = 3;
}
else {
System.out.println("你玩的是同一款游戏吗?");
sayToNum();
}
return personNum;
}
}
机器人类
import java.util.Random;
//机器人随机生成(剪刀、石头、布)
public class Robot{
Random random = new Random();
private int robotNum;
public int getRobotNum (){
robotNum = random.nextInt(3)+1;
switch(robotNum){
case 1:
System.out.println("我出剪刀");
break;
case 2:
System.out.println("我出石头");
break;
default:
System.out.println("我出布");
break;
}
return robotNum;
}
}