1. n! = 1*2*3*4....*n,计算2!,4!,6!,8!和10!,显示计算结果
public class Base03 {
public int fun(int n){
int m = 1;
for (int i=1;i<=n;i++){
m = m * i;
}
return m;
}
public static void main(String args[]){
Base03 bs = new Base03();
System.out.println("2! = "+ bs.fun(2));
System.out.println("4! = "+ bs.fun(4));
System.out.println("6! = "+ bs.fun(6));
System.out.println("8! = "+ bs.fun(8));
System.out.println("10! = "+ bs.fun(10));
}
}
2.请写出使用for语句计算 S = 1 + 1/2 + 1/3 + ···+ 1/n 的代码
public class Base04 {
public double fun(int n){
double S=0;
for (int i=1;i<=n;i++){
S = S + 1.0/i;
}
return S;
}
public static void main(String args[]){
Base04 bs = new Base04();
System.out.println("n=5 S="+bs.fun(5));
System.out.println("n=10 S="+bs.fun(10));
System.out.println("n=100 S="+bs.fun(100));
}
}
3.计算 1 + 1/3 + 1/6+···+ 1/3*n 的值
public class Base05 {
public double fun01(int n){
double S1=1.0;
for (int i=1;i<=n;i++){
S1 = S1 + 1.0/(3*i);
}
return S1;
}
public static void main(String args[]){
Base05 bs1 = new Base05();
System.out.println("1+1/3+1/6+...+1/3*5 = "+bs1.fun01(5));
System.out.println("1+1/3+1/6+...+1/3*10 = "+bs1.fun01(10));
}
}
4.打印下面的图案
* * * * * * *
* * * * *
* * *
*
* * *
* * * * *
* * * * * * *
public class Base06 {
public static void main(String args[]){
for (int i=-3 ;i<4;i++){
for (int k = 6-Math.abs(2*i);k>0;k--){
System.out.print(" ");
}
for (int j=1;j<=Math.abs(2*i)+1;j++){
System.out.print("* ");
}
System.out.print("\n");
}
}
}
5.打印下面图案 (10 * 10)
* * * * * * * * * *
* * * * * * * * *
* * * * * * * *
* * * * * * *
* * * * * *
* * * * *
* * * *
* * *
* *
*
public class Base07 {
public static void main(String args[]){
for(int i=0;i<10;i++){
for(int j=1;j<=10-i;j++){
System.out.print("* ");
}
System.out.print("\n");
}
}
}
6.打印九九乘法表
public class Base08 {
public static void main(String args[]){
for(int i=1;i<=9;i++){
for(int j=1;j<=i;j++){
int c = i*j;
System.out.print(j+"*"+i+"="+c+" ");
}
System.out.print("\n");
}
}
}