• 人机猜拳


    import java.util.Scanner;

    public class Person {
    String name;
    int score;
    String punches;
    String exit;
    int count;
    public int showFist() {
    System.out.println("请出拳1.剪刀2.石头3.布");
    Scanner input = new Scanner(System.in);
    int punches = input.nextInt();
    switch (punches) {
    case 1:
    System.out.println("你出拳:剪刀");
    break;
    case 2:
    System.out.println("你出拳:石头");
    break;
    case 3:
    System.out.println("你出拳:布");
    break;
    default:
    System.out.println("您的输入有误,请重新输入");
    break;
    }
    return punches;

    }
    public int showComputer() {
    int x = 1 + (int) (Math.random() * 3);
    switch (x) {
    case 1:
    System.out.println(name+"出拳:剪刀");
    break;
    case 2:
    System.out.println(name+"出拳:石头");
    break;
    case 3:
    System.out.println(name+"出拳:布");
    break;
    }
    return x;
    }
    public void playGame() {
    System.out.println("----------------欢迎进入游戏世界----------------");
    System.out.println(" ***********************************");
    System.out.println(" ************ 猜拳,开始 *************");
    System.out.println(" ***********************************");
    System.out.println("出拳规则:1.剪刀2.石头3.布");
    Scanner input = new Scanner(System.in);
    String start;
    do {
    System.out.println("请选择对方角色:(1.江户川柯南2.逆回十六夜3.空白)");
    int role = input.nextInt();
    switch (role) {
    case 1:
    name = "江户川柯南";
    System.out.println("你选择了" + name + "对战");
    break;
    case 2:
    name = "逆回十六夜";
    System.out.println("你选择了" + name + "对战");
    break;
    case 3:
    name = "空白";
    System.out.println("你选择了" + name + "对战");
    break;
    default:
    System.out.println("输入错误!");
    break;
    }
    String exit = "n";
    System.out.print("要开始吗?(y/n)");
    start = input.next();
    int perFist;
    int comFist;
    while (start.equals("y")) {
    perFist = showFist();
    comFist = showComputer();
    if ((perFist==1&&comFist==1)||(perFist==2&&comFist==2)||(perFist==3&&comFist==3)) {
    System.out.println("结果:和局,真衰! ");
    }else if ((perFist==2&&comFist==1)||(perFist==3&&comFist==2)||(perFist==1&&comFist==3)) {
    System.out.println("结果说:^_^,你输了,真笨! ");
    score++;
    }else {
    System.out.println("结果:恭喜,你赢了! ");
    score++;
    }
    count++;
    System.out.print("是否进行下一轮(y/n)");
    start = input.next();
    }
    break;
    } while (!"n".equals(exit));
    }
    }

    public class personText {
    public static void main(String[] args) {
    Person person=new Person();
    person.playGame();
    }
    }

  • 相关阅读:
    #include<unistd.h>存在linux中,含有系统服务的函数
    Value Categories
    Main function
    Phases of translation
    Memory model
    Unqualified name lookup
    Qualified name lookup
    Name lookup
    Definitions
    Object lifetime
  • 原文地址:https://www.cnblogs.com/F017/p/9785653.html
Copyright © 2020-2023  润新知