C#显示数组的格式化内容

时间:2012-07-15 23:31:31

标签: c# arrays loops

我正在尝试显示一个本身没有问题的数组。但是,我想添加一个If statement,这样如果正在显示的score[]数组的当前迭代次数等于300,那么它将在其后放置一个*。像300*

这样的东西

此外,阵列需要显示从最高到最低的位置,通过在数组中从最低到最高的位置反转显示。我正在考虑使用交换来逆转订单,但如果我不需要,那么我想以这种方式解决它。

到目前为止,我正在

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

    }
}
}

2 个答案:

答案 0 :(得分:2)

在循环中移动if语句应该有效:

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定义的列表或数组而不是两个单独的数组来将玩家名称与其分数相关联。将它们分开将导致由于错误而无法匹配的问题。 (从一个中移除而不是另一个,在一个中排序更改等)

答案 1 :(得分: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]);
 }