gpt4 book ai didi

java - 为我的游戏在 java 方法中运行一个方法?

转载 作者:行者123 更新时间:2023-12-02 01:37:55 25 4
gpt4 key购买 nike

我正在努力处理我的游戏的库存扫描,如果存在“飞天扫帚”,它基本上会搜索用户库存(它是用另一种方法收集的,上传代码太长),如果没有,它会再次运行方法challengedragon();否则,如果该项目存在,它将继续进行下一个挑战。我曾考虑插入方法作为参数,但这是不可能的。这就是我现在所拥有的。 :

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 根据是否找到该项目来回答 true 或 false?然后你会得到这样的东西:

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 的返回值来决定是否循环。

关于java - 为我的游戏在 java 方法中运行一个方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54950669/

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