代码不能提供期望的输出

本文关键字:输出 期望 不能 代码 | 更新日期: 2023-09-27 18:13:20

我目前正试图找出我在这个分配的代码逻辑中出错的地方。任何评论或建议将不胜感激!

无论我输入什么,我得到的输出都是:

平均温度0,

最高温度

最低温度0,

平均值,不包括最低0,

寒冷日数10

头等舱

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace TemperatureAverager
{
class Temperatures
{
    public double[] weeksTemperatures;
    public double threshTemp;
    public double average;
    public double averageExcludingLowest;
    public double highest;
    public double lowest;
    public int numOfThreshs;


    public double[] WeeksTemperatures
    {
        get
        {
            return weeksTemperatures;
        }
    }
    public double ThreshTemp
    {
        get
        {
            return threshTemp;
        }
    }
    public double Average
    {
        set
        {
            average = value;
        }
        get
        {
            return average;
        }
    }
    public double AverageExcludingLowest
    {
        set
        {
            averageExcludingLowest = value;
        }
        get
        {
            return averageExcludingLowest;
        }
    }
    public double Highest
    {
        set
        {
            highest = value;
        }
        get
        {
            return highest;
        }
    }
    public double Lowest
    {
        set
        {
            lowest = value;
        }
        get
        {
            return lowest;
        }
    }
    public int NumberOfThreshs
    {
        set
        {
            numOfThreshs = value;
        }
        get
        {
            return numOfThreshs;
        }
    }
    public Temperatures()
    {
    }
    public Temperatures(double[] wTemperatures, double threshT)
    {
        weeksTemperatures = wTemperatures;
        threshTemp = threshT;
    }
    public double DetermineAverage()
    {
        double average = 0;
        for (int x = 0; x < 7; x++) 
        {
            average = average + weeksTemperatures[x];   
        }
        average = average / 7;    
        return average;
    }
    public double DetermineAverageExcludingLowest()
    {
        for (int x = 0; x < 7; x++) 
        {
            averageExcludingLowest = averageExcludingLowest + weeksTemperatures[x];
            if (weeksTemperatures[x] < lowest)   
            {
                lowest = weeksTemperatures[x];   
            }
        }
        averageExcludingLowest = ((averageExcludingLowest - lowest) / 7);   // calculate average excluding lowest temperature
        return averageExcludingLowest;
    }
    public double DetermineLowest()
    {
        for (int x = 0; x < 7; x++) //Traverse through the week's temperatures
        {
            if (weeksTemperatures[x] < lowest)   //find the lowest temperature of the week
            {
                lowest = weeksTemperatures[x];   //and set it to lowest
            }
        }
        return lowest;
    }
    public double DetermineHighest()
    {
        for (int x = 0; x < 7; x++) //Traverse through the week's temperatures
        {
            if (weeksTemperatures[x] > highest)  //find the highest temperature of the week
            {
                highest = weeksTemperatures[x];  //and set it to highest
            }
        }
        return highest;
    }
    public double DetermineNumberOfThreshs()
    {
        for (int x = 0; x < 7; x++) //Traverse through the week's temperatures
        {
            if (weeksTemperatures[x] < threshTemp)   //find the lowest temperature of the week
            {
                numOfThreshs++;
            }
        }
        return numOfThreshs;
    }
    public override string ToString()
    {
        return "====================='nWeekly Statistics'n" + "---------------------'n" + "Average Temperature: " + average + "'nHighest Temperature: "
        + highest + "'nLowest Temperature: " + lowest + "'nAvg. Excl. Lowest: " + averageExcludingLowest + "'n# of Cold Days: " + numOfThreshs + "'n====================="; //Formats and the invoice to be printed
    }
}
}

二级

namespace TemperatureAverager
{
class TemperatureApp
{
    static void Main(string[] args)
    {
        double[] week = new double[7];    
        string[] days = new string[] { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" }; //array to track days of week
        for (int x = 0; x < 7; x++) 
        {
            Console.Write("What was the temperature on " + days[x] + "?: ");
            string userTemperatureInput = Console.ReadLine();
            week[x] = double.Parse(userTemperatureInput);
        }
        Console.Write("How cold is too cold?: ");
        string userThreshInput = Console.ReadLine();    
        double thresh = double.Parse(userThreshInput);
        Temperatures weekOne = new Temperatures(week, thresh);  
        Console.WriteLine(weekOne.ToString());
        Console.ReadLine();
    }
}
}

代码不能提供期望的输出

第一类
public class Temperatures
{
    private double sum;
    private int daysInWeek;
    public double[] WeeksTemperatures { get; set; }
    public double ThreshTemp { get; set; }
    public double Average { get; set; }
    public double AverageExcludingLowest { get; set; }
    public double Highest { get; set; }
    public double Lowest { get; set; }
    public int NumOfThreshs { get; set; }
    public Temperatures(double[] wTemperatures, double threshT)
    {
        this.WeeksTemperatures = wTemperatures;
        this.ThreshTemp = threshT;
        sum = 0.0;
        daysInWeek = 7;
    }
    public void GetWeekStatistics()
    {
        GetSum();
        DetermineLowest();
        DetermineHighest();
        DetermineAverage();
        DetermineAverageExcludingLowest();
        DetermineNumberOfThreshs();
    }
    private void GetSum()
    {
        for (int x = 0; x < daysInWeek; x++) //Traverse through the week's temperatures
        {
            this.sum = this.sum + this.WeeksTemperatures[x];
        }
    }
    public void DetermineLowest()
    {
        this.Lowest = this.WeeksTemperatures[0];
        for (int x = 0; x < daysInWeek; x++) //Traverse through the week's temperatures
        {
            if (this.WeeksTemperatures[x] < this.Lowest)   //find the lowest temperature of the week
            {
                this.Lowest = this.WeeksTemperatures[x];   //and set it to lowest
            }
        }
    }
    public void DetermineHighest()
    {
        this.Highest = this.WeeksTemperatures[0];
        for (int x = 0; x < daysInWeek; x++) //Traverse through the week's temperatures
        {
            if (this.WeeksTemperatures[x] > this.Highest)  //find the highest temperature of the week
            {
                this.Highest = this.WeeksTemperatures[x];  //and set it to highest
            }
        }
    }
    private void DetermineAverage()
    {
        this.Average = this.sum / daysInWeek;
    }
    public void DetermineAverageExcludingLowest()
    {
        this.AverageExcludingLowest = ((this.sum - this.Lowest) / daysInWeek);   // calculate average excluding lowest temperature
    }
    public void DetermineNumberOfThreshs()
    {
        for (int x = 0; x < daysInWeek; x++) //Traverse through the week's temperatures
        {
            if (this.WeeksTemperatures[x] < this.ThreshTemp)   //find the lowest temperature of the week
            {
                this.NumOfThreshs++;
            }
        }
    }
    public override string ToString()
    {
        return "====================='nWeekly Statistics'n" + "---------------------'n" + "Average Temperature: " + this.Average + "'nHighest Temperature: "
        + this.Highest + "'nLowest Temperature: " + this.Lowest + "'nAvg. Excl. Lowest: " + this.AverageExcludingLowest + "'n# of Cold Days: " + this.NumOfThreshs + "'n====================="; //Formats and the invoice to be printed
    }
}

第二类变化

Temperatures weekOne = new Temperatures(week, thresh);
weekOne.GetWeekStatistics();

整个代码都是7。让它成为常数public static int DaysCount = 7的温度类和引用,无论何时需要(见下面的例子)。

class Temperatures
{
  public static int DaysCount = 7;
}

你可以这样使用它:

   static void Main(string[] args)
    {
        double[] week = new double[Temperatures.DaysCount];    
    }

我看不出你在哪里运行的决定…()函数,所以值从来没有计算过。

在resolve…()函数中,应该在运行循环之前将值设置为适当的初始值,如:

public double DetermineHighest()
{
  highest = -273 // degrees
  for (int x = 0; x < DaysCount; x++) //Traverse through the week's temperatures
  {
    if (weeksTemperatures[x] > highest)  //find the highest temperature of the week
    {
      highest = weeksTemperatures[x];  //and set it to highest
    }
  }
  return highest;
}