Get the App
SLTechnology News&Howtos  ›  Development  › 

How to realize simple fist guessing Mini Game by java

Shulou Source: shulou.com Published: 2022-06-02 11:52:46 10月03日 Update

This article mainly explains "java how to achieve simple fist guessing Mini Game", interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Next let the editor to take you to learn "java how to achieve a simple fist guess Mini Game" bar!

User.java

Import java.util.Scanner;public class User {String name; int score; public int showFist () {System.out.println ("Please punch 1. Scissors\ T2. Stone\ T3. Cloth "); Scanner input=new Scanner (System.in); int choice=input.nextInt (); if (choice==1) {System.out.println (" you are out of scissors ");} else if (choice==2) {System.out.println (" you are out of rock ");} else if (choice==3) {System.out.println (" you are out of paper ") } else {System.out.println ("incorrect input!") ;} return choice;}}

Computer.java

Public class Computer {String name; int score; public int showFist () {int choice = (int) (Math.random () * 3) + 1; / generate the random number if (choice = = 1) {System.out.println (name + "out of the scissors");} else if (choice = = 2) {System.out.println (name + "out of the rock") } else if (choice = = 3) {System.out.println (name + "you are out of cloth");} else {System.out.println ("incorrectly typed!" );} return choice;}}

Game.java

Import java.util.Scanner;public class Game {User user; / / user Computer computer; / / computer int count; / / record the number of battles / / initialize: set your own name, opponent's name, score 0 public void init () {System.out.println ("Please enter your own name"); Scanner input = new Scanner (System.in); String name = input.next () User=new User (); / / object initializes user.name=name; user.score=0; System.out.println ("Please select your opponent:\ N1. Zhang San\ T2. Li Si\ T3. ); int choice = input.nextInt (); computer=new Computer (); / / object initialization computer.score=0; switch (choice) {case 1: computer.name= "Zhang San"; case 2: computer.name= "Li Si"; case 3: computer.name= "Wang Wu" Break; default: System.out.println ("incorrect input!") ;} System.out.println ("you choose to fight TA:" + computer.name);} public void start () {init (); Scanner input=new Scanner (System.in); String isContinue = null; do {int userFist = user.showFist (); / / people punch int chomputerFist = computer.showFist () / / computer punch calcResult (userFist, chomputerFist); System.out.println ("do you want to continue?" Y (continue) / other (end)); isContinue=input.next ();} while ("y" .equals (isContinue)); showResult (user,computer); / / Show the final result} / / calculate the result of each round public void calcResult (int userFist,int computerFist) {/ / "1. Scissors\ T2. Stone\ T3. Cloth "if ((userFist==1&&computerFist==3) | | (userFist==2&&computerFist==1) | | (userFist==3&&computerFist==2)) {System.out.println (" you win "); user.score++;} else if ((userFist==3&&computerFist==1) | | (userFist==1&&computerFist==2) | | (userFist==2&&computerFist==3)) {System.out.println (" you lost "); computer.score++ } else {System.out.println ("your tie");}} / / calculate the final result public void showResult (User user,Computer computer) {System.out.println (user.name + "\ t" + user.score); System.out.println (computer.name + "\ t" + computer.score) If (user.score > computer.score) {System.out.println ("Congratulations, final victory");} else if (user.score

Tags: Scissors stone result input Mini Game name content opponent object draw computer Zhang San Li Si Wang Wu learning choice practical deeper regret interest Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno OPPO Reno MySQL NVidia Microsoft Shulou Technology