我的代码首先使用用户输入的“ s”或“ k”或“ c”来显示价格。用户输入硬币价值以降低该价格。当用户输入“ s”和第一个硬币值时,我的代码起作用了,但是在第四次输入后它就关闭了,我不确定为什么。
我尝试了ReadKey和Console.ReadLine,但是我不确定从这里去哪里
namespace VendingMachine
{
class Program
{
static void Main(string[] args)
{
//Gives user necessary info to operate vending machine
Console.WriteLine("Welcome to vending machine.");
Console.WriteLine("We offer you (s)oda, coo(k)ies, and (c)hips");
Console.WriteLine("Please select the product you want to purchase:");
string userselection = Console.ReadLine();
if (userselection == "s")
{
//Generates a random number between 0 and 5 using the random class
Random rn = new Random();
int randomnumber = rn.Next(1, 5);
double lottery = randomnumber * 10;
Console.WriteLine("Congratulations! You win a coupon with " + lottery + " cents.");
//soda price after lottery
double sodaprice = 100 - lottery;
Console.WriteLine("You only need to pay " + sodaprice + " cents");
Console.WriteLine("Please insert a coin of 5, 10, or 25:");
string coininput = Console.ReadLine();
double coin = Convert.ToDouble(coininput);
while (coin > 0)
{
if (coin == 25)
{
double sodapricecoins = sodaprice - coin;
Console.WriteLine("You still owe " + sodapricecoins + " cents.");
Console.WriteLine("Please insert a coin of 5, 10, or 25:");
break;
}
else if (coin == 10)
{
double sodapricecoins = sodaprice - coin;
Console.WriteLine("You still owe " + sodapricecoins + " cents.");
Console.WriteLine("Please insert a coin of 5, 10, or 25:");
break;
}
else if (coin == 5)
{
double sodapricecoins = sodaprice - coin;
Console.WriteLine("You still owe " + sodapricecoins + " cents.");
Console.WriteLine("Please insert a coin of 5, 10, or 25:");
break;
}
}
}
}
}
}
程序应该一直运行,直到达到0或负数为止,我知道我还没有这个部分。
答案 0 :(得分:0)
第一件事是删除三个if中的 break ,但是如果您注意到三个if包含相同的代码。根本不需要它们。
第二点是要有一种退出循环的方法。如果用户输入的硬币金额等于或小于零,则可能是零。
第三点是询问循环中的硬币值,进行数学运算,然后确定是否要继续循环或停止循环。
Console.WriteLine("You only need to pay " + sodaprice + " cents");
// To force to start the loop, we ask the value inside the loop
double coin = 1d;
// We should reach zero or less starting from the initial price (minus lottery)
double amountDue = sodaprice;
while (coin > 0)
{
Console.WriteLine("Please insert a coin of 5, 10, or 25:");
string coininput = Console.ReadLine();
// use tryparse when trying to convert user input in numbers.
// tryparse returns true if the input is a number without raising exceptions
if(double.TryParse(coininput, out coin))
{
// Decrement the amount to pay with the user input
amountDue -= coin;
// Decide for a message and continue the loop or stop
if(amountDue > 0)
Console.WriteLine("You still owe " + amountDue + " cents.");
else
break;
}
}
答案 1 :(得分:0)
如果您希望该程序连续运行直到苏打价格达到0或为负数,请尝试使用此代码,但当您使用硬币> 0的条件进行while循环时,请继续操作,直到用户键入零或负数。
//Gives user necessary info to operate vending machine
Console.WriteLine("Welcome to vending machine.");
Console.WriteLine("We offer you (s)oda, coo(k)ies, and (c)hips");
Console.WriteLine("Please select the product you want to purchase:");
string userselection = Console.ReadLine();
if (userselection == "s")
{
//Generates a random number between 0 and 5 using the random class
double lottery = new Random().Next(1, 5) * 10;
Console.WriteLine("Congratulations! You win a coupon with " + lottery + " cents.");
//soda price after lottery
double sodaprice = 100 - lottery;
Console.WriteLine("You only need to pay " + sodaprice + " cents");
double coin;
do
{
Console.WriteLine("Please insert a coin of 5, 10, or 25:");
if (double.TryParse(Console.ReadLine(), out coin))
{
if (coin == 25 || coin == 10 || coin == 5)
{
sodaprice -= coin;
Console.WriteLine(sodaprice > 0 ? "You still owe " + sodaprice + " cents." : "Finish...");
}
}
} while (sodaprice > 0);
}
Console.ReadLine();