c# - 将自动售货机界面构建为控制台应用程序但出现错误 - 无法将双精度值转换为布尔值
问题描述
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);
}
这是我到目前为止的代码,请帮帮我,因为我真的被卡住了。它说如果我尝试将其更改为布尔值,我无法将双精度值转换为布尔值并返回。
解决方案
首先,将类型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
}
注意:在代码中,我们希望用户在结帐时写“是”。
如果您想输入其他内容,只需将该单词替换为您想要的单词即可。
推荐阅读
- reactjs - Reactjs重定向路由不起作用但url更新
- jquery - 在rails 6中使用jquery进行用户名和电子邮件实时验证
- javascript - 如何在移动设备上显示系统共享按钮?并在相同条件下隐藏桌面共享按钮
- rust - 如何在 Yew 应用程序中创建网格和单元格(使用 Rust 程序)
- reactjs - 如何在 Ionic React 中使用 Chart.js?
- collections - 使用 AutoMapper 转换为只读集合
- javascript - 在垫输入(角度)中搜索没有国家代码的联系人
- html - 如何将 css 代码从网站复制到我的 tumblr 博客
- r - 使用“pander”居中列名,同时左对齐内容
- java - 过滤表中的数据 - 如何正确组织它