ユーザーに 10 から 100 までの 10 個の数字を入力してもらいたいのですが、それらは一意である必要があり、そうでない場合は新しい番号を要求されます。配列を機能させて10個の数字を受け入れることができましたが、他の数字と照合したり、値の間にあることを確認したりしません。このコードの下に、私が書いたコードがありますが、これは機能しますが、ループではありません。どんな助けでも大歓迎です。
int a;
int[] arr = new int[10]; // The size of the array is 10
// Here the values are accepted from the user
for (a = 0; a <= 9; a++)
{
Console.Write("\nEnter your number:\t");
//Here is were all the storing is done
arr[a] = Convert.ToInt32(Console.ReadLine());
//Here is my check values for the inputted numbers
if (arr[a] < 10 || arr[a] > 100) // I tried using brackets and && nothing worked
{
// If they do not meet the correct information
Console.WriteLine
("You did not enter a valid number.");
--arr[a];
}
else
{
//When they do meet the correct values
Console.WriteLine("Thanks for entering the number " + arr[a]);
}
}
Console.WriteLine("\n");
//Here the inputted values are printed out
for (a = 0; a < 10; a++)
{
Console.WriteLine("You entered the number {0}", arr[a]);
}
Console.ReadLine();
私が書いた動作するコードはループではありません。私はこの正確なコードをより少ない記述でループとして実行しようとしています。
Console.WriteLine("Please enter 10 numbers between 10 and 100. They cannot be identical.");
Retrypoint1:
int a = int.Parse(Console.ReadLine());
if ((a > 10) && (a < 100));
else
{
Console.WriteLine("The number you entered does not fall between 10 and 100.\r\n Please try again.");
goto Retrypoint1;
}
Retrypoint2:
int b = int.Parse(Console.ReadLine());
if ((b > 10) && (b < 100) && (b != a)) ;
else
{
Console.WriteLine("The number you entered does not fall between 10 and 100 \r\n or is identical to one of the other numbers. Please try again");
goto Retrypoint2;
}
Retrypoint3:
int c = int.Parse(Console.ReadLine());
if ((c > 10) && (c < 100) && (c != a) && (c != b)) ;
else
{
Console.WriteLine("The number you entered does not fall between 10 and 100 \r\n or is identical to one of the other numbers. Please try again");
goto Retrypoint3;
}
Retrypoint4:
int d = int.Parse(Console.ReadLine());
if ((d > 10) && (d < 100) && (d != a) && (c != b) && (d != c)) ;
else
{
Console.WriteLine("The number you entered does not fall between 10 and 100 \r\n or is identical to one of the other numbers. Please try again");
goto Retrypoint4;
}
Retrypoint5:
int e = int.Parse(Console.ReadLine());
if ((e > 10) && (e < 100) && (e != a) && (e != b) && (e != c) && (e != d)) ;
else
{
Console.WriteLine("The number you entered does not fall between 10 and 100 \r\n or is identical to one of the other numbers. Please try again");
goto Retrypoint5;
}
Retrypoint6:
int f = int.Parse(Console.ReadLine());
if ((f > 10) && (f < 100) && (f != a) && (f != b) && (f != c) && (f != d) && (f != e)) ;
else
{
Console.WriteLine("The number you entered does not fall between 10 and 100 \r\n or is identical to one of the other numbers. Please try again");
goto Retrypoint6;
}
Retrypoint7:
int g = int.Parse(Console.ReadLine());
if ((g > 10) && (g < 100) && (g != a) && (g != b) && (g != c) && (g != d) && (g != e) && (g != f)) ;
else
{
Console.WriteLine("The number you entered does not fall between 10 and 100 \r\n or is identical to one of the other numbers. Please try again");
goto Retrypoint7;
}
Retrypoint8:
int h = int.Parse(Console.ReadLine());
if ((h > 10) && (h < 100) && (h != a) && (h != b) && (h != c) && (h != d) && (h != e) && (h != f) && (h != g)) ;
else
{
Console.WriteLine("The number you entered does not fall between 10 and 100 \r\n or is identical to one of the other numbers. Please try again");
goto Retrypoint8;
}
Retrypoint9:
int i = int.Parse(Console.ReadLine());
if ((i > 10) && (i < 100) && (i != a) && (i != b) && (i != c) && (i != d) && (i != e) && (i != f) && (i != g) && (i != h)) ;
else
{
Console.WriteLine("The number you entered does not fall between 10 and 100 \r\n or is identical to one of the other numbers. Please try again");
goto Retrypoint9;
}
Retrypoint10:
int j = int.Parse(Console.ReadLine());
if ((j > 10) && (j < 100) && (j != a) && (j != b) && (j != c) && (j != d) && (j != e) && (j != f) && (j != g) && (j != h) && (j != i)) ;
else
{
Console.WriteLine("The number you entered does not fall between 10 and 100 \r\n or is identical to one of the other numbers. Please try again");
goto Retrypoint10;
}
Console.WriteLine("The numbers you entered were " + a + ", " + b + ", " + c + ", " + d + ", " + e + ", " + f + ", " + g + ", " + h + ", " + i + " & " + j);
Console.WriteLine("Please press enter to continue");
Console.ReadKey();