【发布时间】:2013-10-19 09:01:00
【问题描述】:
大家好,所以当我尝试访问代码时会发生以下情况,第一部分很好,因为没有库存中的手提包我希望它说你正在携带但是如果我有手提包我希望它说你是将这些物品放在手提包中,但会发生以下情况;
现在呢? 拿到手电筒 好的
现在呢? 列表 您携带: 手电筒
现在呢? 走楼梯 这里很黑!
现在呢? 拿手提包 好的
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)
抱歉,模糊不清......几乎当我没有手提包时,我希望它通过说“你正在携带”来列出我携带的物品,但是如果我在拿起手提包的时候拿起手提包......我想让它说“你的手提包里有这些物品”,但目前它打印出你只携带一次......但是你的手提包里有这些物品印在每一行......我只想要一次。
【问题讨论】:
-
不太清楚你想要什么,但我认为
if(hasItem[6])应该是if(hasItem[i]) -
@Andrew:您能否尝试完善您的问题?
-
对不起,伙计们,我已经编辑了问题,最后添加了我想要的内容......我只是希望它只打印一次“你的手提包里有这些物品”,而不是每行..那就是如果我有 (hasItem[6])
标签: java if-statement for-loop switch-statement