1.我家的狗5岁了,5岁的狗相当于人的多大呢?,其实狗的前两年,每一年相当于人的10.5岁,之后每增加一岁就增加4岁,
那么5岁的狗相当于人的年龄就应该是10.5+10.5+4+4+4=33岁.
编写程序获取用户输入狗的年龄(整数),通过程序输出显示相当于人的年龄.如果为负数请提示.
public class demo4 {
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
System.out.println("请输入狗的年龄");
double year = scanner.nextDouble();
if(year<=2&&year>0){
year = 10.5 * year;
System.out.println(year);
} else if(year>2) {
year = 4 * (year - 2) + 21;
System.out.println(year);
}else if(year<0){
System.out.println("请输入正数");
}
}
}
2..判断某一年是否为闰年
通过Scanner 输入一个年份,然后判断概念是否是闰年
闰年判断标准(满足任何一个)
1)如果能够被4整除&&但是不能被100整除
2) 能够被400整除
public class demo1 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入年份");
int year = scanner.nextInt();
if(year%4==0&&year%100!=0||year%400==0){
System.out.println(year+"是闰年");
}else{
System.out.println(year+"不是闰年");
}
}
}
3.根据指定月份,打印该月份所属的季节,3,4,5春季 678夏季 9 10 11秋季 12 ,1,2 冬季
public class demo2_ {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入一个月份");
int month = scanner.nextInt();
if(month==3||month==4||month==5){
System.out.println(month+"月是春季");
}
else if(month==6||month==7||month==8){
System.out.println(month+"月是夏季");
}
else if(month==9||month==10||month==11){
System.out.println(month+"月是秋季");
}
else if(month==12||month==1||month==2){
System.out.println(month+"月是冬季");
}
else
System.out.println(month+"月不是月份");
}
}
4.猴子吃桃问题。猴子第一天摘下若干个桃子,当时就吃了一半,还不过瘾,
就又吃了一个。第二天又将剩下的桃子吃掉一半,又多吃了一个。以后每天都吃
前一天剩下的一半零一个。到第 10 天在想吃的时候就剩一个桃子了,求第一天共
摘下来多少个桃子?
public class demo5 {
public static void main(String[] args) {
int h = 0;
int day =0;
while(h<11){
h+=3;
if(h>11){
break; }
h-=2;
day++;
}
System.out.println(day);
}
}
5.输出奇数100以内最大的5个奇数
public class demo3 {
public static void main(String[] args) {
int count = 0;
for(int i=100;i>0;i--){
if((i%2==1)&&(count!=5)){
System.out.print(i + " ");
count++;
}
}
}
}