Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Java to realize man-machine fist guessing Mini Game

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article introduces the relevant knowledge of "how to use Java to achieve man-machine fist guessing Mini Game". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Stage 1: experiment-analyze the business and create user classes

1. Analyze the business, abstract the characteristics and behavior of the class 2. Create a user class

Stage 2: experiment-- create a computer class

1. Create the computer class Computer. Realize computer punching 2. Generate a random number of 1-3 to simulate the punching result of the computer.

Stage 3: experiment-- create a game class and choose to fight against your opponent.

1. Create a game class Game2. Write the initialization method of the game class initial () 3. Write the start game method of the game class startGame ()

Stage 4: experiment-- realizing a game against each other

Call the punching method showFist () of the user class and the computer class respectively, accept the return value and compare it, and give the result.

Stage 5: experiment-- achieve a round-robin battle and accumulate scores

Achieve a round robin and add up the winner's score

Stage 6: experiment-showing the results of the match

After the end of the game, show the results of the game and write the showResult () method, compare the scores of the two, and give the results of the game.

Stage 7: experiment-- perfecting the startGame of games ()

Enter and save the user name, and display the respective scores of both sides at the end of the game.

Game.java

Import java.util.Scanner;public class Game {Scanner sc = new Scanner (System.in); User User;// user player Computer Computer;// computer player int count;// score / / initialization method of game class public void initial () {/ / create user object User= new User (); / create computer object Computer = new Computer (); / / initialize the number of battles to 0 count = 0 } / / method to start the game public void startGame () {System.out.println ("- Welcome to the game world--\ n\ n"); System.out.println ("\ t\ t *") System.out.println ("\ t\ tguess *, start * *"); System.out.println ("\ t\ t *\ n\ n"); System.out.println ("punch rule: 1. Stone 2. Scissors 3. Cloth "); System.out.print (" Please choose the other role (1: Liu Bei 2: sun Quan 3: Cao Cao) "); int choose = sc.nextInt (); / / receive the user's chosen role switch (choose) {case 1: Computer.computerName =" Liu Bei "; / / assign the value to the computer name System.out.println (" you chose "+ Computer.computerName+" fight "); break; case 2: Computer.computerName =" Sun Quan " System.out.println ("you chose" + Computer.computerName+ "fight"); break; case 3: Computer.computerName = "Cao Cao"; System.out.println ("you chose" + Computer.computerName+ "to fight"); break; default: System.out.println ("typed incorrectly!") ; break;} System.out.println ("Please enter your name:"); User.userName = sc.next (); System.out.println ("do you want to start (yAsia)"); String con = sc.next (); if (con.equals ("n")) {/ / prompt to exit System.out.println ("system exit!") if you don't start typing "n". ;} int perFist;// user punch int comFist;// computer punch while (con.equals ("y")) {/ / punch perFist = User.showFirst (); / / call the user class method comFist = Computer.chuQuan () / / call the method of computer class / / decide if ((perFist = = 1 & & comFist = = 1) | | (perFist = = 2 & & comFist = = 2) | | (perFist = = 3 & & comFist = = 3) {System.out.println ("result: draw, bad!") / / draw} else if ((perFist = = 1 & & comFist = = 3) | | (perFist = = 2 & & comFist = = 1) | | (perFist = = 3 & & comFist = = 2) {System.out.println ("You lost!"); / / the user loses System.out.println ("result: you are KO"); Computer.score++;// records the score won by the computer} else {System.out.println ("You win!") / / the user won System.out.println ("result: congratulations, you won!") ; User.score++;// records the score won by the user} the number of count++;// battles is added by one System.out.println ("whether to enter the next round (YPAPO)"); con = sc.next ();} / / displays the final result showResult () if it ends. } / / display the final output public void showResult () {System.out.println ("- -"); System.out.println (Computer.computerName+ "\ tVS\ t" + User.userName); System.out.println ("number of battles:" + count) System.out.println ("name" + "\ t" + "points"); System.out.println (User.userName+ "\ t" + User.score); System.out.println (Computer.computerName+ "\ t" + Computer.score); if (User.score > Computer.score) {/ / if the user's score is greater than the computer's score, the user wins the System.out.println (User.userName+ "wins!") ;} else {System.out.println (Computer.computerName+ "won!") ;} System.out.println ("- -");}

User.java

Import java.util.Scanner;public class User {String userName;// username int score;// integral public int showFirst () {System.out.print ("Please punch: 1. Scissors 2. Stone 3. Cloth (enter the corresponding number): "); Scanner sc = new Scanner (System.in); int choice = sc.nextInt (); switch (choice) {case 1: System.out.println (" you punch: stone "); break; case 2: System.out.println (" you punch: scissors "); break; case 3: System.out.println (" you punch: cloth "); break Default: System.out.println ("input error!"); break;} return choice;}}

Computer.java

Import java.util.Scanner;public class Computer {String computerName;// computer name int score;// computer integral public int chuQuan () {int random = (int) (Math.random () * 3) + 1; switch (random) {case 1: System.out.println (computerName+ "punch: rock"); break; case 2: System.out.println (computerName+ "punch: scissors"); break; case 3: System.out.println (computerName+ "punch: cloth"); break Default: System.out.println ("input error, please re-enter:"); break;} return random;}}

Main.java

Public class Main {public static void main (String [] args) {Game g = new Game (); g.initial (); / call the initialization method g.startGame (); / / call the method to start the game}} "how to use Java to achieve human-computer fist guessing Mini Game". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report