我是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进行比较,以检查用户要购买的商品在商店中是否可用,但是它无法识别shopItems中的任何元素。我在使用此方法时是否做错了/某处有错误?这是我在这里的第一篇文章,所以如果我遗漏了任何重要内容,请原谅我。

编辑

首先是在哪里调用将元素存储到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();
}


第二个是我用来读取文本文件并将其存储到shopItems的方法。

public static void read(String shopList) {
    shopItems = shopList.split("\\r?\\n");
}


这是Dropbox中文本文件的链接
https://www.dropbox.com/s/rbfsr1fj2yzus1q/magicitems.txt?dl=0

最佳答案

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

因此,您应使用类似以下代码的代码替换代码:

    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?");
            //...
        }
    }


注意使用request处理内容的if / else语句如何在主循环之外。

10-07 22:42