1) publicstaticvoidmain(String[] args){ // TODO Auto-generated method stub int s = 0; for (int i=0;i<=100;i++){ s += i; } System.out.println("1到100之间所有整数的和为:"+s); } 2) publicstaticvoidmain(String[] args){ // TODO Auto-generated method stub int s = 0; int i = 0; while (i <= 100){ s += i; i++; } System.out.println("1到100之间所有整数的和为:"+s); 3) publicstaticvoidmain(String[] args){ // TODO Auto-generated method stub int s = 0; int i = 0; do{ s += i; i++; }while (i <= 100); System.out.println("1到100之间所有整数的和为:"+s);
2
求n!(n为正整数),n在运行时从键盘输入
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
import java.util.*;
publicclassgq{
publicstaticvoidmain(String[] args){ // TODO Auto-generated method stub Scanner in=new Scanner(System.in); System.out.println("请输入一个正整数:"); int num=in.nextInt(); int sum=1; for(int i=1;i<=num;i++){ sum=sum*i; } System.out.println("sum="+sum); }
}
3
把100-200之间不能被3整出的数输出
1 2 3 4 5 6 7 8 9 10
publicstaticvoidmain(String[] args){ // TODO Auto-generated method stub for (int i = 100;i<=200;i++){ if (i % 3 != 0 ){ System.out.println(i); } } System.out.println("以上数值均不能整除3"); }
publicstaticvoidmain(String[] args){ // TODO Auto-generated method stub for (int i = 100;i<=999;i++){ int x = i / 100; int y = (i%100)/10; int z = (i%100)%10; int s = x*x*x + y*y*y + z*z*z; if ( s == i){ System.out.println(i + "为水仙花数"); } } }
publicstaticvoidmain(String[] args){ // TODO Auto-generated method stub Scanner in = new Scanner(System.in); System.out.println("请输入n行:"); System.out.println("请输入m列:"); int n = in.nextInt(); int m = in.nextInt(); for (int i=1;i<=n;i++){ for (int q=1;q<=m;q++){ System.out.print("*"); } System.out.println("\n"); } }
publicstaticvoidmain(String[] args){ // TODO Auto-generated method stub Scanner in = new Scanner(System.in); System.out.println("请输入一个奇数:"); int n = in.nextInt(); int m = n / 2; for(int i=1;i<m+2;i++){ for(int j=1;j<=i;j++){ System.out.print("*"); } System.out.println(); } for(int i=m;i>=1;i--){ for(int j=1;j<=i;j++){ System.out.print("*"); } System.out.println(); } }
publicstaticvoidmain(String[] args){ // TODO Auto-generated method stub Scanner in = new Scanner(System.in); System.out.println("请输入一个奇数:"); int n = in.nextInt(); int m = n / 2; for(int i=1;i<m+2;i++){ for (int k = m-i+1; k>0;k--) { System.out.print(" "); } for(int j=1;j<=i;j++){ System.out.print("*"); } System.out.println(); } for(int i=m;i>=1;i--){ for (int k = m-i+1; k>0;k--) { System.out.print(" "); } for(int j=1;j<=i;j++){ System.out.print("*"); } System.out.println(); } }
publicstaticvoidmain(String[] args){ // TODO Auto-generated method stub int n = 1; for (int i = 1;i < 10;i++){ n = (n+1)*2; } System.out.println(n); }
思考题
1
求和s=1!+2!+3! +…+20!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
publicstaticvoidmain(String[] args){ // TODO Auto-generated method stub long s = 0,m; for (int i = 1;i <= 20;i++){ m = 1; for(int j = 1;j <= i;j++){ m = m*j; } s = s+m; } System.out.println(s); }
2
求s= 2+22+222+2222+…..,前n项的和,n在运行时从键盘输入
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
publicstaticvoidmain(String[] args){ //求s= 2+22+222+2222+…..,前n项的和,n在运行时从键盘输入 Scanner x = new Scanner(System.in); System.out.print("请输入想要求的n项和:"); double n = x.nextInt(); double s = 2; double I = 0; double sum = 2; for(int i=1;i < n;i++){ double b = Math.pow(10, i); I = b * 2; s = s + I; sum = sum + s; //System.out.println(s); } System.out.printf("前%.0f的和为:%.1f" ,n,sum); x.close(); }
publicstaticvoidmain(String[] args){ double s = 10; double sum = 0; //编写程序:一个球从100m高度自由落下,每次落地后反跳回原高度的一半,再落下,再反弹。求它在第10次落地时,共经过了多少米?第10次反弹多高? for(int i=1;i<=10;i++){ s = s / 2; sum += s *2; } System.out.println("共经过了:"+sum+"米"); System.out.println("第十次落地反弹为:"+s+"米"); }