さて、私は考えられるあらゆる角度からこれを試しました、そして私はいつものように物事を複雑にしすぎていると思います!
購入したアイテムの価格を入力するようにユーザーに要求するコンソールc#アプリケーションを構築しています。次に、この顧客コードに応じて「顧客コード」に対して特定の割引が適用されます。
私はこれにswitchステートメントを使用しましたが、すべてエラーチェックで機能します(whileループを使用して、正しい入力が認識されるまで質問を続けます)これは私が苦労している最後の部分です...コンソールはユーザーにユーザーが間違った入力を入力した場合、必要に応じてユーザーが「N」を入力した場合にもプログラムが終了する場合は、コードにさらにデータを入力します(メインループの先頭に戻ります)。しかし、機能していないのは、ユーザーが「Y」を入力した場合、最初に戻ってさらにデータを入力できるはずですが、これは機能しません=/「ブレーク」を使用しました。出口ループを抜けてメインループに戻るステートメント...
この時点で文字は「Y」なので、メインループはまだ実行されているはずですが、代わりにコンソールは何もしません。カーソルが空白行にあるだけです...入力を要求せず、「キーを押して継続する"。私は可能な限り詳細に説明しましたが、エッセイをお詫びします= / ...以下は私のコードです...うまくいけば、誰かが私が間違っているところを見つけることができます!
更新:メインループを(=='Y')で試し、最初のループを実行するように変数を'Y'に設定したことにも注意してください。また、同じステートメントでdo whileループに変更したので、最初に空白文字で実行され、次に「Y」に変更された場合、ループ条件は除外されているはずです= /
更新:皆さんが私がもっと馬鹿だと思う前に、計算の誤りに気づきました= / LOL
namespace W7Task1
{
class W7Task1
{
// "Main" method begins the execution of the C# application
static void Main(string[] args)
{
char customerCode = '\0';
double initialCost = 0;
string customerType = "";
double finalPrice = 0;
string userInput = "";
char continueChar = '\0';
while (continueChar != 'N')
{
while (initialCost == 0)
{
Console.Write("\nPlease input the cost of the item: ");
userInput = Convert.ToString(Console.ReadLine());
try
{
initialCost = Convert.ToDouble(userInput);
}
catch
{
Console.WriteLine("\nPlease input a number!");
}
}
while (customerCode == '\0')
{
Console.Write("\nPlease input your customer code: ");
userInput = Convert.ToString(Console.ReadLine());
customerCode = Convert.ToChar(userInput);
customerCode = char.ToUpper(customerCode);
switch (customerCode)
{
case 'A':
customerType = "Managerial Staff";
finalPrice = (initialCost / 100) * 30 - initialCost;
Console.WriteLine("\nThe initial cost of the item is: {0:c}\nYour customer type is: {1}\nThe items final price is: {2:c}\n", initialCost, customerType, finalPrice);
break;
case 'B':
customerType = "Sales Staff";
finalPrice = (initialCost / 100) * 20 - initialCost;
Console.WriteLine("\nThe initial cost of the item is: {0:c}\nYour customer type is: {1}\nThe items final price is: {2:c}\n", initialCost, customerType, finalPrice);
break;
case 'C':
customerType = "Account Customers";
finalPrice = (initialCost / 100) * 8 - initialCost;
Console.WriteLine("\nThe initial cost of the item is: {0:c}\nYour customer type is: {1}\nThe items final price is: {2:c}\n", initialCost, customerType, finalPrice);
break;
case 'D':
customerType = "Cash Customers";
finalPrice = (initialCost / 100) * 5 - initialCost;
Console.WriteLine("\nThe initial cost of the item is: {0:c}\nYour customer type is: {1}\nThe items final price is: {2:c}\n", initialCost, customerType, finalPrice);
break;
case 'E':
customerType = "Credit Card/Cheque";
finalPrice = (initialCost / 100) * 0 - initialCost;
Console.WriteLine("\nThe initial cost of the item is: {0:c}\nYour customer type is: {1}\nThe items final price is: {2:c}\n", initialCost, customerType, finalPrice);
break;
default:
Console.WriteLine("\nError Please input a valid Customer Code\n");
customerCode = '\0';
break;
}
}
while (continueChar == '\0')
{
Console.WriteLine("Would you like to input more data?");
userInput = Convert.ToString(Console.ReadLine());
if (char.TryParse(userInput, out continueChar))
{
continueChar = char.ToUpper(continueChar);
if (continueChar == 'Y')
{
break;
}
else if (continueChar == 'N')
{
Console.WriteLine("Thankyou for using this application");
System.Environment.Exit(0);
}
else
{
Console.WriteLine("Please input a 'Y' or 'N'");
continueChar = '\0';
}
}
else
{
Console.WriteLine("Please input a valid character!");
}
}
}
}// End of "Main" method
}// End of "W7Task1" class
}