首页 > 解决方案 > 将自动售货机界面构建为控制台应用程序但出现错误 - 无法将双精度值转换为布尔值

问题描述

    static void Main(string[] args)
    {
        double creditsvalue;
        string inputString;

        double totalvalue;
        string InputString;

        double checkout;
        string INputString;

        Console.WriteLine("Hi");Console.WriteLine("The items and their prices are as follows:");
        Console.WriteLine("Chocolate bar at 0.80");
        Console.WriteLine("Soda can at 0.70"); 
        Console.WriteLine("Soda bottle at 1.25"); 
        Console.WriteLine("Crisps at 0.50");
        Console.WriteLine("Cookies at 1.10");
        
        Console.WriteLine("How many credits to you want to add to your account?"); 
        inputString = Console.ReadLine();
        creditsvalue = double.Parse(inputString);
        Console.WriteLine("Is this amount {0} correct", creditsvalue);
        Console.ReadLine();

        Console.WriteLine("Which items would you like to have?");
        InputString = Console.ReadLine();
        totalvalue = double.Parse(InputString);
        Console.WriteLine("The total is {0}", totalvalue);

        double accountvalue = creditsvalue - totalvalue;

        Console.WriteLine("Would you like to checkout or add more things?");
        INputString = Console.ReadLine();
        checkout = double.Parse(INputString);

        if (checkout);

    }

这是我到目前为止的代码,请帮帮我,因为我真的被卡住了。它说如果我尝试将其更改为布尔值,我无法将双精度值转换为布尔值并返回。

标签: c#.net

解决方案


首先,将类型checkout从 double 更改为 bool:

bool checkout;

然后把解析逻辑改成这样

Console.WriteLine("Would you like to checkout or add more things?");
INputString = Console.ReadLine();
checkout = INputString.ToLower().Equals("yes");

if (checkout) {
   // Your code here
}

注意:在代码中,我们希望用户在结帐时写“是”。
如果您想输入其他内容,只需将该单词替换为您想要的单词即可。


推荐阅读