0

メソッドと配列を使用してユーザー入力を取得し、最小、最大、平均、および合計を表示するプログラムを作成しようとしています。ユーザー入力から最小、最大、合計、および平均を返すメソッドを作成しました。このコードではユーザー入力を取得できますが、表示されません。私は何を間違えましたか?これは私が持っているものです。

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 に答える