java - 在 for 循环中,我的 if 语句没有产生我想要的结果

标签 java if-statement for-loop switch-statement

大家好,当我尝试访问代码时,会发生以下情况,第一部分很好,因为库存中没有手提包,我希望它说您正在携带,但是如果我有手提包,我希望它说您正在携带将这些元素放在手提包中,但会发生以下情况;

现在怎么办? 拿到 torch 好的

现在怎么办? 列表 您携带: torch

现在怎么办? 走楼梯 这里很黑!

现在怎么办? 拿手提包 好的

What now? 
list
You are carrying these items in your handbag:
torch
You are carrying these items in your handbag:
wallet
You are carrying these items in your handbag:
keys
You are carrying these items in your handbag:
ring
You are carrying these items in your handbag:
USB
You are carrying these items in your handbag:
mobile
You are carrying these items in your handbag:
handbag

这是代码

public void listWhatYouHave()
{
    for (int i = 0; i < 7; i++)
    {
        if (hasItem[6])
        {
           System.out.println("You are carrying these items in your handbag:");
           switch (i)
           {
                case 0: 
                    System.out.println("torch");
                    break;
                case 1:
                    System.out.println("wallet");
                    break;
                case 2:
                    System.out.println("keys");
                    break;
                case 3:
                    System.out.println("ring");
                    break;
                case 4:
                    System.out.println("USB");
                    break;
                case 5:
                    System.out.println("mobile");
                    break;
                case 6:
                    System.out.println("handbag");
                    break;
                default: 
                    System.out.println("invalid item!");
                    break;
           }
        }
        else if (hasItem[i])
        {
            System.out.println("You are carrying:");
            switch (i)
            {
                case 0: 
                    System.out.println("torch");
                    break;
                case 1:
                    System.out.println("wallet");
                    break;
                case 2:
                    System.out.println("keys");
                    break;
                case 3:
                    System.out.println("ring");
                    break;
                case 4:
                    System.out.println("USB");
                    break;
                case 5:
                    System.out.println("mobile");
                    break;
                case 6:
                    System.out.println("handbag");
                    break;
                default: 
                    System.out.println("invalid item!");
                    break;
            }
        }
    }

你能帮忙吗...谢谢(这显然是java)

抱歉,我的含糊不清......几乎当我没有手提包时,我希望它通过说“你正在携带”来列出我携带的东西,但是如果我在拿起手提包时拿起手提包......我想让它说“您的手提包中携带这些元素”,但目前它打印出您只携带一次...但是您的手提包中携带这些元素在每一行中都打印出来...我只想要一次。

最佳答案

您的 hasItem[6] 值为 true,则 if (hasItem[6]) 条件将始终为 true。 你的 else 部分不会被执行

可能您需要 if(hasItem[i]) 而不是 if(hasItem[6])

关于java - 在 for 循环中,我的 if 语句没有产生我想要的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19464288/

相关文章:

java - 使用嵌套在 BorderPane 中的 ScrollPane 调整 JavaFX 阶段的大小

java - 使用 if 和 else 语句设置文本颜色

C 程序无法编译,不确定原因

java - 更改 java 循环中的输入变量

java - 仅检查字符串中整数的工作解决方案?

Java 比较器字母数字字符串

java - 合并两个 Observables/Singles 的方法有哪些?

batch-file - 批处理脚本错误级别帮助

java - 如何压缩由 OR 运算符组成的 if 语句?

javascript - 使用javascript迭代添加一个类