Java simple guessing game (III)

Source: Internet
Author: User

Connect (2)

Users and computer humans, we have already completed the compilation. The rest is the game class. To put it bluntly, the main role of the game class is to control the game process, which is equivalentThe handle of the game machine.

Read the code.

Code:
  1. Import java. util. collections;
  2. Public class game
  3. {
  4. Person per;
  5. Computer Com;
  6. Int count;
  7. Wrote input = new partition (system. In );
  8. // Constructor, initialization
  9. Public game ()
  10. {
  11. Per = new person ();
  12. COM = new computer ();
  13. Count = 0;
  14. }
  15. // Start the game
  16. Public void gamebegin ()
  17. {
  18. System. Out. println ("=========== guessing games ========= ");
  19. System. Out. println ("========================== ");
  20. System. Out. println ("game rules: 1. Fist 2. Scissors 3. Baggage ");
  21. System. Out. println ("Please select characters: 1. Xiaoqiang 2. xiaomao 3. Xiaoming ");
  22. Int number = input. nextint ();
  23. Switch (number)
  24. {
  25. Case 1:
  26. Per. Name = "Xiaoqiang ";
  27. System. Out. println ("You selected Xiaoqiang ");
  28. Break;
  29. Case 2:
  30. Per. Name = "Xiao Mao ";
  31. System. Out. println ("You selected Xiao Mao ");
  32. Break;
  33. Case 3:
  34. Per. Name = "James ";
  35. System. Out. println ("You selected James ");
  36. Break;
  37. }
  38. // The recorded boxing
  39. Int perno;
  40. Int comno;
  41. System. Out. println ("do you want to start the game? (Y/n )");
  42. String answer = input. Next ();
  43. While (answer. equalsignorecase ("Y "))
  44. {
  45. Perno = per. showfist ();
  46. Comno = com. showfist ();
  47. // Determine the result
  48. If (perno = 1 & comno = 1) | (perno = 2 & comno = 2) | (perno = 3 & comno = 3 ))
  49. {
  50. System. Out. println ("Draw ");
  51. // Record the total number of contests
  52. This. Count ++;
  53. }
  54. Else if (perno = 1 & comno = 2) | (perno = 2 & comno = 3) | (perno = 3 & comno = 1 ))
  55. {
  56. System. Out. println ("Player wins ");
  57. Per. Count ++;
  58. This. Count ++;
  59. }
  60. Else
  61. {
  62. System. Out. println ("Computer wins ");
  63. Com. Count ++;
  64. This. Count ++;
  65. }
  66. System. Out. println ("do you want to continue? (Y/n )");
  67. Answer = input. Next ();
  68. }
  69. // The summary result is displayed at the end.
  70. Showresult ();
  71. }
  72. // Display the result
  73. Public void showresult ()
  74. {
  75. System. Out. println ("Total number of contests:" + this. Count );
  76. System. Out. println ("Player wins:" + per. Count );
  77. System. Out. println ("Computer wins:" + COM. Count );
  78. }
  79. }

Next, write the test class

Code:
  1. Public class test
  2. {
  3. Public static void main (string ARGs [])
  4. {
  5. Game = new game ();
  6. Game. gamebegin ();
  7. }
  8. }

Run it and check it ··

Now, this simple Java quiz game has been completed ··

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.