英文:
how to make the code not break when the enter key is pressed?
问题
现在,我正在学习C#,并且正在制作一个控制台程序,用于销售水果。借助循环的帮助,我使得如果一个人输入非数字,则循环重新开始。但是事实证明,如果我什么都不输入,只是按回车键,我的代码会出现错误。在这种情况下,我应该怎么办?我该如何更改代码?
// 主要方法
public static void Main()
{
// 购买
float kg = 1000;
string bananas;
float BananaKgPrice = 1.69f;
float bananaWeight = 200;
float WatermelonKgPrice = 3.29f;
double price;
string BuyChoice;
Console.WriteLine("欢迎光临我们的商店!我们销售香蕉和西瓜。");
Console.WriteLine($"一公斤香蕉售价{BananaKgPrice}欧元,一公斤西瓜售价{WatermelonKgPrice}欧元");
bool loopBreakBuyChoice = true;
while (loopBreakBuyChoice)
{
Console.WriteLine("您想购买什么?1 - 香蕉,2 - 西瓜");
BuyChoice = Console.ReadLine();
switch (BuyChoice)
{
case "1":
loopBreakBuyChoice = false;
Console.WriteLine($"每公斤香蕉的价格:{BananaKgPrice}欧元");
bool loopBreakBananaDigits = true;
while (loopBreakBananaDigits)
{
Console.WriteLine("您想购买多少香蕉?");
bananas = Console.ReadLine();//按回车键 + 数字
ConsoleKeyInfo key;
key = Console.ReadKey(true);
if (key.Key == ConsoleKey.Enter)
{
}
else
{
bool DigitsBanana = bananas.All(char.IsDigit);
if (!DigitsBanana)
{
loopBreakBananaDigits = true;
Console.WriteLine("请插入香蕉的数量。");
}
if (DigitsBanana)
{
loopBreakBananaDigits = false;
loopBreakBuyChoice = false;
price = Math.Round(Convert.ToDouble(bananas) * Convert.ToDouble(bananaWeight) / Convert.ToDouble(kg) * Convert.ToDouble(BananaKgPrice), 2);
}
}
}
break;
}
}
}
英文:
So, im learing C# right now, and i was making my console program, which sells you some fruits. with the help of loop, I made it so that if a person enters a non-digit, then the loop starts over. But it turns out, if i type nothing and just press enter, my code gets an error. What should I do in such a situation? How can I change the code?
// Main Method
public static void Main()
{
//buy
float kg = 1000;
string bananas;
float BananaKgPrice = 1.69f;
float bananaWeight = 200;
float WatermelonKgPrice = 3.29f;
double price;
string BuyChoice;
Console.WriteLine("Welcome to our shop! We sell bananas and watermelons.");
Console.WriteLine($"Kilo of bananas costs {BananaKgPrice} euros, kilo watermelons {WatermelonKgPrice} euros");
bool loopBreakBuyChoice = true;
while (loopBreakBuyChoice)
{
Console.WriteLine("What do you want to buy? 1 - bananas, 2 - watermelons");
BuyChoice = Console.ReadLine();
switch (BuyChoice)
{
case "1":
loopBreakBuyChoice = false;
Console.WriteLine($"Price of bananas for kilo.: {BananaKgPrice} euro");
bool loopBreakBananaDigits = true;
while (loopBreakBananaDigits)
{
Console.WriteLine("How much bananas do you want to buy?");
bananas = Console.ReadLine();//press enter +digit
ConsoleKeyInfo key;
key = Console.ReadKey(true);
if (key.Key == ConsoleKey.Enter)
{
}
else
{
bool DigitsBanana = bananas.All(char.IsDigit);
if (DigitsBanana != true)
{
loopBreakBananaDigits = true;
Console.WriteLine("Please insert AMOUNT of bananas.");
}
if (DigitsBanana == true)
{
loopBreakBananaDigits = false;
loopBreakBuyChoice = false;
price = Math.Round(Convert.ToDouble(bananas) * Convert.ToDouble(bananaWeight) / Convert.ToDouble(kg) * Convert.ToDouble(BananaKgPrice), 2);
}
}
}
break;
}
}
}
答案1
得分: 1
我将代码部分保留,只返回翻译后的注释和字符串内容:
public static void Main()
{
//购买
float kg = 1000;
string bananas;
float BananaKgPrice = 1.69f;
float bananaWeight = 200;
float WatermelonKgPrice = 3.29f;
double price;
string BuyChoice;
Console.WriteLine("欢迎光临我们的商店!我们出售香蕉和西瓜。");
Console.WriteLine($"香蕉每千克售价 {BananaKgPrice} 欧元,西瓜每千克售价 {WatermelonKgPrice} 欧元");
bool loopBreakBuyChoice = true;
while (loopBreakBuyChoice)
{
Console.WriteLine("您想购买什么?1 - 香蕉,2 - 西瓜");
BuyChoice = Console.ReadLine();
switch (BuyChoice)
{
case "1":
loopBreakBuyChoice = false;
Console.WriteLine($"每千克香蕉的价格:{BananaKgPrice} 欧元");
bool loopBreakBananaDigits = true;
while (loopBreakBananaDigits)
{
Console.WriteLine("您想购买多少香蕉?");
bananas = Console.ReadLine();//按回车键
ConsoleKeyInfo key;
key = Console.ReadKey(true);
if (key.Key == ConsoleKey.Enter)
{
}
else
{
bool DigitsBanana = bananas.All(char.IsDigit);
if (DigitsBanana != true)
{
loopBreakBananaDigits = true;
Console.WriteLine("请插入香蕉的数量。");
}
if (DigitsBanana == true)
{
loopBreakBananaDigits = false;
loopBreakBuyChoice = false;
price = Math.Round(Convert.ToDouble(bananas) * Convert.ToDouble(bananaWeight) / Convert.ToDouble(kg) * Convert.ToDouble(BananaKgPrice), 2);
}
}
}
break;
}
}
}
希望这对你有所帮助!如果你需要进一步的翻译或解释,请随时提问。
英文:
I pasted your code into mine, but it only works if enter is pressed twice. If you press it 1 time, and then any letter / number, the program will break
public static void Main()
{
//buy
float kg = 1000;
string bananas;
float BananaKgPrice = 1.69f;
float bananaWeight = 200;
float WatermelonKgPrice = 3.29f;
double price;
string BuyChoice;
Console.WriteLine("Welcome to our shop! We sell bananas and watermelons.");
Console.WriteLine($"Kilo of bananas costs {BananaKgPrice} euros, kilo watermelons {WatermelonKgPrice} euros");
bool loopBreakBuyChoice = true;
while (loopBreakBuyChoice)
{
Console.WriteLine("What do you want to buy? 1 - banans, 2 - watermelons");
BuyChoice = Console.ReadLine();
switch (BuyChoice)
{
case "1":
loopBreakBuyChoice = false;
Console.WriteLine($"Price of bananas for kilo.: {BananaKgPrice} euro");
bool loopBreakBananaDigits = true;
while (loopBreakBananaDigits)
{
Console.WriteLine("How much bananas do you want to buy?");
bananas = Console.ReadLine();//press enter
ConsoleKeyInfo key;
key = Console.ReadKey(true);
if (key.Key == ConsoleKey.Enter)
{
}
else
{
bool DigitsBanana = bananas.All(char.IsDigit);
if (DigitsBanana != true)
{
loopBreakBananaDigits = true;
Console.WriteLine("Please insert AMOUNT of bananas.");
}
if (DigitsBanana == true)
{
loopBreakBananaDigits = false;
loopBreakBuyChoice = false;
price = Math.Round(Convert.ToDouble(bananas) * Convert.ToDouble(bananaWeight) / Convert.ToDouble(kg) * Convert.ToDouble(BananaKgPrice), 2);
}
}
}
break;
}
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论