###############获取一个数字的每位数字###############
#############1-100奇数和偶数和##############
public class Sum {
public static void main(String[] args) {
int sum = 0;
int sum1 = 0;
for (int i = 1; i <=100 ; i++) {
if (i%2==0) {
sum += i;
}else {
sum1 += i;
}
}
System.out.println("sum="+sum);
System.out.println("sum1="+sum1);
}
}
###############列出水仙花数###############
public class ShuiXianHua {
public static void main(String[] args) {
for (int i = 100; i <= 999; i++) {
int ge = i%10;
int shi = i/10%10;
int bai = i/100%10;
int gelifang = ge*ge*ge;
int shilifang = shi*shi*shi;
int bailifang = bai*bai*bai;
int sum = gelifang+shilifang+bailifang;
if (i==sum) {
System.out.println("水仙花数是:"+i);
}
}
}
}
#####################统计水仙花个数##############
public class ShuiXianHua { public static void main(String[] args) { int count = 0; for (int i = 100; i <= 999; i++) { int ge = i%10; int shi = i/10%10; int bai = i/100%10; int gelifang = ge*ge*ge; int shilifang = shi*shi*shi; int bailifang = bai*bai*bai; int sum = gelifang+shilifang+bailifang; if (i==sum) { //System.out.println("水仙花数是:"+i); count++; } } System.out.println("共有水仙花数:"+count); } }
#############需求练习###############
public class Sum { public static void main(String[] args) { int count = 0; for (int i = 1; i <=1000; i++) { if(i%3==2 && i%5==3 && i%7==2){ System.out.println(i); count++; } } System.out.println("总共有:"+count+"个"); } } 结果: 23 128 233 338 443 548 653 758 863 968 总共有:10个
####################while循环############################
/* while循环的基本格式: while(判断条件语句) { 循环体语句; } 扩展格式: 初始化语句; while(判断条件语句) { 循环体语句; 控制条件语句; } 通过这个格式,我们就可以看到其实和for循环是差不多的。 for(初始化语句;判断条件语句;控制条件语句) { 循环体语句; } */ class WhileDemo { public static void main(String[] args) { //输出10次"HelloWorld" //for语句版 for(int x=0; x<10; x++) { System.out.println("HelloWorld"); } System.out.println("--------------"); //while语句版 int x=0; while(x<10) { System.out.println("HelloWorld"); x++; } } }
################while 循环1-100 的和############
public class WhileDemo { public static void main(String[] args) { //while 1-100求和 int x =1; int sum = 0; while (x<=100) { sum+=x; x++; } System.out.println("1-100的和:"+sum); } }
##############while和for循环的区别###############
############4行5列小星星#############
public class WhileDemo { public static void main(String[] args) { //4行5列的小星星 for (int i = 0; i < 4; i++) { for (int j = 0; j < 5 ; j++) { System.out.print("*"); } System.out.println(); } } } 结果: ***** ***** ***** *****
while 版本: public class WhileDemo { public static void main(String[] args) { //4行5列的小星星 /*for (int i = 0; i < 4; i++) { for (int j = 0; j < 5 ; j++) { System.out.print("*"); } System.out.println(); } */ int x = 0; while (x<4) { int i = 0; while (i<5) { System.out.print("*"); i++; } System.out.println(); x++; } } }
##########输出正三角形############
public class WhileDemo { public static void main(String[] args) { //输出正三角形 for (int i = 0; i < 5; i++) { for (int j = 0; j < i+1 ; j++) { System.out.print("*"); } System.out.println(); } } } 结果: * ** *** **** *****
##############九九乘法表############
代码实现:
public class Sum { public static void main(String[] args) { for (int i = 1; i <= 9; i++) { for (int j = 1; j < i+1; j++) { System.out.print(j+"*"+i+"="+i*j+" "); } System.out.println(); } } }
另外一种:
public class Sum {
public static void main(String[] args) {
for (int i = 1; i <= 9; i++) {
for (int j = 1; j <=i; j++) {
System.out.print(j+"*"+i+"="+i*j+" ");
}
System.out.println();
}
}
}
结果:
1*1=1
1*2=2 2*2=4
1*3=3 2*3=6 3*3=9
1*4=4 2*4=8 3*4=12 4*4=16
1*5=5 2*5=10 3*5=15 4*5=20 5*5=25
1*6=6 2*6=12 3*6=18 4*6=24 5*6=30 6*6=36
1*7=7 2*7=14 3*7=21 4*7=28 5*7=35 6*7=42 7*7=49
1*8=8 2*8=16 3*8=24 4*8=32 5*8=40 6*8=48 7*8=56 8*8=64
1*9=9 2*9=18 3*9=27 4*9=36 5*9=45 6*9=54 7*9=63 8*9=72 9*9=81
##############控制跳转语句break##############
###############重点跳出循环#################
public class WhileDemo { public static void main(String[] args) { //4行5列的小星星 for (int i = 0; i <5; i++) { for (int j = 0; j <4 ; j++) { if (j == 2) { break; } System.out.print("*"); } System.out.println(); } } } 结果: ** ** ** ** ** ###########对比下########## 跳出外层循环 public class WhileDemo { public static void main(String[] args) { //4行5列的小星星 wc:for (int i = 0; i <5; i++) { for (int j = 0; j <4 ; j++) { if (j == 2) { break wc; } System.out.print("*"); } System.out.println(); } } } 结果: **