0

私はこのクレイジーな考えを持っています.間違ったデータがコンソールに入れられた場合、プログラムが何も実行しないようにしたいと思います. アルファベットなど、変な文字。私が望むのは、10 進数とピリオドだけです。間違ったデータが入力された場合、プログラムがそこにとどまり、Enter キーを押した後はまったく何もしないようにします。

私の考え方は次のように考えています。

if (sum != decimal)
{
   // Don't do anything, just leave it as is. 
    code I have no clue about. 

}

ここで、if ステートメントにデータ型を使用することはできないと考えているに違いありません。できるかもしれませんが、私にはうまくいきません。大雑把ですみません。

try
{

    Console.WriteLine("Put in the price of the product");

    string input = Console.ReadLine();
    decimal sum = Convert.ToDecimal(input);

    if (sum <= 100)
    {
        decimal totalprice = sum * .90m;
        Console.WriteLine("Your final price is {0:0:00}", totalprice);

    }

}


catch
{

}

また、try and catchステートメントも機能するのではないかと考えていましたが、やはり何を入れればよいかわかりません。

あなたの答えがnoob-safeで説明できる場合。(これがどのように機能するかの概念を学びたいので)それはいいでしょう.

視覚的な例:

スタックオーバーフロー画像

Enter キーを押しても何も起こりませんが、正しいデータ型を入力すると、プログラムは続行します。

4

3 に答える 3

2

データ型はコンソールに書き込まれません。コンソール入力から文字列のみを取得できました。文字列を持つ型は何ですか"2"? decimal、int、byte、string? あなたができることは、入力文字列からいくつかのタイプを解析しようとすることだけです:

Int32.TryParse("2", out value)

あなたの場合:

Console.WriteLine("Put in the price of the product");
string input = Console.ReadLine();
decimal sum;
if (!Decimal.TryParse(input, out sum))
{
    Console.WriteLine("Decimal number cannot be parsed from your input.");
    return;
}

if (sum <= 100)
    Console.WriteLine("Your final price is {0:0:00}", sum * 0.90M);

アップデート

  • Decimal.TryParse - 数値の文字列表現をDecimal同等のものに変換します。戻り値は、変換が成功したか失敗したかを示します。変換が失敗した場合、例外はスローされません。
  • ! オペレーター- オペレーターではありません。論理否定演算子 (!) は、オペランドを否定する単項演算子です。に対して定義され、そのオペランドが である場合にのみboolを返します。truefalse

したがってif (!Decimal.TryParse(input, out sum))、変換が成功しなかったかどうかを確認します。次に、ユーザーにサンプルメッセージを入力し、メソッドを終了しました(それがあなたのMainメソッドだった場合、プログラムは終了します。しかし、これはすべて、文字列の解析に関する最初の質問から外れています.

于 2012-11-03T00:15:30.370 に答える
2

Try this (note the while/break pairing):

while (true)
{
    string input = Console.ReadLine();
    decimal sum;

    if (Decimal.TryParse(input, out sum) == true)
    {
        if (sum <= 100)
        {
            decimal totalprice = sum * .90m;
            Console.WriteLine("Your final price is {0:0:00}", totalprice);
            break;  // break out of while
        }
    }
}
于 2012-11-03T00:25:07.717 に答える
0

使用している変換関数は、渡された文字列を要求された型に変換できない場合、例外をスローすると思います。一般に、例外はプログラム フローを制御するために回避し、真に予期しない状況のために予約する必要があります。代わりに、例外をスローせず、代わりに成功または失敗を示す値を返すメソッドの使用を検討する必要があります。これをindで試すことができます:

    try
    {
        Console.WriteLine("Put in the price of the product");
        decimal sum;
        // Repeat forever (we'll break the loop once the user enters acceptable data)
        while (true)
        {
            string input = Console.ReadLine();
            // Try to parse the input, if it succeeds, TryParse returns true, and we'll exit the loop to process the data.
            // Otherwise we'll loop to fetch another line of input and try again
            if (decimal.TryParse(input, out sum)) break;
        }

        if (sum <= 100)
        {
            decimal totalprice = sum * .90m;
            Console.WriteLine("Your final price is {0:0:00}", totalprice);
        }
    }
    catch
    {

    }
于 2012-11-03T00:33:34.543 に答える