1

C#コンソールアプリケーションを閉じようとしています(デバッグなしで実行しています)が、何も機能していません...さまざまな終了コードで通常の容疑者をすべて試しましたが、何も終了していません=/オプションがネストされた束にあるためですifステートメント?それはおそらく私が見逃している本当に単純なものですが、私の脳を傷つけているので、誰か助けてください!私はもう試した :

System.Environment.Exit(0);
System.Environment.Exit(1);
System.Environment.Exit(-1);
return;
Application.Exit(); //(wont even except it)

コンテキストが役立つ場合は、ネストされたifステートメントを使用して、ユーザーが数字を入力したか、文字「q」を入力したかを確認しました。数字を入力した場合は計算が実行され、文字qを入力した場合、プログラムは終了し、それ以外の場合は、エラーステートメントが出力されます。

string userInput;
int userInputDigit = 0;
double userCost = 0;
char userInputChar;

userInput = Convert.ToString(Console.ReadLine());

if (int.TryParse(userInput, out userInputDigit))
{
    if (userInputDigit <= 50)
    {
        userCost = (price * userInputDigit);
        Console.WriteLine("You have purchased {0} Widgets at a cost of {1:c0}", userInputDigit, userCost);
    }
    else if ((userInputDigit > 50) && (userInputDigit <= 80))
    {
        userCost = (price * 50) + ((userInputDigit - 50) * (price - 1));
        Console.WriteLine("You have purchased {0} Widgets at a cost of {1:c0}", userInputDigit, userCost);
    }
    else if ((userInputDigit > 80) && (userInputDigit <= 100))
    {
        userCost = (price * 50) + (30 * (price - 1)) + ((userInputDigit - 80) * (price - 2.50));
        Console.WriteLine("You have purchased {0} Widgets at a cost of {1:c0}", userInputDigit, userCost);
    }
    else
    {
        Console.WriteLine("Error! Please input a number between 0 and 100");
    }

}
else if (char.TryParse(userInput, out userInputChar))
{
    if ((userInput == "q") || (userInput == "Q"))
    {
        System.Environment.Exit(0);
    }
    else
    {
        Console.WriteLine("Incorrect Letter Inputted");
    }
}
else
{
    Console.WriteLine("Error! Please input a number or 'q' to quit");
}
4

6 に答える 6

4

プロジェクトを右クリックし、[プロパティ]を選択してから(msdn.comを引用)する必要があると思います。

VisualStudio開発環境でこのリンカーオプションを設定するには

プロジェクトの[プロパティページ]ダイアログボックスを開きます。詳細については、「VisualC++プロジェクトのプロパティの設定」を参照してください。

リンカーフォルダをクリックします。

[システム]プロパティページをクリックします。

SubSystemプロパティを変更します。

サブシステムの場合は、コンソールを選択します。お役に立てれば!:)

于 2012-11-02T21:19:00.267 に答える
3

application.close() 関数を試すことをお勧めします。それは私の一日を何度も救ってくれました。問題が解決しない場合は、お知らせください。

于 2012-11-02T21:36:08.270 に答える
1

最初にアプリをデバッグで実行し、Application.Exit が実際にヒットしていることを確認します。これにより、アプリが終了しない理由が明らかになる可能性があります。

Environment.Exit(0) を使用する必要があります。これは、コンソール アプリを終了するためのより良い方法のようです。

ソース: http://geekswithblogs.net/mtreadwell/archive/2004/06/06/6123.aspx

このコードが Main にある場合は、return を使用することをお勧めします。アプリを終了するための呼び出しの後。

于 2012-11-02T21:22:12.717 に答える
0

デバッグ モード (Ctrl+F5) ではない Visual Studio からコンソール アプリケーションを起動すると、アプリケーションの実行が終了した後もコンソール ウィンドウが開いたままになります。そして、プロンプトが表示されPress any key to continue...ます。繰り返しますが、この時点でコンソール アプリケーションは終了しています。

*.exe ファイルを実行すると、キーを押すように求められずにコンソールが閉じます。それは単なるVisual Studioの「機能」です-実行可能ファイルを実行してから一cmd.exe時停止するように指示して実行します。

デバッグなしでアプリケーションを起動したときの Visual Studio の動作を次に示します (これを *.bat ファイルにコピーして実行できます)。

ECHO OFF
CLS
"Path\To\Your\ConsoleApplication.exe"
PAUSE
于 2012-11-02T21:34:49.230 に答える
0

ループを使用して、Main が正常に戻るようにします。さらに、文字列の比較と解析に加えて、条件チェックも少し簡素化しようとしました。エラー メッセージは、前述の if/else if ロジックによって実際に適用されない検証範囲 (「0 から 100 の間」) を示唆しています。たとえば、最初のケース (... <= 50) は、ユーザーが負の値を入力した場合に true になります。また、価格が宣言されている場所がわからなかったので、この例では定数を作成しました。

static bool ExitRequired(string line)
{
    return string.Equals(line, "q", StringComparison.OrdinalIgnoreCase);
}

static void Main(string[] args)
{
    const double price = 10;
    int userInputDigit;
    double userCost;
    string line = null;

    while (!ExitRequired(line))
    {
        Console.WriteLine("Enter a number or press 'q' to exit...");
        line = Console.ReadLine();

        if (ExitRequired(line))
            break;

        if (int.TryParse(line, out userInputDigit) 
            && userInputDigit > 0 
            && userInputDigit < 100)
        {
            if (userInputDigit <= 50)
            {
                userCost = (price * userInputDigit);
                Console.WriteLine("You have purchased {0} Widgets at a cost of {1:c0}", userInputDigit, userCost);
            }
            else if ((userInputDigit > 50) && (userInputDigit <= 80))
            {
                userCost = (price * 50) + ((userInputDigit - 50) * (price - 1));
                Console.WriteLine("You have purchased {0} Widgets at a cost of {1:c0}", userInputDigit, userCost);
            }
            else if ((userInputDigit > 80) && (userInputDigit <= 100))
            {
                userCost = (price * 50) + (30 * (price - 1)) + ((userInputDigit - 80) * (price - 2.50));
                Console.WriteLine("You have purchased {0} Widgets at a cost of {1:c0}", userInputDigit, userCost);
            }
        }
        else
        {
            Console.WriteLine("Error! Please input a number between 0 and 100");
        }
    }
}
于 2012-11-02T22:02:15.720 に答える
0

The "press any key to continue" text is added by when you run the project from Visual Studio.

Instead, go to the bin and run it from there.

Make sure you build the project before you run it. If you run from VS it will automatically build if needed, but running the file from windows explorer does not do this.

于 2012-11-02T22:40:32.177 に答える