java - 让一个方法在我的游戏的方法 java 中运行?
问题描述
我正在为我的游戏处理库存扫描而苦苦挣扎,如果存在“飞天扫帚”,它基本上会搜索用户库存(它是用另一种方法收集的,上传代码太长),如果没有,它将运行该方法再次挑战dragon();否则,如果该项目存在,它将继续进行下一个挑战。我认为插入方法作为参数,但这是不可能的。这就是我现在所拥有的。:
public class Main {
String Flyingbroom = "Flying broom";
public static void main(String[] args) {
Player_inventory p = new Player_inventory();
challengedragon();
}
public void challengedragon() {
System.out.println("a Hungarian Horntail dragon! Let's start the battle! You have four options to beat the dragon: ");
System.out.println("1: Fly away with your broom");
System.out.println("2: Fight the dragon");
System.out.println("3: Just run to the egg and get it");
System.out.println("4: Hide behind a rock");
System.out.println("5: Go back to Hogwart");
System.out.println("Your choice is: ");
Scanner in = new Scanner(System.in);
int dragonfightchoice = in .nextInt();
if (dragonfightchoice == 1) {
{
p.Scanitem(Flyingbroom,
"Good choice! You managed to kill the Hungarian Horntail dragon and to get the golden egg",
"You dont have the broom. Try to search for the broom",
playerHP);
proceedtonextchallengelake();
} else if (dragonfightchoice == 2) {
System.out.println("The Hungarian Horntail dragon fired you. - 70HP. ");
playerHP -= 70;
challengedragon();
} else if (dragonfightchoice == 3) {
System.out.println("Bad idea... You lose 100 HP");
playerHP -= 100;
challengedragon();
} else if (dragonfightchoice == 4) {
System.out.println("The dragon found you. You lose 30 HP");
playerHP -= 30;
challengedragon();
} else if (dragonfightchoice == 5) {
Hogwart();
} else {
invalid();
challengedragon();
}
}
对于我的库存类:
public void Scanitem(String item, String trueouputext, String textifconditionisnotmet) {
if (inv.contains(item) == true) {
System.out.println(trueouputext);
} else if (inv.contains(item) == false) {
System.out.println(textifconditionisnotmet);
}
public static ArrayList<String> inv = new ArrayList<String>();
各位有什么推荐吗?
解决方案
是否有其他步骤来填充库存(变量inv
)?
此外,您是否不希望 ScanItem 根据是否找到该项目来回答真假?然后你会有这样的事情:
public boolean scanitem(String item) {
return ( inv.contains(item) );
}
if ( p.scanItem(flyingBroom) ) {
System.out.println("Good choice! You managed to kill the Hungarian Horntail dragon and to get the golden egg");
} else {
System.out.println("You dont have the broom. Try to search for the broom");
}
这会让你更接近你想要的。但是,您还需要在代码中添加另外两个问题:
您将需要某种循环,而不是challengeDragon
从自身内部调用。
不知何故,必须使用 scanItem 的返回值来决定是否循环。
推荐阅读
- azure-devops - 有没有办法在 Azure DevOps 中模拟动态 YAML 管道?
- go - 为什么 Go 中的作业会创建副本?
- reactjs - 在反应中使用材质 UI 的 Hook 调用无效
- c# - 如何验证 lambda 授权程序中的端点
- html - 如何解决这个简单的 Aurelia CSS 问题?
- rsa - 将解码的 pkcs12 密钥转换为 pem 格式
- mysql - SQL 命令错误/外键问题
- python - 使用字典中给出的单词出现更新数据框的单元格
- javascript - 如何使用 HTML 和 Javascript 在没有按钮的情况下更改背景图像
- extension-methods - OnAfterOpenSolution 和 OnAfterOpenProject 未触发,将扩展升级到 Visual Studio 2022