如何在c#中捕获int[5]数组中的五个连续数字?

本文关键字:五个 数字 连续 数组 int | 更新日期: 2023-09-27 18:17:00

我编写了这个小程序,它捕获在控制台中连续输入的五个整数。除了一件事:
我没有找到一种方法来接受0作为输入的数字之一。
当然,使用另一种集合类型的解决方案很容易。但这里的挑战是用一个包含五个整数的数组来做。
我尝试设置一个布尔标志"zeroEntered",尝试使用计数器,尝试向后计数j。这一切都不起作用。
也许这是不可能的?有人能确定吗?
下面是代码:

class Program
{
    static void Main(string[] args)
    {
    #region Catch5IntegerInArrayOfInt[5]
        // I try to catch five integers in an array of int[5] 
        // This works as expected except I cannot catch 0 as one of the numbers
        // Cannot wrap my head around this one it seems
        // because all ints are initialized with 0
        Console.WriteLine("Please enter five unique numbers consecutively.");
        int[] fiveNumbers = new int[5];          // do it using an array just the same (as collections were not part of the lectures so far)
        for (int i = 0; i < fiveNumbers.Length; i++)
        {
            Console.WriteLine("Please enter your {0} number:", (Countables)i);
            CatchUsersNumbers(fiveNumbers, i);
        }
        DisplayResult(fiveNumbers);
        Console.WriteLine("'n");
    }
    #endregion
    #region HelperMethods
    private static bool CheckWhetherInteger(string userInput)
    {
        bool result = Int32.TryParse(userInput, out myInteger);
        if (result == false)
        {
            Console.Clear();
            Console.WriteLine("You did not enter an integer.");
        }
        return result;
    }
    private static bool CheckUniqueness(int[] fiveNumbers, int userInput)
    {
        for (int i = 0; i < fiveNumbers.Length; i++)
        {
            if (userInput == 0)
            {
                for (int j = i ; j <fiveNumbers.Length; j--)
                {
                    if (j == 0)
                        break;
                    if (fiveNumbers[j] == 0)
                    {
                        return false;
                    }
                }
             }
            else if (fiveNumbers[i] == userInput)
            {
               return false;
            }
        }
        return true;
    }
    private static void CatchUsersNumbers(int[] fiveNumbers, int i)
    {
        while (true)
        {
            userInput = Console.ReadLine().Trim();
            if (CheckWhetherInteger(userInput) && CheckUniqueness(fiveNumbers, myInteger))
            {
                fiveNumbers[i] = myInteger;
                break;
            }
            else
                Console.Clear();
            Console.WriteLine("You did not enter a unique integer number, try again...");
        }
    }
    private static void DisplayResult(int[] fiveNumbers)
    {
        Console.Clear();
        Array.Sort(fiveNumbers);
        Console.WriteLine("These are the five interger numbers you entered 'nand that were stored in the array:'n");
        for (int i = 0; i < fiveNumbers.Length; i++)
        {
            if (i != fiveNumbers.Length - 1)
                Console.Write(fiveNumbers[i] + ", ");
            else
                Console.Write(fiveNumbers[i]);
        }
    }
    #endregion
    #region Class Variables
    private static int myInteger = 0;
    private static string userInput;
    private enum Countables
    {
        first = 0,
        second,
        third,
        fourth,
        fifth
    }
    #endregion
}

谢谢。

如何在c#中捕获int[5]数组中的五个连续数字?

这是可能的,但是你的5个整型数组将被初始化为5个零,所以当扫描唯一性时,你的检查失败了,特别是因为这段代码:

if (fiveNumbers[j] == 0)
{
  return false;
}

因此,不需要循环遍历整个数组,而应该保留一个计数器来跟踪数组中已经有多少项。然后,在执行检查时,只检查到该索引,而不包括检查中的其他项,因为它们包含0,但您应该将它们视为未初始化。

您也可以使用其他数据类型来解决这个问题。例如,您可以创建一个可空整数数组,这样您就可以实际检查一个项是否已经有值了。或者(也许是最好的解决方案)您可以使用List而不是array。

您唯一的错误是int.TryParse()将0视为无效,您可以使用另一个if语句来处理异常,但这看起来不太干净

 private static bool CheckWhetherInteger(string userInput)
{
    if (userInput == "0")
    {
       myInteger = 0;
       return true 
    }
    else
    {
        bool result = Int32.TryParse(userInput, out myInteger);
        if (result == false)
            {
            Console.Clear();
            Console.WriteLine("You did not enter an integer.");
        }
    }
    return result;
}

我只是发布解决方案-使用可空整数-根据Golez Trol的建议。在这里,如果有人感兴趣的话:

class Program
{
    static void Main(string[] args)
    {
    #region Catch5IntegerInArrayOfInt[5]
        // The solution to catching five integers in an array of int[5] 
        // is to use nullable integers.
        // Keeping a counter when entering an integer to the array does not appeal to me.
        // With normal integers I cannot catch 0 as one of the numbers
        // because all ints are initialized with 0
        Console.WriteLine("Please enter five unique numbers consecutively.");
        var fiveNumbers = new int?[5];          // do it using an array just the same (as collections were not part of the lectures so far)
        for (int i = 0; i < fiveNumbers.Length; i++)
        {
            Console.WriteLine("Please enter your {0} number:", (Countables)i);
            CatchUsersNumbers(fiveNumbers, i);
        }
        DisplayResult(fiveNumbers);
        Console.WriteLine("'n");
    }
    #endregion
    #region HelperMethods
    private static void CatchUsersNumbers(int?[] fiveNumbers, int i)
    {
        while (true)
        {
            userInput = Console.ReadLine().Trim();
            if (CheckWhetherInteger(userInput) && CheckUniqueness(fiveNumbers, myInteger))
            {
                fiveNumbers[i] = myInteger;
                break;
            }
            else
            {
                Console.Clear();
                Console.WriteLine("You did not enter a unique integer number, try again...");
            }
         }
    }
    private static bool CheckWhetherInteger(string userInput)
    {
            bool result = Int32.TryParse(userInput, out myInteger);
            if (result == false)
            {
                Console.Clear();
                Console.WriteLine("You did not enter an integer.");
            }
            return result;
    }
    private static bool CheckUniqueness(int?[] fiveNumbers, int userInput)
    {
        for (int i = 0; i < fiveNumbers.Length; i++)
        {
            if (fiveNumbers[i] == userInput)
            {
               return false;
            }
        }
        return true;
    }
    private static void DisplayResult(int?[] fiveNumbers)
    {
        Console.Clear();
        Array.Sort(fiveNumbers);
        Console.WriteLine("These are the five interger numbers you entered 'nand that were stored in the array:'n");
        for (int i = 0; i < fiveNumbers.Length; i++)
        {
            if (i != fiveNumbers.Length - 1)
                Console.Write(fiveNumbers[i] + ", ");
            else
                Console.Write(fiveNumbers[i]);
        }
    }
    #endregion
    #region Class Variables
    private static int myInteger = 0;
    private static string userInput;
    private enum Countables
    {
        first = 0,
        second,
        third,
        fourth,
        fifth
    }
    #endregion
}

谢谢你的提示-我真的卡住了