Get the App
SLTechnology News&Howtos  ›  Development  › 

How to realize simple fist guessing Mini Game by Java

Shulou Source: shulou.com Published: 2022-06-01 13:18:12 09月22日 Update

In this article, the editor introduces in detail "how to achieve simple fist guess Mini Game by Java" with detailed content, clear steps and proper handling of details. I hope this article "how to achieve simple boxing guess Mini Game by Java" can help you solve your doubts.

1 requirement

Guess the fist with the computer and record the score.

2 Computer.java source code (computer automatic random punch) public class Computer {public void Fingers (int index) {String [] fingers = {"rock", "scissors", "paper"}; System.out.println ("computer punch:" + fingers [index]);}} 3 Game.java source code (class for main functions) import java.util.Random;import java.util.Scanner Public class Game {public void initial () {/ / Page Scanner scanner = new Scanner (System.in); System.out.println ("- Welcome to the game interface -"); System.out.println () System.out.println ("* *"); System.out.println ("* guessing fist, go! * "); System.out.println (" * * "); System.out.println (" punch rule: 0. Stone 1. Scissors 2. ); / / enter the name System.out.print ("Please enter your name:"); String name = scanner.next (); System.out.println (name+ "VS Boss Battle"); / / determine whether to start System.out.print ("do you want to start?" (enter yes/no): "); String b = scanner.next (); int count=0; int usercount=0; int computercount=0; while (b.equals (" yes ")) {/ / count the number of fights count++; / / players punch System.out.print (" Please punch 0. Stone 1. Scissors 2. Cloth (enter the corresponding number): "); int a = scanner.nextInt (); String [] fingers = {" rock "," scissors "," paper "}; System.out.println (" you punch: "+ fingers [a]); / / computer random punch Random random = new Random (); int index = random.nextInt (3) Computer computer = new Computer (); computer.Fingers (index); / / decide whether to win or lose if ((a = = 0 & & index = = 1) | | (a = = 1 & & index = = 2) | | (a = = 2 & & index = = 0) {System.out.println ("Congratulations, you won!") ; usercount = usercount+1;} else if ((a = = 0 & & index = = 2) | (a = = 1 & & index = = 0) | | (a = = 2 & & index = = 1) {System.out.println ("I'm sorry, you lost!") ; computercount = computercount+1;} else {System.out.println ("result: tie!") ;} / / whether to proceed to the next round of System.out.print ("whether to proceed to the next round!" (enter yes/no); b = scanner.next ();} / / Summary System.out.println ("--Summary -"); System.out.println (name+ "VS Boss") System.out.println ("number of battles:" + count); System.out.println ("name score"); System.out.println ("Boss Wen" + computercount); System.out.println (name+ "" + usercount); String I = computercount > usercount? " It's a pity that you lost the game! " : computercount

Tags: Input scissors computer rock Mini Game name article source code result boss content draw number win game proper sorry regret score function Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Technology Xiaomi MariaDB Huawei MySQL