Home > Java > javaTutorial > How to use Java to implement a simple guessing number game

How to use Java to implement a simple guessing number game

WBOY
Release: 2023-05-19 16:01:24
forward
2020 people have browsed it

Implementation Idea

The computer randomly generates integers from 1 to 100. When the user guesses the number given by the computer and enters it, if the entered number is the same as the number given by the computer, "Congratulations, you guessed it correctly" will be output. When the input number is lower than the number generated by the computer, the output value is too small. When the input number is higher than the number generated by the computer, the output value is too large.

You can use a for loop to set the number of guesses in the game. , increase the playability of the code

Code implementation

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

public class User {
    public static void main(String[] args) {

        {
            Random ran = new Random();//实例化随机对象
            System.out.println("欢迎进入猜数字游戏,祝你玩的愉快");
            int b = ran.nextInt(100) + 1;//随机生成一个整数作为猜的答案
            int i;

            for (i = 0; i < 10; i++) {//给游戏设定一个次数,最多输入10次
                System.out.println("请输入一个整数:");

                Scanner a = new Scanner(System.in);//实例化一个输入流对象
                if (a.hasNextInt()) {//判断输入的数是否为int类型

                    var c = a.nextInt();//将输入的值赋给c


                    if (c < b) {//判断c是否小于答案,如果是就输出下面这段话
                        System.out.printf("太小了,还剩%2d次机会", 9 - i);
                        System.out.println();
                    } else if (c > b) {//判断c是否大于答案,如果是就输出下面这段话
                        System.out.printf("太大了,还剩%2d次机会", 9 - i);
                        System.out.println();
                    } else {//否则就是等于答案
                        System.out.println("恭喜你,猜对了");
                        System.out.println("游戏结束,获胜");
                        break;
                    }
                } else {//如果输入的不是int类型,运行下面这段代码
                    if ("不想玩了".equals(a.next())) {//判断输入的是否为不想玩了这几个字,如果是就执行下面这句话
                        System.out.println("结束游戏,不玩了");
                        break;
                    } else {//否则就输出下面这段话
                        System.out.println("输入错误");
                        i--;//i-1 此次输入不算次数
                    }

                }
            }
            if (i == 10) {//判断输入次数是否为10次,如果为十次就输出下面这段话
                System.out.println("游戏结束,失败");
            }
        }
    }

}
Copy after login

Welcome to the guessing number game, I wish you a happy play
Please enter an integer:
50
Too small, there are 9 chances left
Please enter an integer:
60
Too big, there are 8 chances left
Please enter an integer:
55
Too Big, there are 7 chances left
Please enter an integer:
53
Guessed correctly
The game is over, win

The above is the detailed content of How to use Java to implement a simple guessing number game. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template