玩家应该输入系列中的咒语,如果它在数组中,计算机将在数组中输出不同的咒语。如果它不在数组内,它将打印数组中的第一个咒语,依此类推。
我只用了 4 个咒语进行了测试,但它总是输出“Crucio”。我不知道为什么会发生这种情况!
请帮忙。
这是我到目前为止所拥有的:
public class HarryPotterGame {
public static void main(String[] args) {
System.out.println("---------------------------------------");
System.out.println("Welcome to the Harry Potter Spell Game!");
System.out.println("---------------------------------------");
String[] Spells;
Spells = new String[] {"Accio","AvadaKedavra","Crucio","Imperio"};
System.out.println("Your turn. Do not use spaces!");
Scanner sn = new Scanner(System.in);
String Spell1 = sn.nextLine();
int i = 0;
while(Spells[i] != Spell1){
if (i == 4){
System.out.println("Accio");
}
i++;
break;
}
System.out.println(Spells[i+1]);
最佳答案
使用equals method比较字符串
while(!Spells[i].equals(Spell1)){
关于java - 我正在尝试制作哈利波特咒语游戏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22184527/