【问题标题】:User enters size of array, numbers in array and searches for numbers in the array用户输入数组的大小、数组中的数字并在数组中搜索数字
【发布时间】:2016-03-22 06:24:49
【问题描述】:

我正在通过书本自学 C#,我需要一些帮助。我想创建一个控制台程序,用户在其中输入一个数字作为浮点数数组的大小。然后程序会提示用户搜索一个(浮点数)并告诉用户它是否存在于数组中。

我遇到的问题是它还不接受浮点数(目前只有整数有效)并且它只会找到输入的第一个数字。

这是我目前写的代码:

    public static void Main()
    {
        int size;
        int i;

        size = readNumber("How many numbers do you want to enter: ");
        float[] numbers = new float[size];

        for (i = 0; i < size; i++)
            numbers[i] = readFloat("Enter a number: ");
        Console.WriteLine();


        Console.WriteLine("Please enter any number to check if it exists in the array: ");
        float userInput = float.Parse(Console.ReadLine());
        for (i = 0; i < size; i++)
        {
            if (userInput == numbers[i])
            {
                Console.Write("Congratulations! The number you entered exists in the array.");
                break;
            }
            else
            {
                Console.Write("Sorry. The number you entered does not exist in the array.");
                break;
            }
        }

            Console.ReadLine();
    }

    private static float readFloat(string question)
    {
        Console.Write(question);
        string ans = Console.ReadLine();
        float number = float.Parse(ans);
        return number;
    }

    private static int readNumber(string question)          
    {
        Console.Write(question);
        string ans = Console.ReadLine();
        int number = int.Parse(ans);
        return number;
    }
}

【问题讨论】:

  • 你用的是什么ide?因为您是编程新手,我强烈建议您调试程序并查看错误发生的位置。
  • 通过考虑循环中的 if 语句可以发现一个非常明显的错误。尝试通过想象一些输入来推理代码,并尝试一路单步执行程序。你不必走得太远(提示:它与范围和流量控制有关)
  • 如果输入浮点数会怎样? kai 的评论应该可以解决您的第二个问题。
  • 对于浮动问题,您是否尝试过输入2.52,5,这取决于您的文化设置,错误的会导致25
  • 你得到了答案。 :)

标签: c# arrays


【解决方案1】:

我遇到的问题是它还不接受浮点数(仅 整数工作到目前为止)并且它只会找到第一个数字 输入。

不清楚您所说的不接受浮点数是什么意思。如果您在尝试读取解析您的浮点值时遇到任何异常,您必须告诉我们您的输入示例和您的CurrentCulture。除此之外,我们无法帮助您处理这种情况。

你说;

"对于浮动问题,您是否尝试输入 2.5 或 2,5 取决于 在您的文化设置中,错误的设置将导致 25" . 2,5 作品 (显示 2,5)但 2.5 会导致未处理的异常。

太正常了。由于float.Parse(string) 使用您的CurrentCulture 设置默认情况下,并且看起来像您的CurrentCulture 使用, 作为NumberDecimalSeparator,这就是您成功将2,5 解析为@987654333 的原因@ 但你得到 2.5 的例外。

由于您将break 用于ifelse 部分,因此在第一次迭代后它将退出for 循环。

$8.9.1 起 break 声明部分;

break 语句退出 最近 包围 switch, while, do, forforeach 声明。

由于要读取所有元素,因此需要删除这些 break 语句。

如果您使用 Visual Studio,您会收到i++ 部分的警告,因为检测到无法访问的代码

你的 for 循环对我来说也没有太大意义。您为数组中的每个项打印"Congratulations..""Sorry.." 字符串。我假设您只想显示这些字符串之一,您可以将Contains method 用于您的数组,例如;

if(numbers.Contains(userInput))
{
   Console.Write("Congratulations! The number you entered exists in the array.");
}
else
{
   Console.Write("Sorry. The number you entered does not exist in the array.");
}

【讨论】:

  • 我已经删除了 break 语句,但是如果我为 eaxmple 输入四个数字,它会打印一次“Congratulations!...”和“Sorry...”三次。
  • @BadtotheClone 阅读我的最后一段。由于您使用 for 循环迭代所有元素,因此打印四次是正常的。你不需要这样做。只需使用if(numbers.Contains(userInput)) 检查它,如果用户输入是否在您的数组中,您就可以打印您的消息。这正是我最后一个代码部分所做的。
【解决方案2】:

您不需要else 语句,除非您想获得除查找匹配浮点数之外的进一步操作。

在你的 else 语句中,break 实际上是在做它的工作并打破语句并退出范围。

if (userInput == numbers[i])
            {
                Console.Write("Congratulations! The number you entered exists in the array.");
                break;
            }
            else
            {
                Console.Write("Sorry. The number you entered does not exist in the array.");
                //break; REMOVE THIS LINE
            }

那么它应该可以正常工作!

除了逻辑错误,也如cmets中提到的@AntiHeadshot:

根据您的文化设置输入 2.5 或 2,5 是错误的 将导致 25

【讨论】:

  • 我不明白你的意思..这已经是他想要的了!
  • 你有这个数组,例如 1.1 和 1.3 和 3.6 和 5.2 ,你的解决方案会找到 5.2 ;然后开始搜索输出将是“对不起,您输入的号码不存在”三遍,最后写“恭喜号码存在”好吗?
  • @nik 是的,这就是为什么他有一个 else 声明!否则他只会使用 if 语句。
【解决方案3】:

你可以使用

float.Parse(ans, CultureInfo.InvariantCulture.NumberFormat);

祝你好运

【讨论】:

  • 我试过了,但不幸的是它说:转换不包含'ToFloat'的定义
猜你喜欢
  • 1970-01-01
  • 2012-03-19
  • 2015-01-28
  • 1970-01-01
  • 1970-01-01
  • 2018-08-06
  • 1970-01-01
  • 1970-01-01
  • 2023-03-13
相关资源
最近更新 更多