Java 将扫描仪输入与阵列中的元素进行比较

Java 将扫描仪输入与阵列中的元素进行比较,java,Java,我是java新手,所以如果我犯了一个非常简单的错误,请原谅我。我正在尝试在一个基于文本的冒险游戏中购物。我创建了一个数组shopItems,它将商品列表存储为商店可以出售的字符串。以下是我在游戏中使用的方法的一部分,供用户在游戏中进行购买 String request = s.nextLine().toLowerCase(); for (int i = 0 ; i < shopItems.length ; i++) {

我是java新手,所以如果我犯了一个非常简单的错误,请原谅我。我正在尝试在一个基于文本的冒险游戏中购物。我创建了一个数组
shopItems
,它将商品列表存储为商店可以出售的字符串。以下是我在游戏中使用的方法的一部分,供用户在游戏中进行购买

String request = s.nextLine().toLowerCase();            
        for (int i = 0 ; i < shopItems.length ; i++)
        {
            if(request.equalsIgnoreCase(shopItems[i]))
            {
                System.out.println("We have this item in stock! That will be " + itemPrice[i] + " gold, "
                        + "would you like to purchase this item?");
                String command2 = s.nextLine().toLowerCase();
                if(command2.equals("yes") || command2.equals("y"))
                {
                    if (savings >= itemPrice[i])
                    {
                        System.out.println("Congratulations! You have purchased " + shopItems[i] + ". Thank you "
                                + "for your business.");
                        savings = savings - itemPrice[i];
                        inv.add(shopItems[i]);
                        magicShopPurchase();
                    }

                    else if (savings < itemPrice[i])
                    {
                        System.out.println("I'm sorry, you don't have enough gold to purchase this item! Try "
                                + "again when you have enough!");

                    }
                }
            }
            else if(request.equals("leave"))
            {
                System.out.println("Thank you! Please come again soon!");
                inMagicShop();
            }
            else
            {
                System.out.println("I'm sorry, we don't have any of those in stock at the moment. Would you "
                        + "like to purchase a different item?");
                String command2 = s.nextLine().toLowerCase();
                if(command2.equals("yes") || command2.equals("y"))
                {
                    magicShopPurchase();
                }
                else if(command2.equals("no") || command2.equals("n"))
                {
                    System.out.println("Thank you! Please come again soon!");
                    inMagicShop();
                }
                else
                {
                    System.out.println("Haha, you kiss your mother with that mouth? Come back some other time!");
                    inMagicShop();
                }
            }
        }
第二种是我用来读取文本文件并将其存储到
shopItems
中的方法

try {
    itemList = new String(Files.readAllBytes(Paths.get("C:\\Users\\gravy_000\\Desktop\\Software Development 1\\GameProject\\src\\hallSim\\magicitems.txt")));
    read(itemList);
} catch (IOException e) {
    e.printStackTrace();
}
public static void read(String shopList) {
    shopItems = shopList.split("\\r?\\n");
}
这是Dropbox中文本文件的链接
https://www.dropbox.com/s/rbfsr1fj2yzus1q/magicitems.txt?dl=0

问题在于,您在第一次
请求时告诉用户找不到该项目。equalsIgnoreCase(shopItems[i])
返回false,而不是在
请求不存在时返回false

因此,您应该将代码替换为以下内容或等效内容:

    String request = s.nextLine().toLowerCase();

    if(request.equals("leave")) {
        //leave
    } else {
        boolean isItemInStock = false;

        for (int i = 0 ; i < shopItems.length ; i++) {
            if(request.equalsIgnoreCase(shopItems[i])) {
                isItemInStock = true;
                break;
            }
        }

        if(isItemInStock) {
            System.out.println("We have this item in stock! That will be " + itemPrice[i] + " gold, "
                    + "would you like to purchase this item?");
            //...
        } else {
            System.out.println("I'm sorry, we don't have any of those in stock at the moment. Would you "
                    + "like to purchase a different item?");
            //...
        }
    }
String请求=s.nextLine().toLowerCase();
如果(请求等于(“离开”)){
//离开
}否则{
布尔值isItemInStock=false;
对于(int i=0;i

请注意,if/else语句如何处理与
请求
相关的内容,它位于主循环之外

问题在于,您第一次请求时告诉用户找不到该项目。equalsIgnoreCase(shopItems[i])
返回false,而不是在请求不存在时返回false

因此,您应该将代码替换为以下内容或等效内容:

    String request = s.nextLine().toLowerCase();

    if(request.equals("leave")) {
        //leave
    } else {
        boolean isItemInStock = false;

        for (int i = 0 ; i < shopItems.length ; i++) {
            if(request.equalsIgnoreCase(shopItems[i])) {
                isItemInStock = true;
                break;
            }
        }

        if(isItemInStock) {
            System.out.println("We have this item in stock! That will be " + itemPrice[i] + " gold, "
                    + "would you like to purchase this item?");
            //...
        } else {
            System.out.println("I'm sorry, we don't have any of those in stock at the moment. Would you "
                    + "like to purchase a different item?");
            //...
        }
    }
String请求=s.nextLine().toLowerCase();
如果(请求等于(“离开”)){
//离开
}否则{
布尔值isItemInStock=false;
对于(int i=0;i

请注意,if/else语句如何处理与
请求
相关的内容,它位于主循环之外

shopItems
String[]
还是其他类型的数组?您的代码表明是这样的。是的
shoppitems
是一个
String[]
您的代码逻辑看起来很好,您能给出一个它没有按预期执行的示例吗?你确定没有多余的空间或类似的东西吗?是的,例如,
shopItems[11]=“Rags”
。我尝试了所有不同的
碎布
变体作为输入
请求
,例如
碎布
碎布
,还尝试了在这两个变量之后添加空格,以防意外添加一个。知道
shopItems
中的元素是从文本文件中获取的,这会有帮助吗?文件的每一行都是一个单独的元素。除了这个问题,我的代码中没有关于
shopItems
的错误,所以我无法想象这与此有关。请您显示一下加载
shopItems
数据的代码,以及源文件的示例。是
shopItems
字符串[]
,还是其他类型的阵列?您的代码表明是这样的。是的
shoppitems
是一个
String[]
您的代码逻辑看起来很好,您能给出一个它没有按预期执行的示例吗?你确定没有多余的空间或类似的东西吗?是的,例如,
shopItems[11]=“Rags”
。我尝试了所有不同的
碎布
变体作为输入
请求
,例如
碎布
碎布
,还尝试了在这两个变量之后添加空格,以防意外添加一个。知道
shopItems
中的元素是从文本文件中获取的,这会有帮助吗?文件的每一行都是一个单独的元素。我的代码中没有关于
shopItems
的错误,除了我遇到的这个问题,所以我无法想象这与此有关。请您显示一下代码,您将
shopItems
的数据加载到哪里,以及源文件的示例?非常感谢这帮助我让代码正常工作!非常感谢这帮助我让我的代码正常工作!