上机练习

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

package tk;
public class work {
    public static void main(String[] args) {
        int x,y,z;
        System.out.println("水仙花数");
        for(int i=100;i<=999;i++){
            z=i/100;
            y=(i%100)/10;
            x=i%10;
            if(i==x*x*x+y*y*y+z*z*z){
                System.out.println(i);
            }
        }
    }
}

 

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

 

 

package tk;
public class work {
    public static void main(String[] args) {
         for(int x=1;x<=6;x++)
            {
                for(int y=1;y<=x;y++)
                    System.out.printf("%d",y);
                System.out.print("\n");
            }
            System.out.print("\n");
    }
}
package tk;
public class work {
    public static void main(String[] args) {
         for(int x=6;x>=1;x--)
            {
                for(int y=1;y<=x;y++)
                    System.out.printf("%d",y);
                System.out.print("\n");
            }
            System.out.print("\n");
    }
}
package tk;
public class work {
    public static void main(String[] args) {
         for(int x=1;x<=6;x++)
            {
                for(int y=x;y>=1;y--)
                    System.out.printf("%d",y);
                System.out.print("\n");
            }
            System.out.print("\n");
    }
}
package tk;
public class work {
    public static void main(String[] args) {
        for(int x=6;x>=1;x--)
        {
            for(int y=1;y<=2*(6-x);y++)
                System.out.print(" ");
            for(int y=1;y<=x;y++)
                System.out.printf("%d ",y);
            System.out.print("\n");
        }
    }
}

 

 

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

package tk;
import java.util.Scanner;
public class work {
    public static void main(String[] args) {
        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 total=0;
        for(int i=1;i<month;i++){
            switch(i){
            case 4:
            case 6:
            case 9:
            case 11:
                total +=30;
                break;
            case 2:
                if(year%4==0&&year%100!=0||year%400==0)
                    total +=29;
                else
                    total +=28;
                default:
                    total +=31;
                    break;
            }
        }
        total +=day;
        System.out.println("该天是第"+total+"天");
    }
}

 

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

package tk;
import java.util.Scanner;
public class work {
    public static void main(String[] args) {
        System.out.println("输入一个四位数");
        Scanner input=new Scanner(System.in);
        int i=input.nextInt();
        if(i>999&&i<=9999){
            int a=i%10;
            int b=i%100/10;
            int c=i%1000/100;
            int d=i/1000;
            int sum=d+c*10+b*100+a*1000;
            System.out.println("反转后的数字"+sum);
            }else{
                System.out.println("不是四位数");
            }
        }
    }

 

posted @ 2020-04-02 12:10  rookrisk  阅读(108)  评论(0编辑  收藏  举报