ChatGPT uses Java to write guessing games

Table of contents

The code written using ChatGPT is as follows

        existing problems

Improved Java code


        I believe that many students who learn programming have heard of the guessing game. I believe there is this topic in the after-school homework of the learning cycle. Just now when ChatGPT is on fire, I also tried to ask it to write this code. Unfortunately, there are bugs in its code, and it cannot realize the function of guessing numbers well.

The code written using ChatGPT is as follows

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

public class GuessNumberGame {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        Random random = new Random();
        int numberToGuess = random.nextInt(100) + 1;
        int numberOfTries = 0;
        boolean win = false;

        System.out.println("欢迎来到猜数字游戏!");

        while (!win) {
            System.out.print("请输入一个 1 到 100 之间的整数:");
            int guess = scanner.nextInt();
            numberOfTries++;

            if (guess == numberToGuess) {
                System.out.println("恭喜你,猜对了!");
                System.out.println("你猜了 " + numberOfTries + " 次。");
                win = true;
            } else if (guess < numberToGuess) {
                System.out.println("猜的数字太小了,请再试一次。");
                System.out.println("范围是 " + (guess + 1) + " 到 100。");
            } else {
                System.out.println("猜的数字太大了,请再试一次。");
                System.out.println("范围是 1 到 " + (guess - 1) + "。");
            }
        }

        scanner.close();
    }
}

这个程序首先生成一个 1 到 100 之间的随机整数,然后提示用户输入一个整数并进行比较,直到用户猜对为止。程序还会记录用户猜的次数并在猜对后输出。如果用户猜错了,程序会提示用户当前猜测的数字不在正确的范围内,并提示正确的范围。

        existing problems

        After debugging, it is found that the range of his prompts is not so accurate, and there is no corresponding handling mechanism for exceptions.

Improved Java code

package mySourceCode;

import java.util.Scanner;

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

        guessNumbers();
    }

    /**
     * 猜数游戏
     */
    public static void guessNumbers() {
        Scanner scan = new Scanner(System.in);
        System.out.println("数字范围:[1,100]");
        System.out.println("请输入你的猜测:");
        int max = 0;
        int min = 101;
        try {
            int inPut = scan.nextInt();
            int ints = (int) (Math.random() * 100 + 1);// [1,100]

            int counts = 0;
            while (inPut != ints) {
                counts++;
                if (inPut > ints) {
                    System.out.println("\t大了,已尝试:" + counts + " 次");
                    if (max != 0) {
                        max = Math.min(max, inPut);
                    } else {
                        max = inPut;
                    }
                    if (min == 101) {
                        System.out.println("\t范围:(0 , " + max + ")");
                    } else {
                        System.out.println("\t范围:(" + min + " , " + max + ")");
                    }
                } else {
                    System.out.println("\t小了,已尝试:" + counts + " 次");
                    if (min != 101) {
                        min = Math.max(min, inPut);
                    } else {
                        min = inPut;
                    }
                    if (max == 0) {
                        System.out.println("\t范围:(" + min + " , 100)");
                    } else {
                        System.out.println("\t范围:(" + min + " , " + max + ")");
                    }
                }
                inPut = scan.nextInt();// 实现持续输入,直到正确为止
            }
            System.out.println("\t对了,共尝试:" + (counts + 1) + " 次");
        } catch (Exception es) {
            System.out.println(es.getMessage());
        }
    }

}

 

        It can be seen from the above that ChatGPT can indeed write code, but it cannot do basic exception handling and boundary condition considerations. At the same time, there may be bugs, and there is a clear gap with the code written by humans.

Guess you like

Origin blog.csdn.net/qq_57163366/article/details/130057420