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:
- Import java. util. collections;
- Public class game
- {
- Person per;
- Computer Com;
- Int count;
- Wrote input = new partition (system. In );
- // Constructor, initialization
- Public game ()
- {
- Per = new person ();
- COM = new computer ();
- Count = 0;
- }
- // Start the game
- Public void gamebegin ()
- {
- System. Out. println ("=========== guessing games ========= ");
- System. Out. println ("========================== ");
- System. Out. println ("game rules: 1. Fist 2. Scissors 3. Baggage ");
- System. Out. println ("Please select characters: 1. Xiaoqiang 2. xiaomao 3. Xiaoming ");
- Int number = input. nextint ();
- Switch (number)
- {
- Case 1:
- Per. Name = "Xiaoqiang ";
- System. Out. println ("You selected Xiaoqiang ");
- Break;
- Case 2:
- Per. Name = "Xiao Mao ";
- System. Out. println ("You selected Xiao Mao ");
- Break;
- Case 3:
- Per. Name = "James ";
- System. Out. println ("You selected James ");
- Break;
- }
- // The recorded boxing
- Int perno;
- Int comno;
- System. Out. println ("do you want to start the game? (Y/n )");
- String answer = input. Next ();
- While (answer. equalsignorecase ("Y "))
- {
- Perno = per. showfist ();
- Comno = com. showfist ();
- // Determine the result
- If (perno = 1 & comno = 1) | (perno = 2 & comno = 2) | (perno = 3 & comno = 3 ))
- {
- System. Out. println ("Draw ");
- // Record the total number of contests
- This. Count ++;
- }
- Else if (perno = 1 & comno = 2) | (perno = 2 & comno = 3) | (perno = 3 & comno = 1 ))
- {
- System. Out. println ("Player wins ");
- Per. Count ++;
- This. Count ++;
- }
- Else
- {
- System. Out. println ("Computer wins ");
- Com. Count ++;
- This. Count ++;
- }
- System. Out. println ("do you want to continue? (Y/n )");
- Answer = input. Next ();
- }
- // The summary result is displayed at the end.
- Showresult ();
- }
- // Display the result
- Public void showresult ()
- {
- System. Out. println ("Total number of contests:" + this. Count );
- System. Out. println ("Player wins:" + per. Count );
- System. Out. println ("Computer wins:" + COM. Count );
- }
- }
Next, write the test class
Code:
- Public class test
- {
- Public static void main (string ARGs [])
- {
- Game = new game ();
- Game. gamebegin ();
- }
- }
Run it and check it ··
Now, this simple Java quiz game has been completed ··