繁体   English   中英

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

[英]Comparing a scanner input to elements in an array

我是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语句如何在主循环之外

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM