30个java基础程序

30个java基础程序

这段时间为了熟悉java基础语法,就顺手写了30个java基础程序,这里很大家分享一下,有什么不足的地方欢迎大家随时指出。

1.数字排列

public static void main(String[] args)
  {
      int i,j,k;
      System.out.println("\n");
      for(i=1;i<5;i++)
          for(j=1;j<5;j++)
              for(k=1;k<5;k++)
              {
                  if(i!=k&&i!=j&&j!=k)
                      System.out.println(i+""+j+""+k);
              }
  }

2.奖金分配问题

public static void main(String[] args)
{
    Scanner in=new Scanner(System.in);
    long  i;
    double a,b,c,d,e,bonus;
    i=in.nextInt();
    a=100000*0.1;
    b=100000*0.075;
    c=a+200000*0.05;
    d=c+200000*0.03;
    e=d+400000*0.015;
    if(i<=100000)
        bonus=i*0.1;
    else if(i<=200000)
        bonus=a=(i-100000)*0.75;
    else if(i<=400000)
        bonus=b+(i-200000)*0.05;
    else if(i<=600000)
        bonus=c+(i-400000)*0.03;
    else if(i<=1000000)
        bonus=d+(i-600000)*0.015;
    else
        bonus=e+(i-1000000)*0.01;
    System.out.println(bonus);
}

3.已知条件求解整数

public static void main(String[] args)
   {
       int i;
       long x,y;
       for(i=1;i<=100000;i++)
       {
           x=(int)Math.sqrt(i+100);
           y=(int)Math.sqrt(i+268);
           if(x*x==i+100&&y*y==i+268)
               System.out.println(x);
       }
   }

4.输入日期判断第几天

public static void main(String[] args)
    {
        Scanner in=new Scanner(System.in);
       int day,month,year,sum=0,leap;
        System.out.println("\nplesse input your year.month,day\n");
        year=in.nextInt();
        month=in.nextInt();
        day=in.nextInt();
        switch(month)
        {
            case 1:sum=0;break;
            case 2:sum=31;break;
            case 3:sum=59;break;
            case 4:sum=90;break;
            case 5:sum=120;break;
            case 6:sum=151;break;
            case 7:sum=181;break;
            case 8:sum=212;break;
            case 9:sum=243;break;
            case 10:sum=273;break;
            case 11:sum=304;break;
            case 12:sum=334;break;
            default:System.out.println("data error");break;
        }
        sum=sum+day;
        if(year%400==0||(year%4==0&&year%100!=0))
            leap=1;
        else
            leap=0;
        if(leap==1&&month>2)/*如果是闰年且月份大于2,总天数应该加一天*/
            sum++;
        System.out.println("it is the "+sum);
    }

5.输入整数进行排序

public static void main(String[] args)
 {
     Scanner in=new Scanner(System.in);
     int x,y,z,t;
     x=in.nextInt();
     y=in.nextInt();
     z=in.nextInt();
     if(x>y){
         t=x;x=y;y=t;
     }
     if(x>z){
         t=z;z=x;x=t;
     }
     if(y>z){
         t=y;y=z;z=t;
     }
     System.out.println("small to big "+x+" "+y+" "+z);
 }

6.打印九九口诀

public static void main(String[] args)
    {
        System.out.println("\n");
        int i,j,result;
        for(i=1;i<=9;i++)
        {
            for(j=1;j<=i;j++)
            {
                result=i*j;
                System.out.printf("%d*%d=%-3d",i,j,result,"\t");
            }
            System.out.println();
        }
    }

7.输出国际象棋棋盘

public static void main(String[] args)
  {
      int i,j;
      for(i=0;i<8;i++)
      {
      for(j=0;j<8;j++)
        if((i+j)%2==0)
      System.out.printf("%c%c",219,219);
      else
      System.out.printf(" ");
      System.out.printf("\n");
      }
  }

8.经典兔子问题

public static void main(String[] args)
{
    System.out.println("第一个月的兔子对数: 1");
    System.out.println("第二个月的兔子对数: 1");
    int f1=1,f2=1,f,M=24;
    for(int i=3;i<M;i++){
        f=f2;
        f2=f1+f2;
        f1=f;
        System.out.println("第"+i+"个月的兔子对数"+f2);
    }
}

9.判断素数

public static void main(String[] args)
    {
        int count = 0;
        for(int i=101; i<200; i+=2) {
            boolean b = false;
            for(int j=2; j<=Math.sqrt(i); j++)
            {
                if(i % j == 0) { b = false; break;
                }
                else
                {
                    b = true;
                }
            }
            if(b == true) {count ++;System.out.println(i );
            }
        }
        System.out.println( "素数个数是: " + count);
    }

10.水仙花数问题

public static void main(String[] args) {
        int x = 0;        //定义水仙花数的个数
        for(int i=100;i<=999;i++){
            int b = i/100;        //取得百位数
            int s = (i-100*b)/10;        //取得十位数
            int g = (i-s*10-b*100);        //取得个位数

            if(i==g*g*g+s*s*s+b*b*b){
                x++;    //每次符合水仙花数条件,则x+1;
                System.out.print(i+" ");    //输出符合条件的数
        }
        }System.out.println();        //换行
        System.out.println("水仙花数总共有"+x+"个");    //输出水仙花数的总数
    }

11.正整数分解质因数

public static void main(String[] args) {
    Scanner in=new Scanner(System.in);
    int n,i;
    System.out.println("\nplease input a number:\n");
    n=in.nextInt();
    System.out.print(n+"=");
    for(i=2;i<=n;i++) {
        while(n!=i) {
            if(n%i==0) {
                System.out.print(i+"*");
                n=n/i;
            }
            else
                break;
        }
    }
    System.out.print(n);
}

12.学习成绩划分

public static void main(String[] args) {
    Scanner in =new Scanner(System.in);
    int score;
    char grade;
    System.out.print("please input a score\n");
    score=in.nextInt();;
    grade=score>=90?'a':(score>=60?'b':'c');
    System.out.print(score+"  "+"  "+grade);
}

13.正整数求其最大公约数和最小公倍数

public static void main(String[] args) {
        Scanner in= new Scanner(System.in);
        int m,n;
        System.out.print("请输入一个数:");
        m= in.nextInt();
        System.out.print("请输入另一个数:");
        n= in.nextInt();
        System.out.println(gongyue(m, n));
        System.out.println(gongbei(m, n));
    }

    // a与b的最大公约数
    public static int gongyue(int m, int n) {
        while(n%m!=0){
            int temp=m%m;
            n=m;
            m=temp;
        }
        return m;
    }

    // a与b的最小公倍数
    public static int gongbei(int m, int n) {
        return m* n / gongyue(m, n);
    }

14.统计英文字母/空格/数字个数

public static void main(String[] args) {
        int num=0,chartra=0,blak=0,other=0;
        Scanner  s=new Scanner(System.in);
        System.out.println("请输出字符串");
        String   result=s.nextLine();
        char x[]=result.toCharArray();
        for(int i=0;i<x.length;i++){
            if(Character.isDigit(x[i])){
                num++;
            }else if(Character.isLetter(x[i])){
                chartra++;
            }else if(Character.isSpace(x[i])){
                blak++;
            }
            else{
                other++;
            }
            System.out.println(x.length);
        }
        System.out.println("数字的个数是"+num);
        System.out.println("字符的个数是"+chartra);
        System.out.println("空值的个数是"+blak);
        System.out.println("其他的个数是"+other);
    }    

15.求s=a+aa+aaa+aa…a的值

public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    System.out.println("用哪个数循环?:");
    int _temp = sc.nextInt();
    System.out.println("循环相加多少次?:");
    int temp = sc.nextInt();
    int newNumber = 0; // 每次生成的新数
    int total = 0; // 计算结果
    for (int i = 0; i < temp; i++) {
        newNumber = newNumber * 10 + _temp;
        System.out.println(newNumber);
        total = total + newNumber;
    }
    System.out.println("计算结果:" + total);
}

16.求解”完数”

public static void main(String[] args) {
        int i,j;
        for(i=1;i<=1000;i++){
            int sum=0;
            for(j=1;j<i;j++){
                if(i%j==0)
                    sum=sum+j;
            }
            if(sum==i)
                System.out.print(i+"  ");
        }
    }

17.球体自由落下物理问题

public static void main(String[] args) {
        double sn=100,hn=sn/2;
        int n;
        for(n=2;n<=10;n++)
        {
            sn=sn+2*hn;/*第n次落地时共经过的米数*/
            hn=hn/2; /*第n次反跳高度*/
            }
        System.out.print("the total of road is "+sn);
        System.out.print("the tenth is "+hn);
    }

18.猴子吃桃问题

public static void main(String[] args) {
        int today=1;
        for(int i=1;i<=9;i++)
        {
            today=(1+today)*2;
        }
        System.out.println(today);
    }    

19.乒乓球比赛抽签问题

public static void main(String[] args) {
    char i,j,k;/*i是a的对手,j是b的对手,k是c的对手*/
    for(i='x';i<='z';i++)
    for(j='x';j<='z';j++) {
        if(i!=j)
        for(k='x';k<='z';k++) {
            if(i!=k&&j!=k) {
                if(i!='x'&&k!='x'&&k!='z')
                System.out.printf("order is a--%c\tb--%c\tc--%c\n",i,j,k);
            }
        }
    }
}

20.打印菱形图案

public static void main(String[] args) {
    int i,j,k;
    for(i=0;i<=3;i++)
    {
        for(j=0;j<=2-i;j++)
        System.out.print(" ");
        for(k=0;k<=2*i;k++)
            System.out.print("*");
        System.out.print("\n");
        }
    for(i=0;i<=2;i++)
    {
        for(j=0;j<=i;j++)
            System.out.print(" ");
        for(k=0;k<=4-2*i;k++)
            System.out.print("*");
        System.out.print("\n");
        }
}

21.分数数列求和

public static void main(String[] args) {
        int n,t,number=20,a=2,b=1;
        float s=0;
        for(n=1;n<=number;n++)
        {
            s=s+a/b;
            t=a;
            a=a+b;
            b=t;
            }
        System.out.print("sum is \n"+s);
    }

22.求1+2!+3!+…+20!的和

public static void main(String[] args) {
        int n,s=0,t=1;
        for(n=1;n<=20;n++)
        {
            t*=n;
            s+=t;
            }
        System.out.print("1+2!+3!...+20!=\n"+s);
    }

23.利用递归方法求5!

public static void main(String[] args) {
        int x = 5;
        long rs = Fac(x);
        System.out.println("" + x + "! = " + rs);
    }
    public static long Fac(int x) {
        if (x > 1)
            return (x * Fac(x - 1));
        else
            return 1;
    }

24.将输入字符以相反顺序打印

public static void main(String[] args) {
        String originalString = "abcdefg";
        String resultString = "";
        char[] charArray = originalString.toCharArray();
        for (int i = charArray.length-1; i>=0; i--){
            resultString +=charArray[i];
        }
        System.out.println(resultString);
    }    

25.岁数问题

public static void main(String[] args) {
        int n;
        int a=age(5);
        System.out.print(age(5));
    }
    public static int age(int n) {
        int c;
        if(n==1) c=10;
        else c=age(n-1)+2;
        return(c);
    }

26.求解正整数位数

public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int n = sc.nextInt();
    int count = 0;
    while(true)
    {
        count++;
        n = n/10;
        if(n==0)
            break;
    }
    System.out.println("用while循环求其字符串的长度len="+count);
}

27.判断回文数

public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("Please input the number: ");
        String word = sc.next();
        int i = word.length();
        int j = 0;
        while (j <= (i / 2) -1 && word.charAt(j) == word.charAt(i - j - 1))
            j++;
        if (j == i / 2)
            System.out.println("Yes it is HuiWen.");
        else
            System.out.println("No it is not HuiWen.");
        sc.close();
    }

28.求100之内的素数

public static void main(String[] args)
    {
        int count = 0;
        for(int i=1; i<100; i+=2) {
            boolean b = false;
            for(int j=2; j<=Math.sqrt(i); j++)
            {
                if(i % j == 0) { b = false; break;
                }
                else
                {
                    b = true;
                }
            }
            if(b == true) {count ++;System.out.println(i );
            }
        }
        System.out.println( "素数个数是: " + count);
    }

29.求3*3矩阵对角线元素之和

public static void main(String[] args) {
        double sum=0;
        int array[][]={{1,2,3},{4,5, 6},{7,7,8}};
        for(int i=0;i<3;i++)
            for(int j=0;j<3;j++){
                if(i==j)
                    sum=sum + array[i][j];
            }
        System.out.println( sum);
    }    

30.数字插入数组重新排序

public static void main(String[] args) {
        int[] b={1,2,3,4,5,6,7,8,9};
        insert(6, b);
    }
    public static void insert(int a,int[] b ){
        int size;
        size=b.length;
        int l=size;
        int[] c=new int[size+1];
        for(int i=0;i<size;i++){
            if(a<b[i]) {
                l=i;
                break;
            }
        }
        for(int i=0;i<l;i++){
            c[i]=b[i];  //将比a小的前面的值放在新的数组c中
        }
        c[l]=a;//将a放在 l 的位置上
        for(int i=l;i<b.length;i++){
            c[i+1]=b[i];    //将比a大的值放在l位置后的数组c中
        }
        System.out.print("原数组 :");
        for(int i=0;i<b.length;i++) {
            System.out.print(b[i]);
        }
        System.out.println("\n");
        System.out.println("插入数字 :\t" +a);
        System.out.println();
        System.out.print("插入后新数组 :");
        for(int i=0;i<c.length;i++) {
            System.out.print(c[i]);
        }
    }    
文章目录
  1. 1. 30个java基础程序
|