头图

This is actually a fishing guide.

This article is mainly to learn how to use the Java online compiler to make a number guessing game in the browser. It really only needs a network + a browser. Friends who want to learn Java programming can calm down and study hard, and friends who just want to fish directly like, collect and take away the code, open the source code of this Java project on the browser and copy it to the personal project column, you can use the Java online compiler to start Learning mo and yu: https://2fe02ea528-share.lightly.teamcode.com

After all, who doesn't want to play a little game while studying and working?

Install the editor. unnecessary.
Install the compiler. unnecessary.
Install environment variables. unnecessary.
Install dependencies. unnecessary.

Just create a new project in Lightly's Java online compiler ( https://lightly.teamcode.com/ ), and you can start typing code to make our number-guessing game.

图片

Before starting to write the main program, we need to import some libraries that will be used:

 import javax.swing.*;
import java.awt.*;

First, we need to create two subroutines in the main program of public class main: public static void main(String[] args) and public static String determineGuess(int userAnswer, int computerNumber, int count). The former is the main body of our small game program, while the latter is the program for judging the size and right or wrong of numbers.

Here is a brief description of the functions of the number guessing game:
Generate a random number and ask the player to enter a number to determine whether the number is greater than, less than or equal to the previously generated random number to record the number of player input

In the program body of the Java online compiler, we first set variables such as random numbers, player input, and input times:

 int computerNumber = (int)(Math.random() * 100 + 1);
int userAnswer = 0;
int count = 1;

Next, we use a while loop to make the program repeat until the player answers correctly. We use JOptionPane to draw the GUI, and the small game that runs after that will be displayed in the pop-up window. Of course, friends who don't want GUI can also try to run the mini game directly in the output bar, which may be more in line with the spirit of fishing.

The while loop code in the Java online compiler should look like this:

图片

Among them, the UI interface text size can be adjusted according to your preferences. You can leave it out, too small text might be bad for your eyes, but the boss might not be able to see you playing a game from behind.

The last important part, we only need to finish the determineGuess part, and the game can run normally. There are basically if, else if and else codes. You can also add a few more layers of fun content according to your own preferences.

 public static String determineGuess(int userAnswer, int computerNumber, int count) {
    
    if (userAnswer <= 0 || userAnswer > 100) {
        return "Your guess is invalid.";
    }

    else if (userAnswer == computerNumber) {
        return "Correct!\nTotal Guesses: " + count;
    }
        
    ...
    
    else {
        return "Your guess is incorrect.\nAttempt: " + count;
    }

}

After the Java online compiler clicks the run button on the upper right, the effect of the mini-game is roughly as follows:

The Lightly Java online compiler is developed by TeamCode . TeamCode focuses on the field of cloud-native collaborative development, simplifies the development process, and helps developers and enterprises collaborate efficiently .


TeamCode
23 声望14 粉丝

TeamCode - 云端协作开发平台,重构开发流程,提供基于云端开发的一站式工具和服务,持续自动生成开发环境、构建和部署项目,帮助开发者云端协作开发。