1.求1-100的和
public static void main(String[] args) { int sum = 0; for (int i = 0; i <= 100 ; i++) { sum += i; } System.out.println(sum); }
2.求1-100之间偶数和
public static void main(String[] args) { int sum = 0; for (int i = 0; i <= 100 ; i++) { if(i % 2 ==0){ sum += i; } } System.out.println(sum); }
3.求1-100之间奇数和
public static void main(String[] args) { int sum = 0; for (int i = 0; i <= 100 ; i++) { if(i % 2 == 1){ sum += i; } } System.out.println(sum); }
4.求水仙花数
水仙花数即一个三位数,这个三位数=个位数的立方+十位数的立方+百位数字的立方。
public static void main(String[] args) { for (int i = 100; i < 1000; i++) { int ge = i%10; int shi = (i/10)%10; int bai = (i/100)%10; if(i == ge * ge * ge + shi * shi * shi + bai * bai * bai){ System.out.println(i); } } }
4.九九乘法表
public static void main(String[] args) { for (int i = 1; i < 10; i++) { for(int j = 1;j<=i;j++) { System.out.print(i+"*"+j+"="+i*j+" "); } System.out.println(); } }