package chapter_02;publicclassPractice_02_01{publicstaticvoidmain(String[] args){int sum =0;for(int i =1; i <101; i++){
sum += i;}
System.out.println("1+2+3+...+100="+sum);}}
2.代码
package chapter_02;publicclassPractice_02_02{publicstaticvoidmain(String[] args){int sum =0;for(int i =1; i <100; i +=2){
sum += i;}
System.out.println("1+3+5+...+99="+sum);}}
3.代码
package chapter_02;publicclassPractice_02_03{publicstaticvoidmain(String[] args){int sum =0, i =1;while(i <101){
sum += i;
i++;}
System.out.println("1+2+3+...+100="+sum);
sum =0;
i =1;do{
sum += i;
i +=2;}while(i <100);
System.out.println("1+3+5+...+99="+sum);}}
4.代码
package chapter_02;import java.util.*;publicclassPractice_02_04{publicstaticvoidmain(String[] args){
System.out.println("请输入一个小于10的整数:");
Scanner s =newScanner(System.in);int n = s.nextInt(), factorial =1;for(int i = n; i >0; i--){
factorial *= i;}
System.out.println(n+"的阶乘为:"+factorial);}}
5.代码
package chapter_02;publicclassPractice_02_05{publicstaticvoidmain(String[] args){int sum =0;for(int i =3; i <100; i +=3){if(i%3==0&& i%5!=0){
sum += i;}}
System.out.println("100以内能被3整除但不能被5整除的数之和为:"+sum);}}
6.代码
package chapter_02;publicclassPractice_02_06{publicstaticvoidmain(String[] args){int a, b, c;for(c =0; c <100; c +=3){int lefta =(100-c/3)/3;for(a =0; a <= lefta; a++){
b =(100-a*3-c/3)/2;if(c+b+a ==100)
System.out.println(a+" "+b+" "+c);}}}}
package chapter_02;publicclassPractice_02_08{publicstaticvoidmain(String[] args){int a, b, c, d;for(int i =1000; i <=9999; i++){
a = i/1000;
b = i/100%10;
c = i/10%10;
d = i%10;if((a*b+c*d)*(a*b+c*d)== i)
System.out.println(i);}}}
9.代码
package chapter_02;import java.util.*;publicclassPractice_02_09{publicstaticvoidmain(String[] args){
System.out.println("请输入一个整数n:");
Scanner s =newScanner(System.in);int n = s.nextInt();for(int i =1; i <= n; i++){for(int j =0; j < n-i; j++){
System.out.print(" ");}for(int j =0; j <2*i-1; j++){
System.out.print("*");}
System.out.println();}}}
10.代码
package chapter_02;publicclassPractice_02_10{publicstaticvoidmain(String[] args){for(int i =1; i <10; i++){for(int j =1; j <= i; j++){
System.out.print(j+"*"+i+"="+i*j+" ");}
System.out.println();}}}
11.代码
package chapter_02;publicclassPractice_02_11{publicstaticvoidmain(String[] args){int a, b, c;for(int i =100; i <999; i++){
a = i/100;
b = i/10%10;
c = i%10;if(a*a*a+b*b*b+c*c*c == i){
System.out.println(i);}}}}