C#编写:用数字1、2、3、4排列成三位互不相同不重复的数字。while怎么写代码
你可以使用一个while循环来实现该功能。具体代码如下:
using System;
public class Program
{
public static void Main(string[] args)
{
int number = 123;
while (number <= 432)
{
int digit1 = number / 100; // 获取百位数字
int digit2 = (number / 10) % 10; // 获取十位数字
int digit3 = number % 10; // 获取个位数字
// 判断三个数字是否互不相同
if (digit1 != digit2 && digit1 != digit3 && digit2 != digit3)
{
Console.WriteLine(number);
}
number++;
}
}
}
上述代码使用一个while循环来遍历数字123到432之间的所有数。在循环内部,首先获取当前数字的百位、十位和个位数字,并通过条件判断确保这三个数字互不相同。如果满足条件,则输出当前数字。最后,通过递增number的值来遍历下一个数字
原文地址: https://www.cveoy.top/t/topic/iiUf 著作权归作者所有。请勿转载和采集!