0

我正在尝试显示一个本身没有问题的数组。但是,我想添加一个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);

    }
}
}
4

2 回答 2

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

于 2012-07-15T23:53:49.917 回答
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]);
 }
于 2012-07-16T01:47:04.757 回答