无法将类型为“string”的值隐式转换为“bool”

12

可能是重复问题:
帮助转换类型 - 无法隐式转换类型'string'到'bool'

我有以下的代码:

private double Price;
private bool Food;
private int count;
private decimal finalprice;

public void Readinput()
{
    Console.Write("Unit price:  ");
    Price = Console.ReadLine();

    Console.Write("Food item y/n:  ");
    Food = Console.ReadLine();

    Console.Write("Count:  ");
    count = Console.ReadLine();
}

private void calculateValues()
{
    finalprice = Price * count;
}

我想执行以下代码:

但是出现了以下错误:

无法将类型'string'隐式转换为'bool'

无法将类型'string'隐式转换为'double'

无法将类型'string'隐式转换为'int'

无法将类型'double'隐式转换为'decimal'。可以进行显式转换(是否缺少强制转换?)

我知道这些错误的含义,但不知道如何解决。


1
嘿,为什么投票数下降了? - Ivo
21
我认为这个问题不值得那么多的踩。给他指路会更有帮助。 - Bastardo
5
我没有投反对票,但我猜想这是因为这个问题的答案非常简单,导致投反对票的人认为问题提出者在发布之前并没有自己尝试过。很难说他们错了。 - Steve B
4
我认为这个想法是帮助并给出方向。回答一个重复问题的链接就足够了。 - Ivo
@Styne666 这不是完全重复的。 - Esoteric Screen Name
@Esoteric 没错,撤回评论。 - Samuel Harmer
5个回答

24

使用 bool.Parsebool.TryParse 方法将字符串值转换为布尔值。

Price = double.Parse(Console.ReadLine());
Food =bool.Parse(Console.ReadLine());
count = int.Parse(Console.ReadLine());

你不能将 "y" 或 "n" 的值转换为布尔值,而是应该将其作为字符串接收,如果是 "y" 则存储 true,否则存储 false

Console.Write("Food item y/n:  ");
string answer = Console.ReadLine();
if(answer=="y")
   Food=true;
else
   Food=false;
或者(@Mr-Happy的建议)
 Food = answer == "y"

在计算finalprice时,您需要指定明确的类型转换。

private void calculateValues()
{
   // convert double result into decimal.
    finalprice =(decimal) Price * count;
}

1
你可能想使用 Food = answer == "y"; - dvdvorle

5

你需要使用静态类Convert将从控制台读取的内容(即字符串)转换为实际类型。例如:

Console.Write("Count:  ");
count = Convert.ToInt32(Console.ReadLine());

如果给定的参数无法转换,则会导致崩溃,但这并不是您目前的主要问题,因此让我们保持简单。


2
Console.Write("Unit price:  ");
double.TryParse(Console.ReadLine(), out Price);

Console.Write("Food item y/n:  ");
bool.TryParse(Console.ReadLine(), out Food);

Console.Write("Count:  ");
int.TryParse(Console.ReadLine(), out count);

0
private double Price;
private bool Food;
private int count;
private decimal finalprice;

public void Readinput()
{
    Console.Write("Unit price:  ");
    double.TryParse(Console.ReadLine(), out Price);

    Console.Write("Food item y/n:  ");
    bool.TryParse(Console.ReadLine(), out Food);

    Console.Write("Count:  ");
    int.TryParse(Console.ReadLine(), out count);
}

private void calculateValues()
{
    finalprice = Price * count;
}

1
如果您不捕获结果,使用tryparse是无用的。您如何知道出了什么问题?要么使用经典的Parse并抛出异常,要么使用if(!tryparse(..)) error handling包装tryparse。 - Steve B
确实,当您捕获错误时会更好,但应用程序不会崩溃并使用双精度= 0,布尔值= false和整数= 0的默认值。 - Wouter Janssens
所以这取决于业务需求...程序是否应该通知用户他输入了错误的内容?我认为答案是肯定的。答案应该是肯定的。我的观点是,静默错误比应用程序崩溃更危险(它可能会导致后续问题)。 - Steve B

0

您需要将Console.ReadLine()调用包装在适当的解析器函数中,因为(与例如PHP不同),C#是一种静态类型语言,此外只有那些可以保证安全且无损转换的值才可以被隐式地转换:

Price = double.Parse(Console.ReadLine());

Console.Write("Food item y/n:  ");
// I think you want the user to type in "y", "Y", "n" or "N", right?
Food = Console.ReadLine().ToUpper() == "Y";

Console.Write("Count:  ");
count = int.Parse(Console.ReadLine());

在你的计算方法中,你必须明确地将得到的double转换为十进制数,因为C#不支持固定点和浮点值之间的隐式转换。
finalprice = (decimal)(Price * count);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接