TryParse使值= 0并使我不希望退出循环,如何解决此问题?

问题描述 投票:-1回答:2

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

当您输入不是数字的任何内容时,它会显示一条错误消息,然后再次提示您输入有效的条目。如果输入另一个非数值,而不是停留在循环中,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);
    }
c# loops do-while tryparse
2个回答
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);

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();
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.