Java makes a simple guessing game

foreword

        I learned Java with my tutor. After learning loops and judgment statements today, I will play a small game of guessing fists, which is rock-paper-scissors.

1. Game architecture process

        First of all, let's design the rules and gameplay of the game. The rules of the game are also very simple: rock scissors paper, rock eats scissors, scissors eats paper, cloth wraps rock. After the program runs, the player has to choose to punch, and then the program automatically punches for comparison, the winner will get 1 point, the first to get 5 points wins, and the player can quit the game at any time.

1.1 Introduction to Loop Structure

        First of all, the knowledge points we use in this small game are: loops and judgments. First, let’s learn about java loop statements and their usage in detail.

       1. for loop

        The structure of the for loop is very simple, and the grammatical format is [for (initial value; loop condition; expression)] as shown in the figure:

         The three expressions in the brackets are separated by semicolons, and can also be omitted, and written outside or inside the for structure. If there are none, it will become an endless loop. The for loop is generally used to do a program with a certain number of times, such as inputting a specified number of numbers, finding the maximum value and outputting it.

        

         Two, while loop and do while loop

        The while loop structure is also very simple, the grammatical format is [while (Boolean expression)], when the expression inside the brackets is true, the loop body will be executed all the time, and the do while loop is almost exactly the same as the while loop, but do while is first Execute the loop body and then judge the conditions, as shown in the figure:

         The while loop and do while loop are suitable for programs that do not know the number of times to be executed, but the loop can be interrupted by setting conditions, and the while and do while loops can be converted to each other.

         Three, jump out of the loop body statement

        We have known three kinds of loop statements before, and basically control whether the loop body continues through the expressions in the brackets, but in actual design, in many cases, we don't need the program to run completely. For example, if I want to find the smallest multiple of 27 within 100, I can use the for loop to traverse from 27 and divide by 27 in turn. The first one that can divide 27 is the number I am looking for. At this moment, my needs have been completed, but the loop is still It will not stop until it continues to traverse to 100, which will cause a waste of computer resources. So Java provides two statements to jump out of the loop: break and continue .

        break : Jump out and interrupt the current loop body, generally used with conditional expressions and multi-layer loops.

        continue: Jump out of the current loop and enter the next loop.

Here are two example statements:

 

2. Complete code example of the mini game

import java.util.Random;
import java.util.Scanner;

public class MethodBlogs {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        System.out.println("\"石头剪刀布,电脑已就绪!游戏规则如下:和电脑猜拳\n" + " 1【石头】;2【剪刀】;3【布】\n");
        int comIntegral, plaIntegral;
        plaIntegral = comIntegral = 0;

        while (plaIntegral<5 | plaIntegral<5) {
            Random r = new Random();
            int comPuter = (int) (Math.random() * 3 + 1);
            System.out.println("请输入数字猜拳");
            int player = scan.nextInt();
            if (player==886){
                System.out.println("下次再见咯~");
                break;
            }

            if ((player > 0) & (player < 4)) {
                System.out.println("你的选择为:" + player);
                if (comPuter != player) {
                    if ((player - comPuter == -1) | (player - comPuter == 2)) {
                        plaIntegral += 1;
                        System.out.println("不错不错,竟然真的赢了!本次游戏,电脑出" + comPuter + "你出" + player);
                        System.out.println("目前战况为: 电脑:" + comIntegral + " 分" + ",玩家 " + plaIntegral + " 分");
                        System.out.println("退出游戏请输入886,输入其他数字继续");
                        System.out.println("----------------------------------------\n");
                    } else {
                        comIntegral += 1;
                        System.out.println("还好我技高一筹,你败了!本次游戏,电脑出" + comPuter + "你出" + player);
                        System.out.println("目前战况为: 电脑:" + comIntegral + " 分" + ",玩家 " + plaIntegral + " 分");
                        System.out.println();
                        System.out.println("退出游戏请输入886,输入其他数字继续");
                        System.out.println("----------------------------------------\n");

                    }

                }else if (comPuter == player) {
                    System.out.println("真是太有默契了,电脑出 " + comPuter + "你出" + player);
                    System.out.println("目前战况为: 电脑:" + comIntegral + " 分" + ",玩家 " + plaIntegral + " 分");
                    System.out.println("退出游戏请输入886,输入其他数字继续");
                    System.out.println("----------------------------------------\n");
                }
            }
            else {
                System.out.println("你输入的数字不合法,重新输入吧~");
                System.out.println();
                System.out.println("----------------------------------------\n");
            }

        }

        }

    }

The result of the operation is as follows:

You will understand after watching it a few times 

summary

        In addition, the Java loop body also has a foreach loop, which we will learn later. at last:

        The road to Java is long, although the road is long and difficult, but the journey will come!

Guess you like

Origin blog.csdn.net/qq_51294997/article/details/130777655