我正在使用Java制作控制台游戏。它不会很简单,但我也不希望它变得非常高级。我只是想使用我学到的基础知识来测试自己的技能。我已经尝试了几次,但总是遇到同样的“问题”。
虽然这不是一个实际的问题,但我可以做得更好。最好的解释方法就是展示一些示例代码。
这是我的主类。我们将其称为“Main.java”。
package com.mattkx4.cgamedev.main;
public class Main {
public static void main(String[] args) {
}
}
好的,现在让我们在Main.java中创建一个新方法。我们将这个方法称为“Start”。我们的Main类现在如下所示:
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();
}
}
}
现在我们将添加两个新方法。我们将它们命名为 "middle" 和 "end"。我们的最终类现在如下所示:
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.");
}
}
我认为这是一个非常低效的程序,特别是在编程方面。除了在方法内调用其他方法推进程序之外,还有其他方法来完成这个程序。
我的问题总结如下:上述代码是否是编写控制台游戏的最有效方式?
提前感谢您的帮助!如果您有任何问题,我很乐意回答。 -马修