2016-04-10 15 views
0

ユーザーが名前とスコアを取るボウリングゲームのコードを書く必要があります。ユーザーが名前やスコアを書き込まずに入力すると停止します。その情報を取得し、得点の平均値を取得して印刷し、得点と名前を最高から最低まで印刷します。これは私が今まで持っているものですが、ソートコードと、ユーザーが配列をすべて埋めていない場合(この場合は最大10個)、0の束なしで印刷する方法はわかりません。スコアを高低から並べ替えて印刷する

これは平均、最高と最低のスコアを取得するための別のクラスである:

class BowlingScore 
{ 

    public int LowScore(int[] scores, int j) 
    { 
     int min = scores.Where((v, i) => i < j).Min(); 
     return Array.IndexOf(scores, min); 
    } 

    public int HighScore(int[] scores) 
    { 
     int max = scores.Max(); 
     return Array.IndexOf(scores, max); 
    } 

    public double AverageScore(int[] numbers, int j) 
    { 
     double sum = 0; 
     for (int i = 0; i < j; i++) 
     { 
      sum += numbers[i]; 
     } 
     return (double)sum/j; 
    } 
    public void Swap(ref int a, ref int b) 
    { 

     int temp = a; 
     a = b; 
     b = temp; 
    } 

} 

}

 and this is the main: static void Main(string[] args) 
    { 
     BowlingScore bs = new BowlingScore(); 
     const int MAX = 300; 
     const int SIZE = 10; 
     int i; 

     // create an array with 10 elements 
     string[] scoreInfo = new string[SIZE]; 
     string[] names = new string[SIZE]; 
     int[] scores = new int[SIZE]; 



     Console.WriteLine("Saturday Coder's Bpwling Team"); 
     Console.WriteLine("Enter in a name and a score for each person on the team,"); 
     Console.WriteLine("For example, ''Mary 143''. Just hit Enter when you are done"); 

     for (i = 0; i < SIZE; i++) 
     { 
      Console.Write("Enter in a name and a score: "); 


      // Read one line of data from the file and save it in inputStr 
      string inputStr = Console.ReadLine(); 
      // if statement to break when the user enters a zero 
      if (inputStr == String.Empty) 
      { 
       break; 
      } 
      // The Split method creates an array of two strings 
      scoreInfo = inputStr.Split(); 
      // Parse each element of the array into the correct data type 
      names[i] = scoreInfo[0]; 
      scores[i] = int.Parse(scoreInfo[1]); 
     } 


     Console.WriteLine("The avarage score for this game was {0:N}.", bs.AverageScore(scores, i)); 
     int temp = 0; 

     for (i = 0; i < scores.Length; i++) 
     { 
      for (int j = 0; j < scores.Length - 1; j++) 
      { 
       if (scores[j] > scores[j + 1]) 
       { 
        temp = scores[j + 1]; 
        scores[j + 1] = scores[j]; 
        scores[j] = temp; 
       } 
      } 
     } 

     for (i = 0; i < scores.Length; i++) 
      Console.Write($"{scores[i]}\n"); 




    // sort the array in ascending order 
    // print out lots of messages so we can see the sort work 

    Console.WriteLine(); 
     Console.ReadKey(true); 


     Console.ReadLine(); 
    } 
} 

}

+0

入力された値の数のカウンタを保持します。これをscores.lengthではなくprintout loop limitに使用してください。 – OldProgrammer

+0

カウンターをどうしたらいいですか? –

答えて

1
 List<int> scores = new List<int>(); 
     scores.Add(int.Parse(-your-string-input-)); //Add value to List 
     scores.Min(); //Min Value 
     scores.Max(); //Max Value 
     scores.Average(); //Average 
     scores.Sort(); //Sort the score List 
     scores.Reverse(); //Reverse if necessary 
     scores.Clear(); //Clear the score list 

C#は本当にすべてこのユーティリティであなたを台無しに、私はあなたがプログラマーとしてのあなたのために良いsorting algorithmを学ぶことをお勧めします

関連する問題