JAVA 4.2上机练习

1. 打印出所有的"水仙花数",所谓"水仙花数"是指一个三位数,其各位数字立方和等于该数本身。例如:153是一个"水仙花数",因为153=1的三次方+5的三次方+3的三次方。(知识点:循环语句、条件语句)。

public class blue {
    public static void main(String[] args) {
        // TODO 自动生成的方法存根
        int g,s,b;
        for(int i=100;i<1000;i++) {
            b=i/100;
            s=i/10%10;
            g=i%10;
            if(g*g*g+s*s*s+b*b*b==i)
                System.out.println("水仙花数为:"+i);
        }
    }
}

2.

在控制台输出以下图形(知识点:循环语句、条件语句)

 

 

public class blue {
    public static void main(String[] args) {
        // TODO 自动生成的方法存根
        for(int i=1;i<7;i++) {
            for(int j=1;j<=i;j++) {
               System.out.print(+j);
            }
            System.out.println();
            
        }
    }
}
public class blue {
    public static void main(String[] args) {
        // TODO 自动生成的方法存根
        for(int i=6;i>0;i--) {
            for(int j=1;j<=i;j++) {
               System.out.print(+j);
            }
            System.out.println();
            
        }
    }
}
public class blue {
    public static void main(String[] args) {
        // TODO 自动生成的方法存根
        for(int i=1;i<7;i++) {
            for(int j=i;j>0;j--) {
               System.out.print(+j);
            }
            System.out.println();
            
        }
    }
}

3. 输入年月日,判断这是这一年中的第几天(知识点:循环语句、条件语句)

import java.util.*;
public class blue {
    public static void main(String[] args) {
        // TODO 自动生成的方法存根
        Scanner input=new Scanner(System.in);
        System.out.println("year");
        int year=input.nextInt();
        System.out.println("month");
        int month=input.nextInt();
        System.out.println("day");
        int day=input.nextInt();
        int a=0;
        for(int i=1;i<month;i++){
            switch(i) {
            case 4:
            case 6:
            case 9:
            case 11:
                a+=30;
                break;
            case 2:
                if(year%4==0&&year%100!=0||year%400==0)
                    a+=29;
                else
                    a+=28;
                break;
            default:
                a+=31;
                break;
                    
            
            }
        
        }
        a+=day;
        System.out.println("该天是第"+a+"天");
    }
}

4.由控制台输入一个4位整数,求将该数反转以后的数,如原数为1234,反转后的数位4321(知识点:循环语句、条件语句)

 

import java.util.*;
public class blue {
    public static void main(String[] args) {
        // TODO 自动生成的方法存根
        Scanner input=new Scanner(System.in);
        System.out.println("输入一个四位数a:");
        int a=input.nextInt();
        if(a>999 && a<=9999){
            int g = a%10;
            int s = a%100/10;
            int b = a%1000/100;
            int q = a/1000;
            int sum = q + b*10 +s*100 +g*1000;
            System.out.println(sum);
            }
    }
}

 

posted @ 2020-04-02 12:47  你的名字!  阅读(202)  评论(0编辑  收藏  举报