我只是想要一些帮助。抱歉,我是C#编程的新手。我的问题是,当我输入一个值时,假设我输入2,我想打印出:(到目前为止,已插入:23个中的0个。)但是发生的是它显示的苏打水值是30而不是23
namespace VendingMachine
{
class Program
{
static void Main(string[] args)
{
int itemPrice = 0;
int moneyAvailable = 0;
int change = 0;
int soda = 30;
int juice = 23;
int water = 15;
string userChoice;
// Menu
Console.WriteLine();
Console.WriteLine("1. Soda = P30");
Console.WriteLine("2. Juice = P23");
Console.WriteLine("3. Water = P15");
Console.WriteLine("4. Exit");
Console.WriteLine();
// Values entered by the user
Console.Write("Your choice: ");
userChoice = Console.ReadLine();
Console.WriteLine("================================");
// THE MAIN PROBLEM
if (itemPrice < soda)
{
Console.WriteLine($"Inserted so far: P0 out of P{soda}");
}
Console.Write("Enter the amount of Money!: P");
moneyAvailable = int.Parse(Console.ReadLine());
switch (userChoice)
{
case "1":
itemPrice = itemPrice + soda;
break;
case "2":
itemPrice = itemPrice + juice;
break;
case "3":
itemPrice = itemPrice + water;
break;
default:
Console.WriteLine("Invalid. Choose 1, 2 or 3.");
break;
}
答案 0 :(得分:2)
您使用的string interpolation变量名称错误
if (itemPrice < soda)
{
Console.WriteLine($"Inserted so far: P0 out of P{itemPrice}");
// ^^^^^^^^^^ Problem is here
}
您应该每次打印itemPrice
的值,而不是打印苏打的值。
此打印语句,如果条件应转至 switch语句
类似
Console.Write("Enter the amount of Money!: P");
moneyAvailable = int.Parse(Console.ReadLine());
switch (userChoice)
{
case "1":
itemPrice = itemPrice + soda;
break;
case "2":
itemPrice = itemPrice + juice;
break;
case "3":
itemPrice = itemPrice + water;
break;
default:
Console.WriteLine("Invalid. Choose 1, 2 or 3.");
break;
}
if (itemPrice < soda)
{
Console.WriteLine($"Inserted so far: P0 out of P{itemPrice}");
}
答案 1 :(得分:0)
字符串输出中的变量错误:{soda}
而不是{itemPrice}
答案 2 :(得分:0)
对于您而言,您必须在if (itemPrice < soda)
之后检查switch (userChoice)
答案 3 :(得分:0)
这就是您想要做的:
class Program
{
static void Main(string[] args)
{
List<Item> items = new List<Item>()
{
new Item
{
Name = "Soda",
Price = 30
},
new Item
{
Name = "Juice",
Price = 23
},
new Item
{
Name = "Water",
Price = 15
}
};
while (true)
{
//MENU
Log("Available Items:");
for(int i = 0; i < items.Count; i++)
{
Log("{0}. {1} = P{2}", i, items[i].Name, items[i].Price);
}
Console.WriteLine();
bool isInputValid = false;
string userChoice = string.Empty;
int chosenIndex = 0;
while (isInputValid == false)
{
try
{
Log("Choose your Item:");
userChoice = Console.ReadLine();
chosenIndex = int.Parse(userChoice);
if(chosenIndex < 0 || chosenIndex >= items.Count)
{
throw new ArgumentOutOfRangeException();
}
isInputValid = true;
}
catch
{
Log("Invalid choice!");
}
}
Item chosenItem = items[chosenIndex];
bool isPriceReached = false;
string userInsertedMoney = string.Empty;
int insertedMoney = 0;
while (isPriceReached == false)
{
try
{
Log("P{0} of P{1} needed Money for {2} inserted, waiting for more...", insertedMoney, chosenItem.Price, chosenItem.Name);
userInsertedMoney = Console.ReadLine();
insertedMoney += int.Parse(userInsertedMoney);
isPriceReached = insertedMoney >= chosenItem.Price;
}
catch
{
Log("Invalid money!");
}
}
Log("Here is your {0} with a rest of {1} money.{2}", chosenItem.Name, insertedMoney - chosenItem.Price, Environment.NewLine);
}
}
private static void Log(string message, params object[] args)
{
Console.WriteLine(string.Format(message, args));
}
}
public class Item
{
public string Name { get; set; }
public int Price { get; set; }
}