double.TryParse() Returns 零 (0) 不应该
double.TryParse() Returns zero (0) when it shouldn't
所以当我处理数字时,一切正常,只是当我使用非数字条目时。 (我在有问题的地方评论)
当您输入任何非数字的内容时,它应该会显示一条错误消息,然后再次提示您输入有效的条目。如果您输入另一个非数字值,tryParse 不会停留在循环中,而是会将值更改为 0,从而退出循环并结束程序。我该如何解决这个问题?
我只想在输入0时退出循环,而不是触发tryParse将字符变为0的字符。
我尝试删除最后的 tryParse,但 if 语句中的第一个条件使其为假,因此直径仍然变为 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);
}
//if userInput isn't valid, diameter becomes 0
如果是这种情况,和
//if userInput isn't valid, diameter becomes 0 and exits loop
那么问题是您在共享内容之外将直径设置为 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();
}
}
}
所以当我处理数字时,一切正常,只是当我使用非数字条目时。 (我在有问题的地方评论)
当您输入任何非数字的内容时,它应该会显示一条错误消息,然后再次提示您输入有效的条目。如果您输入另一个非数字值,tryParse 不会停留在循环中,而是会将值更改为 0,从而退出循环并结束程序。我该如何解决这个问题?
我只想在输入0时退出循环,而不是触发tryParse将字符变为0的字符。
我尝试删除最后的 tryParse,但 if 语句中的第一个条件使其为假,因此直径仍然变为 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);
}
//if userInput isn't valid, diameter becomes 0
如果是这种情况,和
//if userInput isn't valid, diameter becomes 0 and exits loop
那么问题是您在共享内容之外将直径设置为 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();
}
}
}