Java程序题

java程序题

1、求100到200的素数:

public class Sushu {
    public static void main(String[] args) {
        int i,j;
        System.out.println("100到200的素数为:");
        for(i=100;i<=200;i++){
            for(j=2;j<i;j++){
                if(i%j==0)
                    break;
            }
            if(i<=j)
                System.out.println(i);
        }
    }

2、水仙花的算法(例如:153=1*1*1+5*5*5+3*3*3)

public class Shuixianhau {
    public static void main(String[] args) {
        int i,j,k;
        for(int m=100;m<1000;m++){
            i=m%10;
            j=m/10%10;
            k=m/100;
            if((i*i*i+j*j*j+k*k*k)==m){
                System.out.println(m+"是一个水仙花");
            }
        }
    }
}

3、分解质因数

public class Zhiyinshu {
    public static void main(String[] args) {
       Scanner s=new Scanner(System.in);
        System.out.println("请输入一个正整数:");
        int i=s.nextInt();
        int k=2;
        System.out.print(i+"=");
        while(k<=i){
            if(k==i){
                System.out.print(k);break;
            }else if(i%k==0){
                System.out.print(k+"*");i=i/k;
            }else {
                k++;
            }
        }
    }
}

4、成绩等级(?():())

public class Grade{
    public static void main(String[] args) {
       Scanner s=new Scanner(System.in);
        System.out.print("请输入成绩为:");
        int n=s.nextInt();
        char grade;
        grade=n>=90?'A' :n>=60?'B':'C';
        System.out.println("等级为:"+grade);
    }
}

5、求最大公约数与最小公倍数

public class Calcu {
    public static void main(String[] args) {
       Scanner s=new Scanner(System.in);
        System.out.print("输入第一个数为:");
        int a=s.nextInt();
        System.out.print("输入第二数为:");
        int b=s.nextInt();
        GongYueShu g=new GongYueShu();
        int m=g.gongYinShu(a,b);
        int n=a*b/m;
        System.out.println("最大公因数:"+m);
        System.out.println("最小公倍数:"+n);
    }
  static class GongYueShu{
        public int gongYinShu(int x,int y){
            int t;
            if(x<y){
                t=x;
                y=t;
                x=y;
            }
            while(y!=0){
                if(x==y){
                   return x;
                }else {
                    int k=x%y;
                    x=y;
                    y=k;
                }
            }
            return x;
        }

    }
}

6、输入一行字符,分别统计英文字母、空格、数字及其他字符的个数

public class Cacul {
    public static void main(String[] args) {
        int digital=0;
        int character=0;
        int blank=0;
        int other=0;
        char[] ch = null;
        Scanner s = new Scanner(System.in);
        System.out.print("输入一个数:");
        String a = s.nextLine();
        ch = a.toCharArray();
        for (int i = 0; i < ch.length; i++) {
            if ((ch[i]>= '0') && (ch[i]<= '9')) {
                digital++;
            } else if ((ch[i] >= 'a' && ch[i] <= 'z') || (ch[i] >= 'A' && ch[i] <= 'Z')) {
                    character++;
                } else if (ch[i] == ' ') {
                    blank++;
                } else {
                    other++;
                }
            }
            System.out.println("数字个数:" + digital);
            System.out.println("字母个数:" + character);
            System.out.println("空白个数:" + blank);
            System.out.println("其它字符的个数:" + other);
        }

    }

7、求s=a+aa+aaa+aaaa+aaaaa(其中a是一个数字)

public class Total {
    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        System.out.print("输入数a的值:");
        int a=s.nextInt();
        System.out.print("输入数字的项数:");
        int n=s.nextInt();
        int i=0;
        long sum=0;
        int b=0;
       while(i<n){
            b=b+a;
            sum+=b;
            a=a*10;
            ++i;
        }
        System.out.println(sum);
        }
    }

8、求1000以内的完数(完数=它的因子之和)

public class Wanshu {
    public static void main(String[] args) {
        System.out.println("1000以内的完数有:");
        for (int i=1;i<=1000;i++) {
            int t=0;
            for(int j=1;j<=i/2;j++){
                if(i%j==0){
                   t=t+j;
                }
            }
            if(t==i){
                System.out.println(+i);
            }
        }
    }
}

9、一球从100m高度自由落下,每次落地后反跳回高度的一半,再落下,求它在第10次落地时,共经过多少米?第10次反弹多高?

public class Cacul {
    public static void main(String[] args) {
       double s=100,h=100;
       for(int i=1;i<=10;i++){
           s+=h;
           h=h/2;
       }
        System.out.println("总高度为:"+s);
        System.out.println("第10次反弹高度:"+h);
    }
}

10、四个数字:1、2、3、4,能组成多少个互不相同且无重复的数字?分别是哪些?

public class Count {
    public static void main(String[] args) {
        int count=0;
   for(int x=1;x<5;x++){
       for(int y=1;y<5;y++){
           for(int z=1;z<5;z++){
               if(x!=y&&x!=z&&y!=z){
                   count++;
                   System.out.println(x*100+y*10+z);
               }
           }
       }
    }
        System.out.println("总个数为:"+count);
}
}

11、一个正整数加上100后是一个完全平方数,再加上168又是一个完全平方数,该数为多少?

public class Cacul {
    public static void main(String[] args) {
      for(int i=0;i<=10000;i++){
          if((Math.sqrt(i+100)%1==0)&&(Math.sqrt(i+168)%1==0)){
              System.out.println(i);
          }
      }
}
}

12、输入某年某月某日,判断这一天是这一年的第几天

public class Caucal {
    public static void main(String[] args) {
        int year,month,day,e;
        int d=0;
        int days=0;
        do {
            e=0;
            Scanner s = new Scanner(System.in);
            System.out.print("请输入年:");
            year = s.nextInt();
            System.out.print("请输入月:");
            month = s.nextInt();
            System.out.print("请输入日:");
            day = s.nextInt();

            if (year < 0 || day < 0 || day > 31 || month < 0 || month > 12) {
                System.out.println("输入错误,请重输");
                e=1;
            }
        }while(e==1);
        for(int i=1;i<month;i++){
         switch (i){
             case 1:
             case 3:
             case 5:
             case 7:
             case 8:
             case 10:
             case 12:days=31;break;
             case 4:
             case 6:
             case 9:
             case 11:days=30;break;
             case 2:
                 if(year%400==0||(year%4==0&&year%100!=0)){
                     days=29;
                 }else {
                     days=28;
                 }
                 break;
         }
         d=d+days;
        }

        System.out.println(year+"年"+month+"月"+day+"日,是今年的第"+(day+d)+"天");

}
}

13、将三个数由小到大排出

public class SortNum {
    public static void main(String[] args) {
        int x, y, z,t;
        Scanner s = new Scanner(System.in);
        System.out.print("请输入第一个数:");
        x = s.nextInt();
        System.out.print("请输入第二个数:");
        y = s.nextInt();
        System.out.print("请输入第三个数:");
        z = s.nextInt();
        if (x > y) {
            t=x;
            x=y;
            y=t;
        }
        if(x>z){
            t=x;
            x=z;
            z=t;
        }
        if(y>z){
            t=y;
            y=z;
            z=t;
        }
        System.out.println("从小到大排列为:"+x+"<"+y+"<"+z);
    }
}

14、输入九九乘法表

public class JiuJiu {
    public static void main(String[] args) {
       for(int i=1;i<=9;i++){
           for(int j=1;j<=i;j++){
               System.out.print(i+"*"+j+"="+i*j+" ");
           }
           System.out.println();
       }
    }
}

15、打印如下图
这里写图片描述

public class PrintTrian {
    public static void main(String[] args) {
        int H = 7, W = 7;
        for (int i = 1; i <= (H +1)/2; i++) {
            for (int j = 1; j <= (W+1) / 2 - i; j++) {
                System.out.print(" ");
            }
            for(int k=1;k<=2*i-1;k++) {
                System.out.print("*");
            }
            System.out.println();
        }
        for(int i=1;i<=H/2;i++){
            for(int j=1;j<=i;j++){
                System.out.print(" ");
            }
            for(int k=1;k<=W-2*i;k++){
                System.out.print("*");
            }
            System.out.println();
        }

    }
}

16、有一分数序列:2/1,3/2,5/3,8/5,13/8,21/13…求出这个序列的20项之和

public class Total{
    public static void main(String[] args) {
       int x=2,y=1,t;
       double sum=0;
       for(int i=1;i<=20;i++){
           sum=sum+(double)x/y;
           t=y;
           y=x;
           x=y+t;
       }
        System.out.println(sum);
    }
}

17、求1!+2!+3!+4!+…+20!的和

public class Total {
    public static void main(String[] args) {
       int s=1;
       int sum=0;
       for(int i=1;i<=20;i++){
           s*=i;
           sum+=s;
       }
        System.out.println(sum);
    }
}

18、利用递归算法求5!

public class Total    {
    public static void main(String[] args) {
        rec f=new rec();
        int n=5;
        System.out.println(f.rec(n));

    }
    static class rec {
        public long rec(int n) {
            long value = 0;
            if (n == 1) {
                value = 1;
            } else {
                value = n * rec(n - 1);
            }
            return value;
        }
    }
}

19、给一个不多于5位的正整数,要求:求它是几位数,并且逆序打印出各数字

public class Test {
    public static void main(String[] args) {
       Scanner s=new Scanner(System.in);
        System.out.print("请输入一个正整数:");
        String ss=s.nextLine();
        char[] ch=ss.toCharArray();
        System.out.println("该数的长度为:"+ch.length);
        for(int i=ch.length-1;i>=0;i--){
            System.out.println(ch[i]);
        }
    }
}

20、判断回文数,例如:12321

public class HuiWen {
    public static void main(String[] args) {
        boolean is=false;
        Scanner s = new Scanner(System.in);
        System.out.print("请输入一个数:");
        String ss = s.nextLine();
        char[] ch = ss.toCharArray();
        int j = ch.length;
        for (int i = 0; i < j / 2; i++) {
            if (ch[i] == ch[j - i - 1]) {
                is=true;
            }
        }
        if(is==true){
            System.out.println("这是一个回文数");
        }else {
            System.out.println("这不是一个回文数");
        }
    }
}

21、请输入星期几的第一个字母来判断一下是星期几,如果第一个字母一样,则继续判断第二个字母

public class Test{
    public static void main(String[] args) {
        Scanner s=new Scanner(System.in);
        System.out.print("请输入第一个大写字母:");
        String s1=s.nextLine();
        switch (s1){
            case "M":
                System.out.println("Monday");
                break;
            case "W":
                System.out.println("Wednesday");
                break;
            case "F":
                System.out.println("Friday");
                break;
            case "T": {
                System.out.print("请输入第二个字母");
                String s2 = s.nextLine();
                char a=s2.charAt(0);
                if (a== 'u') {
                    System.out.print("Tuesday");
                } else if (a == 'h') {
                    System.out.print("Thursday");
                }else {
                    System.out.println("没有这个字母");
                }
            };
            break;
            case "S": {
                System.out.print("请输入第二个字母");
                String s2 = s.nextLine();
                char a=s2.charAt(0);
                if (a == 'a') {
                    System.out.println("Saturday");
                } else if (a == 'u') {
                    System.out.println("Sunday");
                } else {
                    System.out.println("没有这个字母");
                }
            };
            break;
        }
    }
}

22、求100之内的素数

public class Caucal {
    public static void main(String args[]) {
        int i,j;
        for( i=2;i<=100;i++){
            for(j=2;j<=i/2;j++){
                if(i%j==0)
                    break;
            }
            if(j>i/2){
                System.out.println(i);
            }
        }
    }
}

23、对10个数进行排序

public class Paixu {
    public static void main(String args[]) {
       Scanner s=new Scanner(System.in);
       int[] a=new int[10];
        System.out.print("请输入10个整数:");
       for(int i=0;i<10;i++){
           a[i]=s.nextInt();
       }
        for(int i=0;i<10;i++){
           for(int j=i+1;j<10;j++){
               if(a[i]>a[j]) {
                   int k = a[i];
                   a[i] = a[j];
                   a[j]=k;
               }
           }
       }
        for(int i=0;i<10;i++){
            System.out.print(a[i]+" ");
        }

    }
}

24、输入一个3*3矩阵对角线之和

public class Test {
    public static void main(String args[]) {
       Scanner s=new Scanner(System.in);
        int[][] a=new int[3][3];
        System.out.print("请输入9个数");
        for(int i=0;i<3;i++){
            for(int j=0;j<3;j++){
                a[i][j]=s.nextInt();
                System.out.print(a[i][j]+" ");
            }
            System.out.println();
        }
        int sum=0;
        for(int i=0;i<3;i++) {
            for (int j = 0; j < 3; j++) {
                if(i==j){
                    sum+=a[i][j];
                }
            }
        }
        System.out.println("对角线之和为:"+sum);
            }

    }

25、取一个整数a从右端开始的4~7位

public class Category {
    public static void main(String args[]) {
       Scanner s=new Scanner(System.in);
        System.out.print("请输入7位以上的正整数:");
        long a=s.nextLong();
        String ss=Long.toString(a);
        char[] ch=ss.toCharArray();
        int j=ch.length;
        if(j<7){
            System.out.println("输入错误");
        }else {
            System.out.println(ch[j-7]+" "+ch[j-6]+" "+ch[j-5]+" "+ch[j-4]);
        }

    }

}

26、打印出杨辉三角形

public class PrintYHTraingle {
    public static void main(String args[]) {
      int[][] a=new int[10][10];
      for(int i=0;i<10;i++){
         a[i]=new int[i+1];//行从0开始,列数加一
         for(int k=9;k>i;k--){
             System.out.print(" ");
         }
         for(int j=0;j<=i;j++){
             if(j==0||j==i){
                 a[i][j]=1;
                 System.out.print(a[i][j]+" ");
             }else {
                 a[i][j]=a[i-1][j-1]+a[i-1][j];
                 System.out.print(a[i][j]+" ");
             }
         }
          System.out.println();
      }

    }
 }

27、有n个整数,使其前面各数顺序向后移m个位置,最后m个数变成最前面的m个数

public class Test {
    public static void main(String args[]) {
        int N = 10;
      int[] a=new int[N];
      Scanner s=new Scanner(System.in);
        System.out.print("请输入10个整数:");
        for(int i=0;i<N;i++){
            a[i]=s.nextInt();
        }
        System.out.println("输入后的数组为:");
        for(int i=0;i<N;i++){
            System.out.print(a[i]+" ");
        }
        System.out.print("向后移动的位数为:");
        int n=s.nextInt();
        int[] b=new int[n];
        for(int i=0;i<n;i++){
            b[i]=a[N-n+i];
        }
        for(int i=N-1;i>=n;i--){
            a[i]=a[i-n];
        }
        for(int i=0;i<n;i++){
            a[i]=b[i];
        }
        System.out.println("位移后的数组为:");
        for(int i=0;i<N;i++){
            System.out.print(a[i]+" ");
        }

    }
 }
  • 3
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值