Man-Machine guessing (this is a recent summary) and man-machine guessing recently

Source: Internet
Author: User
Tags rounds

Man-Machine guessing (this is a recent summary) and man-machine guessing recently

Man-Machine guessing is a piece of original code. I just learned do-while, But I have limited knowledge, but I feel that this piece of code is a small peak of mine. Let's see it, it is excellent for new users to learn things, and they want to give some suggestions to the old birds. This code is boring. I have been writing it for a long time. I have a deep understanding of it. Later I wrote some interesting content in my code during my practice. As you can see below, this is an entertaining little trick. It wasn't long before I learned it. I felt that I could not learn it by myself. I want everyone to learn it together. As the saying goes: The three NOPs are the top of ZHUGE Liang, so today I am not just going to post my own code, more importantly, I would like to share my face with you and hope you can take care of me, cainiao. The following is the code:

1 import java. util. optional; 2 public class CaiQuan {3 public static void main (String [] args) {4 Inputs input = new inputs (System. in); 5 System. out. println ("-------- welcome to the guessing game --------- \ n"); 6 System. out. println ("Punch rules: 1. scissors 2. stone 3. cloth \ n "); 7 System. out. println ("****************************** \ n "); 8 System. out. println ("******** start with guessing ********** \ n"); 9 System. out. println ("****************************** \ n \ N "); 10 System. out. println ("Give yourself a mighty nickname \ n" before the game starts); 11 System. out. println ("Enter your nickname:"); 12 String username = input. next (); // ask the user to enter the nickname 13 String name = "1"; // give the name an initial value. The name is the computer name 14 do {15 System. out. println ("select a combat role (1. liu Bei 2. cao 3. galun): "); 16 boolean zhenjia = input. hasNextInt (); // determines whether the input value is 17 while (! Zhenjia) {// if it is not a number, enter 18 String zhen = input. next (); 19 System. out. println ("You entered:" + zhen + "! "); 20 System. out. println ("Please reselect the role (1. liu Bei 2. cao 3. galun): "); 21 zhenjia = input. hasNextInt (); // judge whether the input is a numerical value. If not, continue loop 22} 23 int num = input. nextInt (); // The retrieved number 24 while (num> 3 | num <0) {// checks whether the input value is 1-3. If it is not in the 25 System loop. out. println ("You cannot do it! "); 26 System. out. println ("Please reselect the role (1. liu Bei 2. cao 3. galun): "); 27 num = input. nextInt (); // enter a value. If it is a 1-3 hop out loop, if it is not a continue loop 28} 29 switch (num) {30 case 1: 31 name = "Liu Bei "; 32 break; 33 case 2: 34 name = "Cao"; 35 break; 36 case 3: 37 name = "galun"; 38 break; 39} 40} while (name. equals ("1"); // when the name is not 1, the loop 41 System exists. out. println ("you have selected" + name + ""); 42 String go; 43 do {44 System. out. println (username + "\ tVS \ t" + name); 45 // punch code 46 int I = 1; // I is the number of rounds for guessing. 47 int userScore = 0; // The user score is 48 int compScore = 0; // The computer score is 49 String come; // when the value of "come" is "y", continue the loop and write 50 do {51 System. out. println ("this is the" + I + "Wheel"); 52 System. out. println ("please punch: 1. scissors 2. stone 3. cloth "); 53 boolean zhenjia = input. hasNextInt (); // determines whether the input is a value 54 while (! Zhenjia) {// enter 55 String zhen = input if it is not a number. next (); // put the above value here, and re-enter 56 System. out. println ("You entered:" + zhen + "! "); 57 System. out. println ("Try again: 1. scissors 2. stone 3. cloth "); 58 zhenjia = input. hasNextInt (); // judge whether the input is a numerical value. If not, continue loop 59} 60 int num = input. nextInt (); // retrieve the above value 61 while (num> 3 | num <0) {// judge whether the input value is 1-3, if it is not in the loop 62 System. out. println ("You cannot do it! "); 63 System. out. println ("Try again: 1. scissors 2. stone 3. cloth "); 64 num = input. nextInt (); // enter a value. If it is a 1-3 hop out loop, if it is not a continue loop 65} 66 String user = ""; // store the user's punch 67 switch (num) {68 case 1: 69 System. out. println ("You punch: Scissors"); 70 user = "Scissors"; 71 break; 72 case 2: 73 System. out. println (": "); 74 user = ""; 75 break; 76 case 3: 77 System. out. println (": "); 78 user = ""; 79 break; 80} 81 String comp = ""; // store the guessing machine 82 int numc = (Int) (Math. random () * 10) % 3 + 1); // randomly generate 1-3 83 switch (numc) {84 case 1: 85 System. out. println (name + "blow: Scissors"); 86 comp = "Scissors"; 87 break; 88 case 2: 89 System. out. println (name + "Punch: Stone"); 90 comp = "Stone"; 91 break; 92 case 3: 93 System. out. println (name + "blow: cloth"); 94 comp = "cloth"; 95 break; 96} 97 if (user. equals (comp) {// determine if both sides are playing the same 98 System. out. println ("both sides:" + user + "this Council is a draw"); 99} else {100 if (user. equals ("Scissors ")){// 101 if (comp. equals ("Stone") {// 102 System. out. println (name + "win when the computer goes out of the rock! "); 103 compScore ++; 104} else {// 105 System. out. println (" You win! "); 106 userScore ++; 107} 108} else if (user. equals ("Stone") {109 if (comp. equals ("cloth") {110 System. out. println (name + "wins! "); 111 compScore ++; 112} else {113 System. out. println (" You win! "); 114 userScore ++; 115} 116} else {117 if (comp. equals (" Scissors ") {118 System. out. println (name +" win! "); 119 compScore ++; 120} else {121 System. out. println (" You win! "); 122 userScore ++; 123} 124} 125} 126 I ++; // The number of rounds increases by 1127 System. out. println ("continue" + I + "round y \ n end n"); 128 come = input. next (); // when the value of "come" is y, continue loop 129} while ("y ". equals (come); // judge whether the value of come is y. If yes, continue the loop. Otherwise, the System jumps out of the loop 130. out. println ("game played" + (I-1) + "Wheel"); 131 System. out. println ("Name \ t score"); 132 System. out. println (username + "\ t" + userScore); 133 System. out. println (name + "\ t" + compScore );
1 if (userScore = compScore) {// when the score is the same, this is the modified Code 2 System. out. println ("you and" + name + "); 3 System. out. println ("How can this be tolerated? It is a pleasure to beat him?"); 4 System. out. println ("Continue y/confirm n"); 5} else if (userScore> compScore ){
135 System. out. println ("You flushed" + name + "); 136 System. out. println ("\ t you tm handsome"); 137 System. out. println ("" + name + "\ n continue y/recognize n"); 138} else {139 System. out. println ("You have been" + name + "broken into pig heads"); 140 System. out. println ("Revenge y/recognize limit n"); 141} 142 go = input. next (); 143} while ("y ". equals (go); 144 System. out. println ("game over! "); 145} // mian146 147}

Run the following code:

Enter a nickname:

Next, let the user select a role to play the game. There are only three options: 1.2.3. I will first enter a 4 option to see the effect:

You see, when I did not enter 1.2.3, it prompts you that "the minister cannot do it" and asked you to re-choose:

I enter 1:

I chose Liu Bei and then made a blow:

 

Enter n to end the game:

There is a small problem here. I didn't take into account the same score of the two people,

This is the result of my revenge, and then input n to end the game:

The above changes the same score.

There are still many problems with the code, and I will continue to modify it later

Zookeeper ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Reprinted please indicate the source!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.