gpt4 book ai didi

java - 从用户输入调用方法

转载 作者:太空宇宙 更新时间:2023-11-04 12:42:23 25 4
gpt4 key购买 nike

我目前正在开发一个项目,我被要求创建一个基于二维数组的益智类型游戏。我已经创建了所有工作正常的方法,但我不太知道如何制作它,以便用户可以在程序运行时通过用户输入调用这些方法。

我不想上传我的代码,因为这是一个很大的类(class),我不希望其他类(class)成员找到它并复制我的代码。

谢谢:-)

最佳答案

尝试像这样的简单菜单循环:

// scanner created outside the loop because it will be used every iteration
Scanner s = new Scanner(System.in);
while(true) {
System.out.print("Please choose an option: ");
// read some input and trim the trailing/ leading whitespaace
String input = s.nextLine().trim();
// check to see which move was called
if (input.equals("foo")) {
foo();
}
else if (input.equals("bar")) {
bar();
}
// break out of the menu loop
else if (input.equals("exit")) {
break;
}
// if none of the above options were called
// inform user of invalid input
else {
System.out.println("Invalid input");
}
}
// exit program
System.out.println("Goodbye!");

只需根据需要添加选项即可

关于java - 从用户输入调用方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36702319/

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