循环

来源:互联网 发布:JAVA中的maven是什么 编辑:程序博客网 时间:2024/04/29 12:30

        今天课程的主要内容是循环,包括while、for、do…while都有练习,掌握情况不怎么好,下面记个例子:

分别输入年、月、日,计算这个年月日是一年中的第几天。

import javax.swing.JOptionPane;



public class Test4 {


public static void main(String[] args) {
String x=JOptionPane.showInputDialog(null,"请输入年");
int year=Integer.parseInt(x);
String y=JOptionPane.showInputDialog(null,"请输入月");
int month=Integer.parseInt(y);
String c=JOptionPane.showInputDialog(null,"请输入日");
int date=Integer.parseInt(c);
int d=0;
switch(month){
case 12:
d+=30;
case 11:
d+=31;
case 10:
d+=30;
case 9:
d+=31;
case 8:
d+=31;
case 7:
d+=30;
case 6:
d+=31;
case 5:
d+=30;
case 4:
d+=31;
case 3:
if(year%4==0&&year%100!=0||year%400==0){
d+=29;
}
else{
d+=28;
}
case 2:
d+=31;
case 1:
d+=date;
JOptionPane.showMessageDialog(null,year+"年"+month+"月"+date+"日"+"是这一年的第"+d+"天");
}

0 0
原创粉丝点击