我试图这样做,所以我的收据可以输入字母和数字。
decimal counter;
decimal item;
decimal price;
decimal subtotal;
decimal tax;
decimal total;
decimal quantity;
subtotal = 0;
counter = 0;
while (counter <= 10)
{
Console.Write("Item{0}", counter + 1);
Console.Write(" \tEnter item: ");
item = Convert.ToDecimal(Console.ReadLine());
if (item == 0)
break;
Console.Write(" Enter price: ");
price = Convert.ToDecimal(Console.ReadLine());
counter = counter + 1;
Console.Write(" Enter quantity: ");
quantity = Convert.ToInt32(Console.ReadLine());
subtotal += price * quantity;
}
Console.WriteLine("-------------------");
Console.WriteLine("\nNumber of Items:{0}", counter);
Console.WriteLine("Subtotal is {0}", subtotal);
tax = subtotal * 0.065M;
Console.WriteLine("Tax is {0}", tax);
total = tax + subtotal;
Console.WriteLine("Total is {0}", total);
Console.WriteLine("Thanks for shopping! Please come again.");
Console.Read();
例如,假设我买了一个苹果,然后我进入了苹果&#34;在哪里说&#34;输入项目&#34;然后输入数量和价格。然后我决定我要结束它所以我输入0来打破代码。我能够很好地输入数字,但是当我输入一封信时,它不会工作。
答案 0 :(得分:0)
如果您想输入类似Console.Write(" \tEnter item: ");
item = Convert.ToDecimal(Console.ReadLine());
if (item == 0)
break;
的内容,则此代码无效:
decimal
在这里,您尝试将一些符号(而不是数字)解析为Console.Write(" \tEnter item: ");
item = Console.ReadLine();
if (item == "0")
break;
- 因此您应该得到异常。
将您的代码更改为:
item
另外,更改string item; //previously decimal item;
的类型:
{{1}}