2012-07-15 56 views
0

我想顯示一個數組本身沒有問題。但是,我想添加一個If statement,這樣如果正在顯示的score[]陣列的當前迭代等於300,那麼它將在其後面放置一個*。就像300*C#顯示陣列的格式化內容

此外陣列需要顯示從最高到最低,我正在通過顛倒顯示在陣列中的最低點到最高點。我正在考慮使用交換來改變順序,但如果我不必那麼我想這樣解決它。

到目前爲止,我得到

400 
332 
300* 
300 

或以另一種方式我試過了,我得到了

0 
0 
300* 
300 
250 
221 

我只具有顯示和輸出的問題。

static void Output(int iteration, int[] score, string[] player, double average) 
    { //opening output 
     Console.WriteLine("\n\t****** OUTPUT ******"); 
     Console.WriteLine("\nScores for this game.\n"); 

     if (score[iteration - 1] == 300) 
     { 
      Console.WriteLine("{0} score was {1}*", player[iteration - 1], score[iteration - 1]); 
     } 

     for (int i = iteration; i <= MAX_SIZE_ARRAY; i--) 
     {    
       //iterates through the loop to display all the players name then score 
       Console.WriteLine("{0} score was {1}", player[i], score[i]); 
     } 
     //displays high, low, and average score 
     Console.WriteLine("\nThe high score was {0} with {1} points", player[iteration - 1], score[iteration - 1]); 
     Console.WriteLine("The low score was {0} with {1} points", player[0], score[0]); 
     Console.WriteLine("The team average score was {0}", average); 

    } 
} 
} 
+0

你只需要移動你的if語句中的for循環? – Greg 2012-07-15 23:35:08

+0

我確實嘗試過,但它也給出了不理想的結果 – Dan 2012-07-15 23:37:30

+0

您能否提供一個數據樣本?另外在你的for循環中它是否需要i> 0? – Greg 2012-07-15 23:44:47

回答

2

移動如果在循環中聲明應該工作:

for (int i = iteration; i <= MAX_SIZE_ARRAY; i--) 
{    
    //iterates through the loop to display all the players name then score 
    if (score[iteration - 1] == 300) 
    Console.WriteLine("{0} score was {1}*", player[iteration - 1],         score[iteration - 1]); 
    else 
    Console.WriteLine("{0} score was {1}", player[i], score[i]); 
} 

我想這是一個作業,使保齡球評分系統?一個建議是通過使用KeyValuePair,Tuple或者你自己的Struct定義的列表或數組而不是兩個單獨的數組來鏈接玩家名和他們的分數。讓他們分開將導致他們由於錯誤而無法匹配的問題。 (從一個被移除的,而不是在一個其他的,排序的變化等)

+0

它被列爲家庭作業 – Dan 2012-07-16 01:33:54

0

檢查score[i] == 300內循環:

for (int i = iteration; i <= MAX_SIZE_ARRAY; i--) 
{    
    if (score[i] == 300) 
    Console.WriteLine("{0} score was {1}*", player[i], score[i]); 
    else 
    Console.WriteLine("{0} score was {1}", player[i], score[i]); 
}