【问题标题】:method wont pull correct data from an array [closed]方法不会从数组中提取正确的数据[关闭]
【发布时间】:2013-03-24 22:12:21
【问题描述】:

所以基本上发生的事情是我的代码编译并运行良好。但是当它运行为 minScore 和 minName 变量存储的值时保持 0

static void Main(string[] args)
{
   int count = 0;
   string[] names = new string[MAX_SIZE];
   int[] scores = new int[MAX_SIZE];

   string name;
   int sum = 0;
   int minScore = 0;
   int maxScore = 0;
   string minName = string.Empty;
   string maxName = string.Empty;
   string input;
   int score;

   Console.WriteLine("===============Saturday Night Coders================");
   Console.WriteLine("===============Bowling Score Program================");

   for (int i = 0; i < MAX_SIZE; i++)
   {
      Console.WriteLine("\n Please Enter a name and a score separated by a space");
      Console.WriteLine("Enter a blank line when finished");

      input = Console.ReadLine();

      if (input == "")
      {
         count = i;
         Console.WriteLine("===========INPUT COMPLETE=========");
         break;
      }

      string[] splitInput = input.Split();

      name = splitInput[0];
      score = int.Parse(splitInput[1]);
      scores[i] = score;
      names[i] = name;
      sum += score;
      if (minScore >= score)
      {
         minScore = score;
         minName = name;
      }

      if (maxScore <= score)
      {
         maxScore = score;
         maxName = name;
      }
      count = i + 1;
    }

      double average = sum / count;
      Console.WriteLine("Here are the scores for this game");
      PrintScores(names, scores, count);
      Console.WriteLine("Congratulations {0}, your score of {1} was the highest",maxName, maxScore);
      Console.WriteLine("{0} , your score of {1} was the lowest, Maybe you should find a new hobby",minName, minScore);
      Console.WriteLine("\n The team average was {0:f2}", average);
      Console.WriteLine("Press any key to continue...");
      Console.ReadKey();
    }

    static void PrintScores(string[] names, int[] scores, int count)
    {
        for (int i = 0; i < count; i++)
        {
            Console.Write("{0} \t {1}", names[i], scores[i]);
            if (scores[i] == MAX_SCORE)
            {
                Console.WriteLine("*");
            }
            else
            {
                Console.WriteLine("");
            }
            Console.WriteLine();
        }
    }
}

我似乎无法弄清楚为什么 for 循环适用于 maxName 和 maxScore 语句,但不适用于 minName 和 minScore 语句。非常感谢任何帮助

【问题讨论】:

    标签: c# arrays for-loop


    【解决方案1】:

    代替这段代码:

     int minScore = 0;
     int maxScore = 0;
    

    这是一个更好的使用习惯

     int minScore = int.MaxValue;
     int maxScore = int.MinValue;
    

    所以任何值都小于初始最小值,任何值都大于初始最大值。

    【讨论】:

    • 非常感谢您完全解决了这个问题。至于好的做法,我会确保我这样做。
    【解决方案2】:

    我相信您在将默认 minScore 设置为 0 时可能犯了一个逻辑错误。 如果是这种情况,0 将永远不会高于或等于任何高于 0 的分数:

    if (minScore >= score)
    

    其实是

    if (0 >= score)
    

    请尝试以下方法:

    if (minScore == 0 || minScore >= score)
    

    【讨论】:

    • 虽然你可能需要考虑额外的代码或者使用不同的minScore默认值,如果你的最低分数可以是0。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-07
    • 1970-01-01
    • 1970-01-01
    • 2023-03-30
    • 2020-12-18
    相关资源
    最近更新 更多