第五周上机练习

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

package the;


public class newcless4 {

    public static void main(String[] args) {
//        1. 打印出所有的"水仙花数",所谓"水仙花数"是指一个三位数,其各位数字立方和等于该数本身。
//        例如:153是一个"水仙花数",因为153=1的三次方+5的三次方+3的三次方。(知识点:循环语句、条件语句)
        int a,b,c;
        for(int i=100;i<1000;i++){
            a=i/100;
            b=i/10%10;
            c=i%10;
            if(i==a*a*a+b*b*b+c*c*c){
                System.out.println(i);
            }
        }
        
    }

}

2.

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

package the;


public class newcless4 {

    public static void main(String[] args) {
//        在控制台输出以下图形(知识点:循环语句、条件语句)
        for(int i=1;i<=6;i++){
            for(int j=1;j<=i;j++){
                System.out.print(j);
            }
            System.out.println();
        }
    }
}
package the;


public class newcless4 {

    public static void main(String[] args) {
//        在控制台输出以下图形(知识点:循环语句、条件语句)
        for(int i=1;i<=6;i++){
            for(int j=1;j<=7-i;j++){
                System.out.print(j);
            }
            System.out.println();
        }
    }
}
package the;


public class newcless4 {

    public static void main(String[] args) {
//        在控制台输出以下图形(知识点:循环语句、条件语句)
        for(int i=1;i<=6;i++){
            for(int g=1;g<=6-i;g++){
                System.out.print(" ");
            }
            for(int j=i;j>0;j--){
                System.out.print(j);
            }
            System.out.println();
        }
    }
}
package the;


public class newcless4 {

    public static void main(String[] args) {
//        在控制台输出以下图形(知识点:循环语句、条件语句)
        for(int i=1;i<=6;i++){
            for(int g=1;g<=i;g++){
                System.out.print(" ");
            }
            for(int j=1;j<=7-i;j++){
                System.out.print(j);
            }
            System.out.println();
        }
    }
}

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

package the;

import java.util.Scanner;

public class newcless4 {

    public static void main(String[] args) {
//        3.输入年月日,判断这是这一年中的第几天(知识点:循环语句、条件语句)
        Scanner input=new Scanner(System.in);
        System.out.println("输入一个年份");
        int year=input.nextInt();
        System.out.println("输入一个月份");
        int month=input.nextInt();
        System.out.println("输入一个日期");
        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;
                }
                break;
            case 1:
            case 3:
            case 5:
            case 7:
            case 8:
            case 10:
            case 12:
                total+=31;
            }
        }
        total+=day;
        System.out.println("这天是这一年中的第"+total+"天");
    }
}

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

package the;

import java.util.Scanner;

public class newcless4 {

    public static void main(String[] args) {
//        4.由控制台输入一个4位整数,求将该数反转以后的数,如原数为1234,反转后的数位4321(知识点:循环语句、条件语句)
        Scanner input=new Scanner(System.in);
        System.out.println("输入一个4位整数");
        int x=input.nextInt();
        if(x<10000&&x>=1000){
            x=x%10*1000+x/10%10*100+x/100%10*10+x/1000;
            System.out.println("取反以后的数为"+x);
        }else{
            System.out.println("这不是一个四位数");
        }
    }
}

 

package the;

public class newcless4 {
public static void main(String[] args) {//在控制台输出以下图形(知识点:循环语句、条件语句)for(int i=1;i<=6;i++){for(int j=1;j<=i;j++){System.out.print(j);}System.out.println();}}}

原文地址:https://www.cnblogs.com/rxy2000/p/12619680.html