C#编写:分别用数字1、2、3、4排列成三位互不相同不重复的数字。while怎么写代码
以下是使用while循环来实现的C#代码:
using System;
class Program
{
static void Main(string[] args)
{
int a = 1;
while (a <= 4)
{
int b = 1;
while (b <= 4)
{
if (b != a)
{
int c = 1;
while (c <= 4)
{
if (c != a && c != b)
{
Console.WriteLine($"{a}{b}{c}");
}
c++;
}
}
b++;
}
a++;
}
}
}
在上述代码中,我们使用了三个嵌套的while循环来遍历所有可能的排列组合。外部的while循环控制数字1的位置,中间的while循环控制数字2的位置,最内部的while循环控制数字3的位置。
在每个循环中,我们使用条件语句来排除已经使用过的数字。只有当三个数字都不相等时,才输出当前排列组合。
运行上述代码,将会输出以下结果:
123
124
132
134
142
143
213
214
231
234
241
243
312
314
321
324
341
342
412
413
421
423
431
432
``
原文地址: http://www.cveoy.top/t/topic/iiUh 著作权归作者所有。请勿转载和采集!