【发布时间】:2017-01-12 02:36:55
【问题描述】:
for (int xGreenBottles = 11, xyGreenBottles = 10; xGreenBottles > 0; xGreenBottles --, xyGreenBottles > 0, xyGreenBottles--)
if (xGreenBottles == 0 && xyGreenBottles == 0)
{
System.out.println(xGreenBottles + " green bottles standing on the wall, " + xGreenBottles + " green bottles standing on the wall, And if 1 green bottle should accidently fall, there'll be " + xyGreenBottles + " green bottles standing on the wall.");
}
else {
System.out.println("There’ll be no green bottles standing on the wall.");
}
尝试了十亿种不同的方法,但到目前为止没有任何效果。如果它没有任何连贯的地方,那可能是因为我在过去的 4 个小时里一直在尝试这样做,而我的大脑已经炸了。很抱歉,如果长 system.out.println 难以阅读。
另外 - 它需要使用 2 个变量。
编辑
好吧,抱歉,我没有说清楚 - 我不确定如何让“for”有 2 个变量,因为它会一直给我一个错误。主要是给我“不是一个表情”,并且想知道如何解决它。
【问题讨论】:
-
有什么问题?
-
有什么问题和问题是什么?
-
for (int xGreenBottles = 11, xyGreenBottles = 10; xGreenBottles > 0 && xyGreenBottles > 0; xGreenBottles --, xyGreenBottles--)?或者也许将&&替换为||,这取决于您要实现的目标。 -
@AntonH 应该是一个答案
-
@ScaryWombat 我会,但 OP 并没有真正提出问题,所以我不能说我的“答案”是正确的,或者给出一个完整的答案。例如,我使用了
&&,但这意味着我永远不会进入循环中的if语句。
标签: java loops variables for-loop