第六次上机作业

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

package one;


public class text1 {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        
        for(int i=100;i<=999;i++){
            
            int x=i%10;
            int y=i%100/10;
            int z=i/100;
            
            if(i==x*x*x+y*y*y+z*z*z){
                System.out.println("输出的水仙花数是"+i);
            }
            
            
            
        }

    }

}

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

package one;

public class text4 {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        for (int i = 1; i <= 6; i++) {
            for (int j = 1; j <= i; j++) {
                System.out.print(j);
            }
            System.out.print("\n");
        }


}

package one;

public class text5 {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
         for(int i=1;i<=6;i++) {
                for(int j=1;j<=7-i;j++) {
                    System.out.print(j);
                }
                System.out.println();
            }
    }

}


    }

package one;

public class text6 {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        for(int i=1;i<7;i++) {
            for (int k = 1; k <7-i ; k++) {
                System.out.print(" ");
            }
            for(int j=i;j>0;j--) {
                System.out.print(j);
            }
            System.out.println();
        }
    }
    

}

package one;

public class text7 {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        
            for(int i=6;i>0;i--) {
                for(int k=0;k<6-i;k++) {
                    System.out.print(" ");
                }
                for(int j=1;j<=i;j++) {
                    System.out.print(j);
                }
                System.out.println("");
            }
        }
    

}

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

package one;

import java.util.Scanner;

public class text2 {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        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 count=0;
        int days=0;
        if(year>0&&month>0&&day>0&&month<13&&day<32){
            for (int i=1;i<month;i++){
                switch(i){
                case 1:
                case 3:
                case 5:
                case 7:
                case 8:
                case 10:
                case 12:
                    count+=31;
                    System.out.println(days+"31");
                    break;
                case 4:
                case 6:
                case 9:
                case 11:
                    count+=30;
                    System.out.println(days+"30");
                    break;
                case 2:
                    if((year%4==0&&year%1!=0)||(year%400==0)){
                        System.out.println(days+"29");
                    }else{
                        System.out.println(days+"28");
                    }
                    break;
                
                    
                }
            }
            count=count+day;
        }
        System.out.println(year+"年"+month+"月"+day+"日是"+year+"年的第"+
        +count+"天");
    }
        
        
    }

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

package one;
import java.util.Scanner;
public class text3 {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner input=new Scanner(System.in);
        System.out.println("请输入四位整数");
        int num=input.nextInt();
        int []score=new int[4];
        for(int i=0;i<score.length;i++){
            score[i]=num%10;
            num=num/10;
        }
        System.out.println(score[0]*1000+score[1]*100+score[2]*10+
                score[3]);

    }

}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

posted @ 2020-04-02 12:45  窦佳琪  阅读(156)  评论(0编辑  收藏  举报