gpt4 book ai didi

Java 控制台游戏开发 |方法

转载 作者:搜寻专家 更新时间:2023-11-01 04:04:17 25 4
gpt4 key购买 nike

<分区>

好吧,请允许我详细说明我的困境。

我正在使用 Java 制作控制台游戏。它不会 super 简单,但我也不希望它真的很高级。我只是想用我学到的基础知识来测试我的技能。我已经开始了几次,但总是遇到同样的“问题”。

虽然这不是问题,但我可以做得更好。解释它的最好方法是仅显示一些示例代码。

这是我的主类。我们将其称为“Main.java”。

package com.mattkx4.cgamedev.main;

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

}
}

好的,现在让我们在 Main.java 中创建一个新方法。我们将此方法称为“开始”。我们的主类现在看起来像这样:

package com.mattkx4.cgamedev.main;

import java.util.Scanner;

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

}

public static void start() {
System.out.println("This is the start.");
Scanner s = new Scanner(System.in);
System.out.println("Enter \"NEXT\" to continue.");
String in = s.nextLine();

if(in.equalsIgnoreCase("NEXT")) {
}else{
System.out.println("Please input \"NEXT\".");
start();
}
}
}

现在我们再添加两个方法。我们称它们为“中间”和“末端”。我们完成的类(class)现在看起来像这样:

package com.mattkx4.cgamedev.main;

import java.util.Scanner;

public class Main {
public static void main(String[] args) {
start();
}

public static void start() {
System.out.println("This is the start.");
Scanner s = new Scanner(System.in);
System.out.println("Enter \"NEXT\" to continue.");
String in = s.nextLine();

if(in.equalsIgnoreCase("NEXT")) {
middle();
s.close();
System.exit(0);
}else{
System.out.println("Please input \"NEXT\".");
start();
}
}

public static void middle() {
System.out.println("This is the middle.");
System.out.println("Let's move on to the end.");
end();
}

public static void end() {
System.out.println("This is the end.\nFinishing up, and heading back to the start() method to close program.");
}
}

在我看来,我在这里写的是一个非常低效的程序。用那个编码效率低下。必须有另一种方法来执行此程序,而不是调用其他方法内部的方法来执行程序。

我的问题总结:上面的代码是编写主机游戏的最有效方式吗?

提前致谢!如果您有任何问题,我很乐意回答。-马修

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com