Using java to realize guessing games

Time:2021-10-17

This example shares the specific code of Java guessing game for your reference. The specific contents are as follows

Achieve the following requirements

public class User {
 
 private String u_name;
 
 private int u_score;
 
 public User() {
  super();
 }
 
 public User(String name, int score) {
  super();
  this.u_name = name;
  this.u_score = score;
 }
 
 public String getName() {
  return u_name;
 }
 
 public void setName(String name) {
  this.u_name = name;
 }
 
 public int getScore() {
  return u_score;
 }
 
 public void setScore(int score) {
  this.u_score = score;
 }
 /**
  *Boxing method
  *@ param choice the selected number represents the fist (1: stone, 2: scissors, 3: cloth)
  *@ return STR returns the fist you selected
  */
 public String chuQuan(int choice){
  String str = "";
  switch (choice) {
  case 1:
   STR = "stone";
   break;
  case 2:
   STR = "scissors";
   break;
  case 3:
   STR = "cloth";
   break;
  default:
   System.out.println ("unknown error");
   break;
  }
  return str;
 }
 
}

public class Computer {
 
 private String c_name;
 
 private int c_score;
 
 public String getName() {
  return c_name;
 }
 
 public void setName(String name) {
  this.c_name = name;
 }
 
 public int getScore() {
  return c_score;
 }
 
 public void setScore(int score) {
  this.c_score = score;
 }
     /**
  *Boxing method
  *@ param choice the selected number represents the fist (1: stone, 2: scissors, 3: cloth)
  *@ return STR returns the fist you selected
  */
 public String chuQuan(int choice){
  String str = "";
  switch (choice) {
  case 1:
   STR = "stone";
   break;
  case 2:
   STR = "scissors";
   break;
  case 3:
   STR = "cloth";
   break;
  default:
   System.out.println ("unknown error");
   break;
  }
  return str;
 }
}

import java.util.Scanner;
 
public class Game {
 
 Scanner input = new Scanner(System.in);
 
 private User user;
 
 private Computer computer;
 
 private int count;
 
 private int c_score;
 
 private int u_score;
 
 //Initialization method
 public void init(){
  user = new User();
  computer = new Computer();
  System. Out. Println ("----------------- welcome to the game world -------------------------");
  System.out.println("\t   **************************");
  System. Out. Println ("\ t \ t * * guess, start * *);
  System.out.println("\t   **************************");
  System.out.println();
  System. Out. Println ("punching rules: 1. Stone, 2. Scissors, 3. Cloth");
  System. Out. Print ("please select the opposite role: (1: Cao Cao 2: Sun Quan 3: Liu Bei):");
  int key = input.nextInt();
  switch (key) {
  case 1:
   Computer.setname ("Cao Cao");
   break;
  case 2:
   Computer.setname ("Sun Quan");
   break;
  case 3:
   Computer.setname ("Liu Bei");
   break;
  default:
   System. Out. Println ("illegal input...);
   break;
  }
  System. Out. Print ("please enter your name:");
  user.setName(input.next());
  System.out.println(user.getName()+"  VS  "+computer.getName());
  begin();
 }
 
 //Whether to start the execution cycle until the end of input n
 public void begin(){
  System. Out. Print ("start (Y / N):");
//  boolean falg = true;
  String str = input.next();
  if(str.equals("y")){
   while(true){
    score();
    System. Out. Print ("start next round: (Y / N)");
    String str1 = input.next();
    count++;
    if(str1.equals("y")){
     
    }else{
//     falg = false;
     break;
    }
   }
  }
  show();
 }
 
 //People and machines punch and judge the outcome. The number of matches is calculated here, and the scores of both sides are scored
 public void score(){
  System. Out. Print ("please punch:");
  int choice1 = input.nextInt();
  String str1 = user.chuQuan(choice1);
  int choice2 = (int)(Math.random()*3+1);
  String str2 = computer.chuQuan(choice2);
  System. Out. Println ("you punch" + STR1);
  System. Out. Println (computer. Getname() + "punch" + STR2);
  if(choice1 == choice2){
   System. Out. Println ("result: draw");
   
  }else if(choice2-choice1==-1||choice2-choice1==2){
   System. Out. Println ("result:" + computer. Getname() + "win...);
   c_score++;
   computer.setScore(c_score);
  
  }else if(choice1-choice2==-1||choice1-choice2==2){
   System. Out. Println ("result: Congratulations, you win...);
   u_score++;
   user.setScore(u_score);
 
  }
 }
 
 //Show the results of the game and compare them to get the final result
 public void show(){
  System.out.println("--------------------------------");
  System.out.println(user.getName()+"  VS  "+computer.getName());
  System. Out. Println ("number of battles:" + count + "\ n \ n");
  System.out.println ("name \ T score");
  System.out.println(user.getName()+"\t"+user.getScore());
  System.out.println(computer.getName()+"\t"+computer.getScore()+"\n");
  if(user.getScore()>computer.getScore()){
   System. Out. Println ("result: Congratulations");
  }else if(user.getScore()<computer.getScore()){
   System. Out. Println ("result: keep going");
  }else{
   System. Out. Println ("result: draw");
  }
  System.out.println("--------------------------------");
 }
 
}

Test class


public class Test {
 
 public static void main(String[] args) {
  // TODO Auto-generated method stub
  Game game = new Game();
  game.init();
 }
 
}

In this way, the guessing game is realized.

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support developpaer.

Recommended Today

SQL exercise 20 – Modeling & Reporting

This blog is used to review and sort out the common topic modeling architecture, analysis oriented architecture and integration topic reports in data warehouse. I have uploaded these reports to GitHub. If you are interested, you can have a lookAddress:https://github.com/nino-laiqiu/TiTanI recorded a relatively complete development process in my hexo blog deployed on GitHub. You can […]