1.问题重述
题目:有1、2、3、4个数字,能组成多少个互不相同且无重复数字的三位数?都是多少?
2.解析
2.1如何判断无重复位?
使用三个嵌套的for循环,再最里层的for循环中使用if语句判断。
2.2如何输出这些数字?
System.out.println(百位数字100 + 十位数字10 + 个位数字);
3.解决问题
代码如下:
public class demo {
public static void main(String[] args) {
int sum = 0;
for(int bite = 1;bite <= 4;bite++) {
for(int ten = 1;ten <= 4;ten++) {
for(int hundrad = 1;hundrad <= 4;hundrad++) {
if(bite != ten && ten != hundrad && bite != hundrad) {
sum++;
System.out.print(hundrad*100 + ten*10 + bite + "\t");
}
}
}
}
System.out.println();
System.out.println("一共有" + sum + "个");
}
}