使用方法并将用户输入放入数组中

本文关键字:数组 输入 用户 使用方法 | 更新日期: 2023-09-27 18:07:05

我正在尝试创建一个程序,使用方法和数组来获取用户输入并显示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.
     }
   }
 }

使用方法并将用户输入放入数组中

你的程序中有几个错误。

calcmin中,sub++不在while语句中。这样,while循环将永远不会结束。另一个问题是您在calcmin方法中的逻辑。此方法将数组中的所有值替换为最小值。使用此方法后,整个numbers数组具有相同的值。

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

calctotalsub++置于while循环之外。

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

你应该仔细阅读语句块,了解如何在while循环中移动sub++

在您的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

EDIT:查找差异。这里是完整的代码。

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