java第五次上机

1. 打印出所有的"水仙花数

package week4;
import java.util.Scanner;
public class Work1 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		//打印出所有的"水仙花数",所谓"水仙花数"是指一个三位数,其各位数字立方和等于该数本身
		Scanner input=new Scanner(System.in);
		int g,s,b,he;
		  for(int i=100;i<=999;i++){
			  g=i%10;
			  s=i%100/10;
			  b=i/100;
			  he=g*g*g+s*s*s+b*b*b;
			  if(he==i){
				  System.out.println(i);
			  }
	        }
	    }
	}

  2.在控制台输出以下图形

(1)

package week4;

public class Work2 {

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

(2)

package week4;

public class Work22 {

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

(3)

package week4;

public class Work23 {

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

}

(4)

package week4;

public class Work23 {

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

}
 

3. 输入年月日,判断这是这一年中的第几天

package week4;

import java.util.Scanner;

public class Work3 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int x = 0;
		Scanner sc = new Scanner(System.in);
		System.out.print("请输入年份");
		int year = sc.nextInt();
		System.out.print("请输入月份");
		int month = sc.nextInt();
		System.out.print("请输入日期");
		int day = sc.nextInt();
		for (int i = 1; i < month; i++) {
			switch (i) {
			case 4:
			case 6:
			case 9:
			case 11:
				x += 30;
				break;
			case 2:
				if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0)
					x += 29;
				else
					x += 28;
				break;
			default:
				x += 31;
				break;
			}
		}
		x += day;
		System.out.println("该天是第" + x + "天");
	}

}

4.由控制台输入一个4位整数,求将该数反转以后的数

package week4;

import java.util.Scanner;

public class Work4 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner input = new Scanner(System.in);
		System.out.println("输入一个四位数");
		int a = input.nextInt();
		int g, s, b, q;
		g = a % 10;
		s = a / 10 % 10;
		b = a / 100 % 10;
		q = a / 1000;
		System.out.println("反转后的四位数是" + g + s + b + q);
	}
}

  

 
原文地址:https://www.cnblogs.com/overCROSS/p/12618910.html