2012-07-15 64 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]); 
}