JAVA之路003--if和switch的区别

输入年月日,判断这天是该年份的第几天。

package keshang;
import java.util.Scanner;

public class SumDays {

public static void main(String[] args) {
    // TODO Auto-generated method stub
    Scanner s = new Scanner(System.in);   //创建一个输入对象
    System.out.println("请输入年份"); //提示用户输入
    int year = s.nextInt();  //接收用户输入的数据。存在year

    System.out.println("请输入月份"); //提示用户输入
    int month = s.nextInt();  //接收用户输入的数据。存在month

    System.out.println("请输入日期"); //提示用户输入
    int days = s.nextInt();  //接收用户输入的数据。存在day

    int sum = 0;   //我要找个变量装天数
    final int bigMonth = 31;
    final int middleMonth = 30;
    int smallMonth = 28;
    if((year%4==0 && year%100 !=0) || year%400==0){
        smallMonth = 29;
    }

    if(month==1){
        sum+=days;   //1月份只加天数
    }else if(month==2){
        sum=bigMonth+days;
    }else if(month==3){
        sum=bigMonth+smallMonth+days;
    }else if(month==4){
        sum = bigMonth*2+smallMonth+days;
    }else if(month==5){
        sum= bigMonth*2+smallMonth+middleMonth+days;
    }else if(month==6){
        sum= bigMonth*3+smallMonth+middleMonth+days;
    }else if (month==7){
        sum= bigMonth*3+smallMonth+middleMonth*2+days;
    }else if(month==8){
        sum= bigMonth*4+smallMonth+middleMonth*2+days;
    }else if(month==9){
        sum= bigMonth*5+smallMonth+middleMonth*2+days;
    }else if(month==10){
        sum= bigMonth*5+smallMonth+middleMonth*3+days;
    }else if(month==11){
        sum= bigMonth*6+smallMonth+middleMonth*3+days;
    }else if(month==12){
        sum= bigMonth*6+smallMonth+middleMonth*4+days;
    }else{
        System.out.println("非法输入");
    }
    System.out.print("sum:"+sum);
}

}

switch语句

switch(){
case 1:System.out.println();break;
case 2:System.out.println();break;
case 3:System.out.println();break;
default:语句 break;
}
程序示例

import java.util.Scanner;
public class Days {
public static void main(String[] args){
Scanner s = new Scanner(System.in);
System.out.println(“请输入年份”);
int year = s.nextInt();

System.out.println("请输入月份"); 
int month = s.nextInt(); 

System.out.println("请输入日期"); 
int days = s.nextInt(); 

int sum = 0; 
final int bigMonth = 31;
final int middleMonth = 30;
int smallMonth = 28;
if((year%4==0 && year%100 !=0) || year%400==0){
    smallMonth = 29;
    }
    switch(month){
        case 1:sum=bigMonth+days;break;
        case 2:sum=bigMonth+days;break;
        case 3:sum=bigMonth+smallMonth+days;break;
        case 4:sum = bigMonth*2+smallMonth+days;break;
        case 5:sum= bigMonth*2+smallMonth+middleMonth+days;break;
        case 6:sum= bigMonth*3+smallMonth+middleMonth+days;break;
        case 7:sum= bigMonth*3+smallMonth+middleMonth*2+days;break;
        case 8:sum= bigMonth*4+smallMonth+middleMonth*2+days;break;
        case 9:sum= bigMonth*5+smallMonth+middleMonth*2+days;break;
        case 10:sum= bigMonth*5+smallMonth+middleMonth*3+days;break;
        case 11:sum= bigMonth*6+smallMonth+middleMonth*3+days;break;
        case 12:sum= bigMonth*6+smallMonth+middleMonth*4+days;break;
        default :System.out.println("非法输入");
        }
    System.out.print("是今年的:"+sum+"天");
    s.close();
}

}

你可能感兴趣的:(java之路)