【问题标题】:Loops and comparing variable values循环和比较变量值
【发布时间】:2013-11-12 03:03:53
【问题描述】:

用户输入年份并存储在 year 中。并且调用了一个方法来计算并返回将在一周中的同一天的下一年。月份和日期不会仅更改年份。在这种情况下,月份为 Jan,日期为 1。

/**
 * @param theYear the year given
 */
public int yearSync(int theYear)
{
   int month = 1;
   int day = 1;
   int year = theYear;

   int aMonth = 1;
   int aDay = 1;
   int aYear = year++;

   Date d1 = new Date(month, day, year);
   Date d2 = new Date(aMonth, aDay, aYear);

   boolean valid = false;       

   while (!valid)
   {
      if(d1.getDayOfWeek().equals(d2.getDayOfWeek)))//another class provided is used to 
      {                                             //calc what day of the week it is
         System.out.println(aYear); //prints the year
      }                             //that falls on the same week day as the input year
      else
      {
         aYear++;
      }
   }
   return aYear;
}

不要求答案只是想知道我的逻辑错误在哪里,以及在处理此类问题时我可以做些什么来改变我的思维过程。如果有任何混淆,例如,如果我输入 2014,则返回的年份为 2020;他们都在星期三跌倒。

编辑:改变循环类型。

【问题讨论】:

  • 请注意,您有一个无限循环。
  • 考虑使用while 循环,因为您不知道需要多少次迭代才能得到答案。
  • @SotiriosDelimanolis 我尝试了一个带有布尔值的while循环,例如while(!valid),但我也会得到一个无限循环。我知道 for 循环是无限的,应该提到它,我的错。
  • 有趣的是i++ >= i 甚至不会运行一次,而i <= i++ 会无限运行
  • @Claudiu 真的,我对此做了双重考虑。

标签: java for-loop while-loop logic


【解决方案1】:

你的循环for (int i = 0; i <= i++; i++) 永远不会停止...

第一次通过,i设置为0,条件为i < i++,将i的值变为i+1,但比较成功。

所以第一次循环时,i 的值为 1。在循环结束时,它会因为 i++ 而增加它(值 2),然后它会比较它再次使用i <= i++,它将通过,但也会将 i 设置为 3。

所以,i 的值在您的循环中将是 1、3、5、7,...。

这个循环条件很奇怪,但是,这并不是真正的问题......因为它本质上与:

while (true) {...}

奇怪的是,当您实际上找到具有相同星期几的年份时,您不会增加年份,即一旦您找到第一个匹配项,aYear++ 就永远不会改变,然后您只需重复永远一样的System.out.println(...)

但同样,这一切都毫无意义,因为您永远不会在循环内更改日期值d2.....

你想要的是这样的:

while(true) {
    aYear++;
    Date d2 = new Date(aMonth, aDay, aYear);
    if (d1.getDayOfWeek().equals(d2.getDayOfWeek)))//another class provided is used to 
    {                                             //calc what day of the week it is
        System.out.println(aYear); //prints the year
        return aYear;
    }
}

【讨论】:

  • 谢谢!我注意到aYear 永远不会更新,我没有想到在循环本身中创建Date 对象。那么为什么在循环外创建第二个Date 对象会阻止aYear 正常更新呢?
【解决方案2】:

您只是增加了aYear 变量,但d2 引用将始终保持aYear 创建时的初始值。因此,您还需要在 for 循环中更新 d2 参考,以便在每个循环迭代中都有下一年。

您还需要在找到所需年份后打破循环。这是更新的 for 循环,希望它能正常工作:

 //for sake of this question I will use a for loop
   for (int i = 0; i <= i++; i++)
   {
      if(d1.getDayOfWeek().equals(d2.getDayOfWeek)))//another class provided is used to 
      {                                             //calc what day of the week it is
         System.out.println(aYear); //prints the year
         break;
      }                             //that falls on the same week day as the input year
      else
      {
         aYear++;
      }
      d2 = new Date(aMonth, aDay, aYear);
   }
   return aYear;
}

【讨论】:

    【解决方案3】:

    首先,您的循环将永远持续下去。你有:

    for (int i = 0; i <= i++; i++) { ... }
    

    这相当于:

    int i = 0;
    while (i <= i++) {
        ...
    }
    

    i++ 的作用是计算为i,并将i 加一。为了让它更明显,让我们将i++ 所做的事情分解成多个语句:

    int i = 0;
    while (true) {
        //left-hand-side is 'i'
        int leftHandSide = i;
        //right-hand-side is 'i++' which evaluates to 'i' and then 'i' is incremented
        int rightHandSide = i;
        i += 1;
        if (leftHandSide <= rightHandSide) {
            break;
        }
        ...
    }
    

    如果你注意到这只是比较i &lt;= i,这总是正确的,所以循环永远不会中断。

    【讨论】:

      【解决方案4】:

      您可以使用它从两个数组中查找匹配值。添加 getDayofWeek() 以及您需要的任何其他内容。

           for(int x : d1){
      
               //Some counter = 0;  
      
              for(int y : d2) {  
                 if(x.equals(y))
                 //counter++;
             }
              System.out.println(y);
      
             }}
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-08-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多