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

时间:2016-04-17 23:22:22

标签: java

我是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

1 个答案:

答案 0 :(得分: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语句是主循环之外。