C#初学者求助,下面是题目以及我自己写的代码,求指点!还有如何在输入一个数据后实现不换行

2025-06-27 11:21:45
推荐回答(1个)
回答1:

using System;
using System.Collections.Generic;
using System.Text;
namespace racing_Test
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Begin!");
            int horseCount = 0;//马匹数量
            int[] speed1 = null;//速度1
            int[] speed2 = null;//速度2
            string temp = "";
            do
            {
                temp = Console.ReadLine().Trim();
                if (temp == "0")//任何时候输出"0"行,退出程序
                {
                    return;
                }
                //取马匹的数量
                if (horseCount == 0)
                {
                    if (!int.TryParse(temp, out horseCount))
                    {
                        Console.WriteLine("请正确输入每组的马匹数量!");
                    }
                    continue;
                }
                //取第一组马的速度
                if (speed1 == null || speed1.Length < 1)
                {
                    if (!getSpeed(horseCount, temp, out speed1))
                    {
                        Console.WriteLine("请正确输入第一组马的速度数据!");
                    }
                    continue;
                }
                //取第二组马的速度
                if (speed2 == null || speed2.Length < 1)
                {
                    if (!getSpeed(horseCount, temp, out speed2))
                    {
                        Console.WriteLine("请正确输入第二组马的的速度数据!");
                        continue;
                    }
                }
                //输出比较结果
                int count = 0;
                for (int i = 0; i < horseCount; i++)
                {
                    //这里要注意:如果两马的速度相等,要判谁赢?
                    if (speed1[i] > speed2[i])
                    {
                        count++;
                    }
                }
                //输出结果
                if (count > (horseCount / 2))
                {
                    Console.WriteLine("YES");
                }
                else
                {
                    Console.WriteLine("NO");
                }
                //清理变量,以备下一次使用
                horseCount = 0;
                speed1 = speed2 = null;
            } while (true);
        }
        /// 
        /// 取马的速度
        /// 

        /// 马匹数量
        /// 取数量的字符串
        /// OUT 速度
        /// 正确取出速度返回True,出错返回False
        private static bool getSpeed(int n, string tempString, out int[] speed)
        {
            string[] _sp;
            _sp = tempString.Split(' ');
            if (_sp.Length != n)
            {
                speed = null;
                return false;
            }
            speed = new int[n];
            for (int i = 0; i < _sp.Length; i++)
            {
                int _it;
                if (!int.TryParse(_sp[i], out _it))//速度必须为整数
                {
                    if (_it <= 0)//速度必须大于0
                    {
                        speed = null;
                        return false;
                    }
                }
                speed[i] = _it;//到此可以取值了
            }
            return true;
        }
    }
}