int[] numbers = { 1, 2, 3, 4, 5 };
for (int i = 0; i < numbers.Length / 2; i++)
{
int temp = numbers[i];
numbers[i] = numbers[numbers.Length - 1 - i];
numbers[numbers.Length - 1 - i] = temp;
}
for (int i = 1; i <= 9; i++)
{
for (int j = 1; j <= i; j++)
{
Console.Write($"{i} * {j} = {i * j}\t");
}
Console.WriteLine();
}
int[] numbers = { 5, 2, 8, 1, 3 };
for (int i = 0; i < numbers.Length - 1; i++)
{
for (int j = 0; j < numbers.Length - 1 - i; j++)
{
if (numbers[j] > numbers[j + 1])
{
int temp = numbers[j];
numbers[j] = numbers[j + 1];
numbers[j + 1] = temp;
}
}
}
這些是一些在C#中使用for循環的創造性應用,展示了for循環在不同場景中的靈活性和多樣性。