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;
}
}
}
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;
}
}
}
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments