k.lva k.lva - 2 months ago 5
C# Question

The program needs to stop when the user enter 209,312,414

I need the program to stop when the user enter 209, 312, 414. My while loop is not working because the program continue. I made different other way but at the end the loop continue it does not stop when I enter 209, 312, 414. I will take any other suggestion.

using System;

public class program
{
public static void Main()
{
const double PRICE209 = 12.99, PRICE312 = 16.77, PRICE414 = 109.07;

double price;
int stockNum;

Console.Write("Please enter stock number. ");
stockNum = Convert.ToInt32(Console.ReadLine());

while ((stockNum == 209) || (stockNum == 312) || (stockNum == 414))
{
if ((stockNum == 209) || (stockNum == 312) || (stockNum == 414))
{
if (stockNum == 209)
{
price = PRICE209;
Console.WriteLine("The price for item # {0} is {1}", stockNum, price.ToString("C"));
}
else if (stockNum == 312)
{
price = PRICE312;
Console.WriteLine("The price for item # {0} is {1}", stockNum, price.ToString("C"));
}
else if (stockNum == 414)
{
price = PRICE414;
Console.WriteLine("The price for item # {0} is {1}", stockNum, price.ToString("C"));
} // end of the else if statement
} // end the if statement
Console.Write("Please enter stock number. ");
stockNum = Convert.ToInt32(Console.ReadLine());
} // end the while loop
Console.WriteLine();
Console.WriteLine("press <enter> to terminate program");
Console.ReadLine();
}
}

Answer

Change your while loop to:

while ((stockNum != 209) && (stockNum != 312) && (stockNum != 414))
Comments