java - 查找连续几天之间的温度差异

标签 java algorithm logic pseudocode

所以我在我的课上输入了这段代码,它是一种寻找连续两天之间最大温差的方法。还有十天。我创建了一个包含 10 个索引的数组,然后我做了这个:

import java.util.Arrays;
import java.util.Collections;

public class Forecast
{
   int [] temps;
   int WEEK;
   int Under32 = 0;
   int[] blazing;

   public Forecast( int y, int[] x )
   {
      WEEK = y;

      temps = new int[x.length];

      //instantiate array with same length as parameter
      for ( int i = 0; i <= temps.length-1; i++ )
      {
         temps[i] = x[i];
      }
      Arrays.sort(temps);
   }

   public void setWeek( int u )
   {
      WEEK = u;
   }

   public int getWeek()
   {
      return WEEK;
   }

   public void setArray( int[] newTemps )
   {
      temps = newTemps;
   }

   //returns an array of temps
   public int[] getTemps()
   {
      int[] w = new int[temps.length];
      for(int i = 0; i < temps.length; i++)
      {
         temps[i] = w[i];
      }
      return w;
   }

   public int getUnderFreeze()
   {
      int FROZEN = 0;
      for( int i = 0; i < temps.length; i++ )
      {
         if( temps[i] < 32 )
         {  
            FROZEN += 1;

         }
      }
      return FROZEN;
   }

   public int[] above100Degrees()
   {
    int newArrayLength=0;
    for( int i = 0; i < temps.length; i++ )
    {
        if( temps[i] > 100 )
        {
            newArrayLength++;
        }
    }

    int[] blazing = new int[newArrayLength];
    int positionInNewArray = 0;
    for( int i = 0; i < temps.length; i++ )
    {
        if( temps[i] > 100 )
        {
            blazing[positionInNewArray] = temps[i];
            positionInNewArray++;
        }
    }
    return blazing;
  }   

   public int[] Assorted()
   {
      Arrays.sort(temps);
      return temps;
   }

   //return an array in descending order, using set algorithm
    public int[] descendSort()
    {
       int[] tempArray = new int[temps.length];

       for (int i = temps.length-1; i <= 0; --i)  
       {
         for (int j = 0; j < temps.length-1; ++i){
            tempArray[j] = temps[i];
       }
    }
    return tempArray;
    }         

    //method returning the largest change between two consecutive days
    public int NetChange()
    {
      int biggestNet = Math.abs(temps[0] - temps[1]);
      for( int i = 0; i < temps.length - 1; i++ )
      {
         if( Math.abs((temps[i] - temps[i+1])) > biggestNet )
         {
            biggestNet = Math.abs(temps[i] - temps[i+1]);
         }
      }
      return biggestNet;
     } 

    public String toString()
    {
      String returnString = "The temperature forecast of week " + WEEK + " is logged in as: ";
      for( int i = 0; i < temps.length; i++ )
      {
          returnString += "\t" + temps[i] + "\t";
      }
      returnString += "\n" + "The number of temperatures below freezing is " + getUnderFreeze() + "." + "\n" + 
                             "The largest difference this week was a net change of " + NetChange() + "." + "\n" + 
                             "The temperature above 100 degrees is: " ; 

      int[] blazing = above100Degrees();                       
      for( int i = 0; i < blazing.length; i++ )
      {
         returnString += "\t" + blazing[i] ;
      }
      return returnString;
    }

    public boolean equals(Object c)
    {
      if( !(c instanceof Forecast))
         return false;
      else
      {
         Forecast objTemp = (Forecast) c;

         if( temps.length != objTemp.temps.length )
            return false;
      }
      return true;
     }

我在客户端类中的对象数组是这样的:

int[] temps1 = new int[]{45, 76, 12, 102, 107, 65, 43, 67, 81, 14};

我的输出是这样的:

The largest difference this week was a net change of -2.

这个输出严重错误,我做错了什么????

最佳答案

做差分的时候需要放一个绝对值。它是使用数学课完成的。 Math.abs(温度[0]-温度[1])] 对其余的减法执行相同的操作。 编辑 - 让您的方法将数组作为参数更有意义

关于java - 查找连续几天之间的温度差异,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21341971/

相关文章:

java - 有什么方法可以使用 Checkstyle 强制执行枚举类型命名约定吗?

java - SQL从至少有一个异性的表中获取前3名

java - 如何在android中从不同形状的位图中切出一个圆圈

algorithm - 排序算法 - 分组,加权

algorithm - 查找一组间隔的覆盖范围

c++ - 最大值(value)股票,实现细节

java - 验证真值表的最少检查次数

php - MySQL 将多行连接成一列

java - 这段代码出现编译时错误,帮帮我

Azure逻辑应用程序,无法将消息发送到服务总线