2017-02-10 3 views
0

私のコードです:C#が何か

string[] code = new string[9]; 
int[] intCode = new int[9]; 

int cd = 0, dvd = 0, video = 0, book = 0; 
for (int i = 0; i < 10; i++) 
{ 

    Console.Write("Enter code#{0}: ",i+1); 
    code[i] = Console.ReadLine(); 
    if (code[i].Length==5) 
    { 
     intCode[i] = Convert.ToInt32(code[i]); 
     intCode[i] /= 100000; 
     if (intCode[i] == 1) 
     { 
      cd++; 
      break; 

     } 
     if (intCode[i] == 2) 
     { 
      dvd++; 
      break; 
     } 
     if (intCode[i] == 3) 
     { 
      video++; 
      break; 
     } 
     if (intCode[i] == 4) 
     { 
      book++; 
      break; 
     } 
    } 
    else 
    { 
     Console.WriteLine("INVALID CODE"); 

    } 

} 

は、基本的には私がやりたいことの代わりのために行くの、番号を再入力するようユーザーに依頼する{ここにいくつかのことを行う}他にありますiをループしてicrementingしてユーザーに新しい入力を求める。

+3

あなたは」 "いいえ"の条件を聞いたことはありますか?あなたのコードに論理的な失敗があるように見えます。あなたがどのようにして人がこれを働かせてくれるかを考えてみて、それを書き留めてみようと思っている人に説明してみてください。 – BugFinder

+4

おそらく 'while'ループでしょうか?あなたがここで探しているものを教えてくれるのは難しいです... –

+0

5桁の数字を6桁の数字で割って、結果が1,2,3または4になると期待しています。代わりに 'if(code [i] [0] == '1')'などを確認してください。 –

答えて

1

を修正:

else 
{ 
    Console.WriteLine("INVALID CODE"); 
    i -= 1; 
} 
+0

ありがとうございます:) –

+0

それを再入力するには、もう一度やり直してください。 – PSo

+0

ええ、うまく動作します。 –

0

しばらくの組み合わせを使用して切り替える:

 string[] code = new string[9]; 
     int[] intCode = new int[9]; 
     int cd = 0, dvd = 0, video = 0, book = 0; 
     for (int i = 0; i < 10; i++) 
     { 
      bool isCorrectInput = false; 
      while (!isCorrectInput) 
      { 
       isCorrectInput = true; 
       Console.Write("Enter code#{0}: ", i+1); 
       code[i] = Console.ReadLine(); 
       if (code[i].Length == 1) 
       { 
        intCode[i] = Convert.ToInt32(code[i]); 
        // intCode /= 100000; 
        switch (intCode[i]) 
        { 
         case 1: 
          cd++; 
          break; 
         case 2: 
          dvd++; 
          break; 
         case 3: 
          video++; 
          break; 
         case 4: 
          book++; 
          break; 
         default: 
          isCorrectInput = false; 
          break; 
        } 
       } 
       else 
        isCorrectInput = false; 
       if (!isCorrectInput) 
        Console.WriteLine("INVALID CODE ENTERED!"); 
      } 
     } 

EDIT: はあなたが今欲しいものでなければならず、また、他のブロックでは、あなたのバグ

+0

does not work。入力が有効であっても入力が無効であると表示されます –

+0

ユーザーが番号を再入力させる方法を依頼していました。ここでは、入力が有効ではない問題があります。 100000で除算し、各5桁の値を100000で除算し、intに解析すると0になり、シナリオでは0が処理されないので、正しいことは不可能です – Pedro

関連する問題