我想創建一個C#應用程序,輸入上週銷售的一個銷售人員的物品,並計算並顯示該銷售人員的收入。銷售人員每週收到200美元,加上當周總銷售額的9%。提供了每個銷售人員銷售的物品列表及其值,我如何將用戶輸入轉換爲int?其他一切正常。如何在控制檯應用程序中將用戶輸入解析爲int
double value1, value2, value3, value4;
value1 = 239.99;
value2 = 129.75;
value3 = 99.95;
value4 = 350.89;
Console.Write("Enter number sold of product #1: ");
int item1 = Convert.ToInt32(Console.ReadLine());
Console.Write("Enter number sold of product #2: ");
int item2 = Convert.ToInt32(Console.ReadLine());
Console.Write("Enter number sold of product #3: ");
int item3 = Convert.ToInt32(Console.ReadLine());
Console.Write("Enter number sold of product #4: ");
int item4 = Convert.ToInt32(Console.ReadLine());
double sales1, sales2, sales3, sales4, totalSales;
sales1 = item1 * value1;
sales2 = item2 * value2;
sales3 = item3 * value3;
sales4 = item4 * value4;
totalSales = sales1 + sales2 + sales3 + sales4;
double commission;
int weeklyPay = 200;
commission = (9/100) * totalSales;
double salary;
salary = commission + weeklyPay;
Console.Write("Earnings this week: " + salary);
Console.ReadLine();
'Convert.ToInt32(Console.ReadLine())'將起作用。或'int.Parse(Console.ReadLine())'較短。 –
似乎一切正常,你有任何錯誤? –