【问题标题】:For loop with 2 variables?For循环有2个变量?
【发布时间】: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


【解决方案1】:

你会想用这个替换你的 for 循环语句:

for ( int xGreenBottles = 11, xyGreenBottles = 10; xGreenBottles > 0 && xyGreenBottles > 0 ; xGreenBottles--, xyGreenBottles-- )

另外请记住,如果我没看错的话,你的“if”语句永远不会计算为真,因为 for 循环的计算顺序如下:

  1. 初始化变量(在第一个;之前)
  2. 检查条件(两者之间;;),如果失败则退出循环
  3. 执行代码块
  4. 递增变量(循环的最右边部分,在两个 ;; 之后)
  5. 从 #2 重复

如果两个变量中的任何一个达到零,循环将中断而不执行块。

老实说,我并不完全清楚你想用这段代码实现什么,也许提供一些“伪代码”会帮助我们更好地帮助你......如果有更多的东西,很乐意添加到我的答案中具体你正在寻找!

希望这会有所帮助!

【讨论】:

    【解决方案2】:

    for 循环包含由 ; 分隔的 3 部分:

    • 初始化:初始化循环变量的地方
    • 循环限制(这应该是一个输出为布尔值的条件)
    • 在每次迭代时更新计数器
    for(
           int xGreenBottles = 11, xyGreenBottles = 10; 
           (xGreenBottles > 0 && xyGreenBottles > 0); 
           xGreenBottles --, xyGreenBottles--
        )
     {
                }
    

    【讨论】:

      【解决方案3】:

      您没有使用正确的格式。

      分号是根据功能分隔for循环的,不能混用。

      在第一个分号之前初始化变量:

      int a = 0, b = 0;
      

      在第二个分号之前设置条件(任何返回真或假的表达式):

      a < 10 && b < 10;
      

      在最后一节中,您操作变量:

      a++, b++
      

      总之就是:

      for (int a = 0, b = 0; a < 10 && b < 10; a++, b++)
      

      这只是一个格式化示例,您应该可以通过它轻松解决问题。

      【讨论】:

      • @ScaryWombat 改变了它
      【解决方案4】:

      试试这个。

      for (int xGreenBottles = 11, xyGreenBottles = 10; xGreenBottles > 0 && xyGreenBottles > 0; xGreenBottles--, 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.");
        }
      }
      

      我相信你是故意的

      if (xGreenBottles != 0 && xyGreenBottles != 0)
      

      我发现了几个与此类似的问题,您可能会考虑检查它们并评估这些问题的答案,以便更好地理解 Java for 循环语法。 (Java for loop multiple variables)

      【讨论】:

        猜你喜欢
        • 2010-11-13
        • 2018-08-15
        • 2022-01-26
        • 2015-09-26
        • 1970-01-01
        • 2013-09-10
        • 2014-06-12
        • 1970-01-01
        • 2017-11-14
        相关资源
        最近更新 更多