判别天数
输入年月日(中间用空格隔开),判别出是今年的第几天?(用for语句)
package com.company;
import java.util.Scanner;
public class FOR {
public static void main(String[] args) {
System.out.println("年月日:");
Scanner sc = new Scanner(System.in);
int year = sc.nextInt();
int month = sc.nextInt();
int day = sc.nextInt();
int days=0;
for (int i = 1; i < month; i++) {
if (i == 1 || i == 3 || i == 5 || i == 7 || i == 8 || i == 10 || i == 12) {
days += 31;
}
else if (i == 4 || i == 6 || i == 9 || i == 11) {
days += 30;
}
else if (i == 2) {
if ((year % 4 == 0 & year % 100 != 0) || year % 400 ==0) {
days += 29;
}
else {
days += 28;
}
}
} System.out.println(day + days);
}
}
输入年月日(中间用空格隔开),判别出是今年的第几天?(用case语句)
import java.util.Scanner;
public class Caculation {
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
System.out.printf("请输入年月日");
int year = sc.nextInt();
int month = sc.nextInt();
int day = sc.nextInt();
int today = 0;
boolean rui=(year%4==0 && year%100!=0)||(year%400==0);
for(int i=1;i<month;i++)
{
switch(i)
{
case 1:case 3:case 5:case 7:case 8:case 10:case 12:
today += 31;
break;
case 4:case 6:case 9:case 11:
today += 30;
break;
case 2:
if(rui)
today += 29;
else
today += 28;
break;
}
}
today += day;
System.out.printf("今天是今年的第%d。%n",today);
}
}
输入年月日(中间用空格隔开),判别出是今年的第几天?(用数组实现)
package com.company;
import java.util.Scanner;
public class day_of_year {
public static void main(String[] args) {
System.out.println("请输入年 月 日:");
Scanner sc = new Scanner(System.in);
int year=sc.nextInt();
int month=sc.nextInt();
int day=sc.nextInt();
int sum=day;
int[] a={31,0,31,30,31,30,31,31,30,31,30,31};
if(year%400==0||(year%100!=0&&year%4==0)) {//是闰年
a[1]=29;
}
else{
a[1]=28;
}
for(int i=0;i<month-1;i++){
sum+=a[i];
}
System.out.println("是这年的第"+sum+"天");
}
}
水仙花数
package com.company;
public class Narcissus {
public static void main(String[] args) {
System.out.println("水仙花数为:");
for(int i=100;i<1000;i++)
{
int g=i%10;
int s = i%100/10;
int b =i/100;
if(i==Math.pow(g,3)+Math.pow(s,3)+Math.pow(b,3))
System.out.println(i);
}
}
}
查分数线
package com.company;
import java.util.Scanner;
public class Score {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.printf("请输入想要查询的录取分数线:");
String gradation = sc.next();
switch(gradation)
{
case "二本":
System.out.println("二本分数线:445分");
break; //break结束当前switch,return结束当前方法
case "一本":
System.out.println("一本分数线:555分");
break; //break结束当前switch,return结束当前方法
case "民办本科":
System.out.println("民办本科分数线:350分");
break; //break结束当前switch,return结束当前方法
case "艺术类本科":
System.out.println("艺术类本科分数线:290分");
break; //break结束当前switch,return结束当前方法
case "体育类本科":
System.out.println("体育类本科分数线:280分");
break; //break结束当前switch,return结束当前方法
default: //当以上条件均不满足时执行default
System.out.println("###");
break; //break结束当前switch,return结束当前方法
}
}
}