Humans and machines can do this, can't you? The Java console realizes the "guessing game"

The examples in this article share the specific code of the Java console to implement the guessing game for your reference. The specific content is as follows:
1. Goal

A man-machine guessing game is realized through the console. The user inputs (1. Scissors 2. Rock 3. Cloth), the machine randomly generates (1. Scissors 2. Rock 3. Cloth), the winner points, and passes after n rounds. The number of points determines the winner.

2. Source code
According to the requirements of the target, we can roughly know that the classes we need are:

①A user class User, used to obtain and store the user's punches;

②A robot-like robot, which is used to randomly generate the punches of the robot;

③A Game class is a class that stores the core code. The punches of the user and the robot are compared here, the results are recorded, and the results are finally output;

④ Finally, there is a Guess class that calls various methods.

Directly on the code:

User.java

import java.util.Scanner;

public class User{
    
    
  static Scanner input = new Scanner(System.in);
  int user = 0;//用户出拳
  int score = 0;//用户积分
  int num = 0;//对战局数

  public int setUser(){
    
    
      System.out.println("请输入:1.剪刀 2.石头 3.布");
      user = input.nextInt();
      if (user == 1){
    
    
        System.out.println("用户出拳:剪刀");
      }else if (user == 2){
    
    
        System.out.println("用户出拳:石头");
      }else if (user == 3){
    
    
        System.out.println("用户出拳:布");
      }
    return user;
  }
}

Robot.java

import java.util.Random;

public class Robot {
    
    
  int id = 0;//获得机器人的出手
  int score = 0;//机器人的积分

  //获得机器人的出拳
  public int setId(){
    
    
    Random random = new Random();
    int r = random.nextInt(3);
    id = r + 1;//获取机器人随机出拳
    if (id == 1){
    
    
      System.out.println("机器人出拳:剪刀");
    }else if (id == 2){
    
    
      System.out.println("机器人出拳:石头");
    }else if (id == 3){
    
    
      System.out.println("机器人出拳:布");
    }
    return id;
  }
}

Game.java

import java.util.Scanner;

public class Game {
    
    
  //对用户类和机器人类进行初始化
  User user = new User();
  Robot robot = new Robot();

  public void GameStart() {
    
    
    //初始化界面
    System.out.println("-------------欢迎进入猜拳游戏---------------");
    System.out.println();
    System.out.println("******************************************");
    System.out.println("——————出拳规则:1.剪刀  2.石头  3.布——————————");
    System.out.println("-------------》》》猜拳开始《《《------------");
    System.out.println("-------------------------------------------");
    Scanner input = new Scanner(System.in);

    //获得用户想要进行的对战次数
    System.out.println("请输入对战局数:");
    user.num = input.nextInt();
    System.out.println("游戏开始");
    
    int userFirst;//用户出拳
    int robotFirst;//机器人出拳
      for (int i = 0; i < user.num; i++) {
    
    
        //获取双方的出拳
        userFirst = user.setUser();
        robotFirst = robot.setId();
        //判断胜负
        if (userFirst == robotFirst) {
    
    
          System.out.println("结果:平局,不积分。");
        } else if ((userFirst == 2 && robotFirst == 1) || (userFirst == 3 && robotFirst == 2)) {
    
    
          System.out.println("结果:你赢了,加一分!");
          user.score++;
        } else {
    
    
          System.out.println("结果:你输了,机器人加一分。");
          robot.score++;
        }
      }
    //显示结果
    showResult();
    }

    //输出对战结果
  private void showResult() {
    
    
    //显示对战次数
    System.out.println("------------------------------");
    System.out.println("对战次数:" + user.num);
    //显示最终得分
    System.out.println("\n姓名\t得分");
    System.out.println("用户" + "\t" + user.score);
    System.out.println("机器人" + "\t" + robot.score + "\n");

    //显示对战结果
    int result = Result();
    if (result == 1) {
    
    
      System.out.println("结果:打成平手。");
    } else if (result == 2) {
    
    
      System.out.println("结果:恭喜你赢得对战!");//用户获胜
    } else {
    
    
      System.out.println("结果:你输了,不气馁,下次再来");//计算机赢
    }
    System.out.println("游戏结束");
    System.out.println("--------------------------");
  }

  //计算比赛结果
  private int Result() {
    
    
    if (user.score == robot.score) {
    
    
      return 1;
    } else if (user.score > robot.score) {
    
    
      return 2;
    } else {
    
    
      return 3;
    }
  }
}

Guess.java

public class Guess {
    
    
  public static void main(String[] args) {
    
    
    Game game = new Game();
    
    //调用输出比赛结果的方法
    game.GameStart();
  }
}

The latest high-frequency interview questions collected in 2021 (all organized into documents), there are a lot of dry goods, including mysql, netty, spring, thread, spring cloud, jvm, source code, algorithm and other detailed explanations. There are also detailed learning plans and interviews. Questions, etc., friends who need to obtain these content, please add Q Junyang: 547998459

The above is the whole content of this article. If you need more information, please contact me. I hope it will be helpful to everyone's study, and I hope you can support me.

Guess you like

Origin blog.csdn.net/p1830095583/article/details/114448170