大家好,当我尝试访问代码时,会发生以下情况,第一部分很好,因为库存中没有手提包,我希望它说您正在携带,但是如果我有手提包,我希望它说您正在携带将这些元素放在手提包中,但会发生以下情况;
现在怎么办? 拿到 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/