0

私はC#が本当に苦手です私は本当にC#の基本的な知識しか持っていません私はこのコードを持っていますが、うまく動作させることができません申し訳ありませんが、これはおそらく非常に簡単な間違いであり、他のコードは真実であり、私の古いプログラムでした:(

  Console.WriteLine("Sisesta esimene arv vahemikus 10-20");
            vastus1 = int.Parse(Console.ReadLine());
            Console.WriteLine("sisesta teine arv vahemikus 20-32");
            vastus2 = int.Parse(Console.ReadLine());
            Console.WriteLine("Vastus {0}", vastus2 - vastus1);

            string tekst1 = Console.ReadLine();
            vastus3 = int.Parse(tekst1);   <------ debugger says problem is here
        }
        while ((vastus1 < 1 || vastus2 < 12));

        if (vastus3 >= 3 && vastus3 >= 5) 
        {
            Console.WriteLine("On Kevad");

                {
                    if (vastus3 >= 6 && vastus3 >= 8) 
                    {
                        Console.WriteLine("on suvi");
                    }
                }
                if (vastus3 >= 9 && vastus3 >= 11) 
                {
                    Console.WriteLine("on sügis");
               }
                if (vastus3 >= 11 && vastus3 >= 2) 
                {
                    Console.WriteLine("on talv");
                }
            }

        }
    }
}
4

2 に答える 2

4

まあ、エラーはそれをすべて本当に言います

The input string was not in the right format

あなたがしていることのコンテキストでは、これは、コンソールに直接入力したものは何でもint.Parseintとして解析できないことを意味します。

コンソールに入力されるものが数値ではない場合があると予想される場合は、int.TryParseそれが有効かどうかを確認するために使用できます。

int vastus3 = 0;
while(!int.TryParse(Console.ReadLine(),out vastus3 ))
{
    Console.WriteLine("Invalid number, try again!");
}
// here "vastus3" will have your integer

これは、失敗した現在の行が正確にコードに入る可能性があります。

于 2013-03-13T16:53:23.547 に答える
0

Jamiec の説明は正しいです。探している整数が常に文字列の末尾に同じ形式である場合は、Substring を使用して特定の文字を取得し、比較チェックを行うことができます。

Substring を使用して文字列の末尾から文字を削除する方法の例を次に示します。

文字列の末尾から文字列を部分文字列化する

そして、msdn

http://msdn.microsoft.com/en-us/library/system.string.substring%28v=vs.71%29.aspx

于 2013-03-13T17:03:33.400 に答える