1.创建一个英雄联盟英雄类,其属性有:name,sex,city,行为有speak(如我叫德玛西亚之力,性别男,来自德玛西亚),并写一个测试类
2.编写程序输入一个年份和月份,并且输出该月份有多少天
2、参考代码:
/*输入一个年份和月份,输出该月的天数*/
#include<stdio.h>
int main()
{
int year, month;
printf("输入年和月(用空格分隔):\n");
scanf_s("%d %d", &year, &month);
switch (month)
{
case 2:if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0))
printf("29天!\n");
else
printf("28天!\n");
break;
case 4:
case 6:
case 9:
case 11:printf("30天!\n"); break;
default:printf("31天!\n"); break;
}
return 0;
}
第一题:
import java.util.stream.StreamSupport;
public class Hero {
String name;
String sex;
String city;
public Hero(String name, String sex, String city) {
this.name = name;
this.sex = sex;
this.city = city;
}
public void speak() {
System.out.println("我叫" + this.name + ", 性别" + this.sex + ", 来自" + this.city);
}
public static void main(String[] args) {
Hero h = new Hero("德玛西亚之力", "男", "德玛西亚");
h.speak();
}
}
/*
输出结果:
我叫德玛西亚之力, 性别男, 来自德玛西亚
Process finished with exit code 0
*/
第二题:
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();
int day =0;
switch (month){
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
day =31;
break;
case 4:
case 6:
case 9:
case 11:
day =30;
break;
case 2:
if((year%4==0)&&(year%100!=0)||(year%400==0)){
day=29;
}
else{
day=28;
}
break;
}
System.out.println(year+"年"+month+"月有"+day+"天");
}
}
/*
请输入年份:
2022
请输入月份:
12
2022年12月有31天
Process finished with exit code 0
*/