4

さて、私は考えられるあらゆる角度からこれを試しました、そして私はいつものように物事を複雑にしすぎていると思います!

購入したアイテムの価格を入力するようにユーザーに要求するコンソール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

}

4

3 に答える 3

3

変数をリセットしていません。

ユーザーが終了ループから抜け出したときの変数は次のとおりです。

continueChar == 'Y'
customerCode != '\0'
initialCost != 0

これは、すべてのwhileループがトリガーされないことを意味します。

変数宣言または初期化をメインループ内に移動します。

于 2012-11-19T12:36:01.553 に答える
3

何が起こっているのかというと、ユーザーがを入力した後'Y'、外側のループ(つまり、while (continueChar != 'N'))は無期限にループし続けますがwhile (initialCost == 0)、変数(などinitialCost)は値を保持するため、内側のループ(から始まる)はいずれも条件を満たしません。前の反復で割り当てられたもの。

最も簡単な修正は、すべての変数の初期化を外側のループの内側に移動することです。次のコードを変更します。

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)
        {
        // ...

...に:

static void Main(string[] args)
{
    char continueChar = '\0';

    while (continueChar != 'N')
    {
        char customerCode = '\0';
        double initialCost = 0;
        string customerType = "";
        double finalPrice = 0;
        string userInput = "";

        while (initialCost == 0)
        {
        // ...

編集:メソッドの上部にある変数宣言を保持したい場合は、そのように宣言と初期化を分離することができます。これにより、家庭教師を満足させながら、内部ループの各反復で確実にリセットされます。

static void Main(string[] args)
{
    char customerCode;
    double initialCost;
    string customerType;
    double finalPrice;
    string userInput;
    char continueChar = '\0';

    while (continueChar != 'N')
    {
        customerCode = '\0';
        initialCost = 0;
        customerType = "";
        finalPrice = 0;
        userInput = "";

        while (initialCost == 0)
        {
        // ...
于 2012-11-19T12:36:33.147 に答える
0

デバッガーを使用してアプリケーションをステップ実行しましたか(Visual Studioで[F10]を押します)?

'initialCost'変数を0にリセットしていません。

于 2012-11-19T12:37:16.793 に答える