0

我正在尝试创建一个程序,该程序使用方法和数组来获取用户输入并显示 min、max、ave 和 total。我创建了从用户输入返回最小值、最大值、总计和平均值的方法。该代码让我获得用户输入但不会显示。我做错了什么?这就是我所拥有的。

class Program
 {
   const int SIZE = 6;
   static void Main(string[] args)

{


     double min = 0.0;
      double max = 0.0;
      double ave = 0.0;
      double total = 0.0;
      double[] numbers = new double[SIZE];


  getInput(numbers);
  calcmin(numbers, ref min);
  calcmax(numbers, ref max);
  calcTotal(numbers, ref total);
  calcave(numbers, ref ave, ref total);
  printResult(numbers, ref min, ref max, ref ave, ref total);

  }//v

  static void calcave(double[] numbers, ref double ave, ref double total)

  {
    int sub = 0;
    while (sub < numbers.Length)

  {
     ave = total / numbers.Length;

  }
     sub++;

  }//end while

 static void calcTotal(double[] numbers, ref double total)

 {
   int sub = 0;
   while (sub < numbers.Length)

 {
    total = numbers[sub] + total ;

 }
   sub++

 }//end while

 static void calcmax(double[] numbers, ref double max)

 {
  int sub = 0;
  max = numbers[0];
  while (sub < numbers.Length)

   if (numbers[sub] > max)

 {
    numbers[sub] = max;

}//end if

 sub++;

 }//end while

  static void calcmin(double[] numbers, ref double min)

 {
  int sub = 0;
  min = numbers[0];
  while (sub < numbers.Length)

  if (numbers[sub] < min)
  {
     numbers[sub] = min;

  }//end if

   sub++;

 }//end while

 static void getInput(double[] numbers)

 {
   int sub = 0;
   for (sub = 0; sub < numbers.Length; sub++)

   {
      Console.WriteLine("Enter number {0}",sub + 1);

       while (!double.TryParse(Console.ReadLine(), out numbers[sub]))

       Console.WriteLine("Please try again.");

 }//End write lines


    }//end while

 static void printResult(double[] numbers, ref double min, ref double max, ref double ave, ref double total)

  {

     Console.WriteLine("The smallest number is {0}.", min);

     Console.WriteLine("the largest number is {0}.", max);

     Console.WriteLine("the total is {0}.", total);

     Console.WriteLine("The average is {0}.", ave);

}//end write lines for out put of Minimum, Maximum, Total, and Average.
     }
   }
 }
4

2 回答 2

0

在您的calcmin方法中,您不会增加s循环中的值。所以循环永远不会结束。将其更改为:

static void calcmin(double[] numbers, ref double min)
{
    int sub = 0;
    min = numbers[0];
    while (sub < numbers.Length)
    {
        if (numbers[sub] < min)
        {
            numbers[sub] = min;
        }

        sub++;
    }
 }

中的相同错误calmax。你的总数应该是这样的:

static void calcTotal(double[] numbers, ref double total)
{
    int sub = 0;
    while (sub < numbers.Length)
    {
            total = numbers[sub] + total;
            sub++;     //IT MUST BE HERE
    }  
    //sub++;           //NOT HERE
}

和同样的错误calcave

编辑:寻找差异。这是完整的代码。

class Program
{
    const int SIZE = 6;
    static void Main(string[] args)
    {
        double min = 0.0;
        double max = 0.0;
        double ave = 0.0;
        double total = 0.0;
        double[] numbers = new double[SIZE];

        getInput(numbers);
        calcmin(numbers, ref min);
        calcmax(numbers, ref max);
        calcTotal(numbers, ref total);
        calcave(numbers, ref ave,  total);
        printResult(numbers, ref min, ref max, ref ave, ref total);

        Console.ReadLine();
    }

    static void calcave(double[] numbers, ref double ave,  double total)
    {
        ave = total / numbers.Length;
    }

    static void calcTotal(double[] numbers, ref double total)
    {

        int sub = 0;
        while (sub < numbers.Length)
        {
            total = numbers[sub] + total;
            sub++;
        }
    }

    static void calcmax(double[] numbers, ref double max)
    {
        int sub = 0;
        max = numbers[0];
        while (sub < numbers.Length)
        {
            if (numbers[sub] > max)
            {
                max = numbers[sub];  //HERE
            }
            sub++;
        }
    }

    static void calcmin(double[] numbers, ref double min)
    {
        int sub = 0;
        min = numbers[0];
        while (sub < numbers.Length)
        {
            if (numbers[sub] < min)
            {
                min = numbers[sub];  //HERE
            }

            sub++;
        }
    }

    static void getInput(double[] numbers)
    {
        int sub = 0;
        for (sub = 0; sub < numbers.Length; sub++)
        {
            Console.WriteLine("Enter number {0}", sub + 1);

            while (!double.TryParse(Console.ReadLine(), out numbers[sub]))
                Console.WriteLine("Please try again.");
        }
    }

    static void printResult(double[] numbers, ref double min, ref double max, ref double ave, ref double total)
    {

        Console.WriteLine("The smallest number is {0}.", min);

        Console.WriteLine("the largest number is {0}.", max);

        Console.WriteLine("the total is {0}.", total);

        Console.WriteLine("The average is {0}.", ave);
    }
}
于 2013-04-02T08:42:14.800 回答
0

您的程序中有几个错误。

在不是里面的calcmin声明。这样,循环将永远不会结束。另一个问题是您在方法中的逻辑。此方法用最小值替换数组中的所有值。在此方法之后,您的整个数组具有相同的值。sub++whilewhilecalcminnumbers

calcmax您的方法也是如此。sub++在您的 while 循环之外,您正在用最大值(等于前一种方法之后的最小值)替换所有值。

calctotal有循环的sub++外部。while

calcave 只需要总数和项数来计算平均值。我不明白你为什么使用while循环(sub++在循环之外)。

您应该阅读语句块以了解如何sub++在 while 循环内移动。

于 2013-04-02T08:51:37.860 回答