猜拳游戏

import java.util.Scanner;
···
public class Computer {
private String name;// 名字
private int integral;// 积分
private int count;// 胜利次数
private String chuquan;// 出的拳

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public int getIntegral() {
    return integral;
}

public void setIntegral(int integral) {
    this.integral = integral;
}

public int getCount() {
    return count;
}

public void setCount(int count) {
    this.count = count;
}

public String getChuquan() {
    return chuquan;
}

public void setChuquan(String chuquan) {
    this.chuquan = chuquan;
}
// 获取名字
public void giveName() {
    Scanner scanner = new Scanner(System.in);
    boolean flag = true;// 循环条件
    System.out.print("请选择对方角色(1.刘备 2.孙权 3.曹操):");
    int choose = scanner.nextInt();
    do {
        if ( choose == 1 ) {
            setName("刘备");
            flag = false;
        }else if ( choose == 2 ) {
            setName("孙权");
            flag = false;
        }else if ( choose == 3 ) {
            setName("曹操");
            flag = false;
        }else {
            System.out.println("输入的数字不合法,请重新输入");
            flag = true;
        }
    }while ( flag );

}
// 出拳
public void Mora() {
    int num = (int)(Math.random()*3)+1;// num随机数随机1,2,3其一
    if ( num == 1 ) {
        setChuquan("剪刀");
    }else if ( num == 2 ) {
        setChuquan("石头");
    }else if ( num == 3 ) {
        setChuquan("布");
    }
}

}

import java.util.Scanner;

···
public class User {
private String name;// 名字
private int integral;// 积分
private int count;// 游戏次数
private String chuquan;// 出的拳

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public int getIntegral() {
    return integral;
}

public void setIntegral(int integral) {
    this.integral = integral;
}

public int getCount() {
    return count;
}

public void setCount(int count) {
    this.count = count;
}

public String getChuquan() {
    return chuquan;
}

public void setChuquan(String chuquan) {
    this.chuquan = chuquan;
}
// 获取你的名字
public void giveName() {
    Scanner scanner = new Scanner(System.in);
    System.out.println("请输入你的名字:");
    String name = scanner.next();
    setName(name);

}
// 出拳
public void userMora() {
    boolean flag = true;
    Scanner scanner = new Scanner(System.in);
    int num;
    do {
        System.out.println("请出拳:1.剪刀 2.石头 3.布(请输入对应数字)");
        num = scanner.nextInt();
        if ( num == 1 ) {
            setChuquan("剪刀");
            flag = false;
        }else if ( num == 2 ) {
            setChuquan("石头");
            flag = false;
        }else if ( num == 3 ) {
            setChuquan("布");
            flag = false;
        }else {
            System.out.println("输入错误,请重新输入对应数字");
            flag = true;
        }
    }while (flag);

}

}

···
public class Game {
User u = new User();
Computer c = new Computer();
// 获取名字
public void start(){
u.giveName();
c.giveName();
System.out.println(u.getName()+" VS "+c.getName());
}
// 判断出拳结果
public void showFist() {

    u.userMora();
    c.Mora();
    System.out.println(u.getName()+ "出拳" + u.getChuquan());
    System.out.println(c.getName() +"出拳:" + c.getChuquan());

    if ( u.getChuquan().equals("剪刀")&& c.getChuquan().equals("布") ||
            u.getChuquan().equals("石头")&& c.getChuquan().equals("剪刀")||
            u.getChuquan().equals("布")&& c.getChuquan().equals("石头")) {
        u.setCount(u.getCount()+1);// 对战次数
        u.setIntegral(u.getIntegral()+1);// 积分
        System.out.println("恭喜你,你赢了");
    }else if ( u.getChuquan().equals("剪刀")&& c.getChuquan().equals("石头") ||
            u.getChuquan().equals("石头")&& c.getChuquan().equals("布")||
            u.getChuquan().equals("布")&& c.getChuquan().equals("剪刀")) {
        u.setCount(u.getCount()+1);// 对战次数
        c.setIntegral(c.getIntegral()+1);// 积分
        System.out.println("很抱歉,输掉了本局游戏");
    }else if (u.getChuquan().equals(c.getChuquan())){
        u.setCount(u.getCount()+1);// 对战次数
        System.out.println("平局");
    }

}
// 打印结果并结束
public void showResult() {
    System.out.println(u.getName() + " VS " + c.getName());
    System.out.println("对战次数:" + u.getCount());
    System.out.println();
    System.out.println("姓名 \t 得分");
    System.out.println(u.getName() + " \t " + u.getIntegral());
    System.out.println(c.getName() + " \t " + c.getIntegral());
    System.out.println("游戏结束");
}

}

import java.util.Scanner;

···
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("**********************");
System.out.println("***** 猜拳,开始 *****");
System.out.println("**********************");

    System.out.println();
    System.out.println("出拳规则:1.剪刀 2.石头 3.布");
    Game game = new Game();
    game.start();


    System.out.println("要开始吗?(y/n)");
    String start = scanner.next();
    while ( "y".equalsIgnoreCase(start) ) {
        game.showFist();
        System.out.println("是否开始下一局(y/n)");
        start = scanner.next();
    }
    game.showResult();
}

}

你可能感兴趣的:(猜拳游戏)