循环并比较变量值

问题描述 投票:0回答:4

用户输入年份并将其存储在

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 年;他们都在周三。

编辑:更改循环类型。

java for-loop while-loop logic
4个回答
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;
    }
}

0
投票

您只是递增

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;
}

0
投票

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

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

这相当于:

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

i++
的作用是计算为
i
,并将
i
加 1。为了使其更加明显,让我们将
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 <= i
,这总是正确的,所以循环永远不会中断。


0
投票

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

     for(int x : d1){
           
         //Some counter = 0;  
     
        for(int y : d2) {  
           if(x.equals(y))
           System.out.println(y);
           //counter++;
       }
        
        
    
© www.soinside.com 2019 - 2024. All rights reserved.