double.TryParse()不应该返回零(0)

时间:2019-10-24 19:16:11

标签: c# loops do-while tryparse

因此,当我处理数字时,一切工作正常,就在我使用非数字条目时。 (我在出现问题的地方加了评论)

输入非数字的任何内容时,它会显示一条错误消息,然后再次提示您输入有效的条目。如果输入另一个非数值,而不是停留在循环中,tryParse会将值更改为0,因此退出循环并完成程序。我该如何解决?

我只想在输入0时退出循环,而不是触发tryParse将字符更改为0的字符。

我尝试最后删除tryParse,但是if语句中的第一个条件将其设为false,因此直径仍然变为0。

    static void Main()
    {
    const double MINIMUM_DIAMETER = 12.0;
    const double MAXIMUM_DIAMETER = 36.0;
    double diameter, radius, area, sliceArea;
    string userInput;

    Console.Write("\nPlease enter the diameter of your pizza: ");
    userInput = Console.ReadLine();
        do
        {
            if (double.TryParse(userInput, out diameter) == false)
            {
                Console.WriteLine("\nENTRY NON-NUMBERIC ERROR\n");
                Console.WriteLine("Pizza must have a numeric diameter. You entered: \"{0}\"\n", userInput);
                Console.WriteLine("Please try again.\n");

                //**if userInput isn't valid, diameter becomes 0**
            }
            else
            {
                if (diameter < MINIMUM_DIAMETER || diameter > MAXIMUM_DIAMETER)
                {
                    //not important
                }
                else
                {
                    //not important
                }
            }
            Console.Write("\nPlease enter the diameter of your pizza (0 to end program): ");
            userInput = Console.ReadLine();
            double.TryParse(userInput, out diameter);
            Console.Clear();

            //**if userInput isn't valid, diameter becomes 0 and exits loop**

        } while (diameter != 0);
    }

2 个答案:

答案 0 :(得分:0)

问题出在double.TryParse方法上。来自Microsoft Docs

  

此方法返回时,如果转换成功,则包含与s参数等效的双精度浮点数;如果转换成功,则为;如果转换失败,则为零。

您可以通过以下方式在代码中克服这一点:

bool success = true;
    do
    {
        if (double.TryParse(userInput, out diameter) == false)
        {
            Console.WriteLine("\nENTRY NON-NUMBERIC ERROR\n");
            Console.WriteLine("Pizza must have a numeric diameter. You entered: \"{0}\"\n", userInput);
            Console.WriteLine("Please try again.\n");
        }
        else
        {
            if (diameter < MINIMUM_DIAMETER || diameter > MAXIMUM_DIAMETER)
            {
                //not important
            }
            else
            {
                //not important
            }
        }
        Console.Write("\nPlease enter the diameter of your pizza (0 to end program): ");
        userInput = Console.ReadLine();
        Console.Clear();
        success = double.TryParse(userInput, out diameter);
    } while (diameter != 0 || !success);

答案 1 :(得分:0)

  

// 如果userInput无效,则直径变为0

如果是这种情况,请

  

// 如果userInput无效,直径变为0并退出循环

那么问题是您要将直径设置为0,而不是共享尺寸?在else中进行0的检查可以保护它免受TryParsing arg的默认0的影响。

上面的

This link是用来查看下面的代码:

using System;

public class Program
{
    public static void Main()
    {
        string userInput;
        double diameter;
        Console.Write("\nPlease enter the diameter of your pizza: ");
        userInput = Console.ReadLine();

        while(true)
        {
            var isDouble = double.TryParse(userInput, out diameter) == true;

            if (!isDouble)
            {
                Console.WriteLine("\nENTRY NON-NUMBERIC ERROR\n");
                Console.WriteLine("Pizza must have a numeric diameter. You entered: \"{0}\"\n", userInput);
                Console.WriteLine("Please try again.\n");
                diameter = 0;
                //**if userInput isn't valid, diameter becomes 0**
            }
            else
            {
                if (diameter == 0)
                {
                    Console.Write("\n Exit initiated");
                    break;
                }
            }

            Console.Write("\nPlease enter the diameter of your pizza (0 to end program): ");
            userInput = Console.ReadLine();
            double.TryParse(userInput, out diameter);
            Console.Clear();
        }
    }
}