永发信息网

java编程,输入月份判断季节

答案:6  悬赏:30  手机版
解决时间 2021-03-09 11:41
  • 提问者网友:十年饮冰
  • 2021-03-09 04:17
程序如下
import java.io.*;
class Season{
public static void main(String args[])
throws IOException{
int month;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("请输入月份:");

month=br.read();
String season;
if(month==12||month==1||month==2)
season="冬天";
else if(month==3||month==4||month==5)
season="春天";
else if(month==6||month==7||month==8)
season="夏天";
else if(month==9||month==10||month==11)
season="秋天";
else season="不存在的月份";
System.out.println(month+"月份是"+season);
}
}

问题是输入月份:1
之后,显示的是:49月份是不存在的月份
最佳答案
  • 五星知识达人网友:污到你湿
  • 2021-03-09 04:40
BufferedReader的read()方法读的是一个字符,你用int的变量去接收得到的是49啊,char型的1值等于int的49
month = br.read();
改为:month = br.read() - '0'; 或者 string s = br.readLine(); month = Integer.parseInt(s);
试试吧^_^
不过第一种该法你输入两位数的话,还是不对;还是第二种方法好些。
全部回答
  • 1楼网友:街头电车
  • 2021-03-09 06:42
你从system.in中read出来的是字符型的1,他的ascii编码,或者说是数值编码为0x31,即49。 如果你要使用int型变量判断,在判断以前减48,这样就对了。
  • 2楼网友:山河有幸埋战骨
  • 2021-03-09 06:17
你输入其它月份,也会显示不存在。 month=br.read(); 这样直接赋值不对。
  • 3楼网友:罪歌
  • 2021-03-09 06:00

import java.util.scanner;

public class prime {  public static void main(string[] args) {   double year=0.0;      system.out.println("输入年份:");   year = new scanner(system.in).nextdouble();      //说明是正整数   if(year==(int)year)   {    //判断是否为闰年    if((year%400==0)||(year%100!=0&&year%4==0))    {     system.out.println("是闰年!");    }    else    {     system.out.println("不是闰年!");    }   }   else   {    system.out.println("非整数年份,无法判断!");   }  } }

  • 4楼网友:轻熟杀无赦
  • 2021-03-09 05:49
代码喝注释如下: public static void main(String[] args) {  System.out.print("Please input the month to check:");  int month = new Scanner(System.in).nextInt();//月份//月份不在1~12的情况,提醒输入错误  if (month <= 0 || month > 12) {   System.out.println("Error! month must be between 1 and 12!");  }  //1-3月是春天 else if (month <= 3) {   System.out.println("Month " + month + " is in Spring!");  }  //4-6月是夏天 else if (month <= 6) {   System.out.println("Month " + month + " is in Summer!");  }  //7-9月是秋天 else if (month <= 9) {   System.out.println("Month " + month + " is in Autumn!");  }  //10-12月是冬天 else {   System.out.println("Month " + month + " is in Winter!");  } }
  • 5楼网友:洒脱疯子
  • 2021-03-09 05:01
import java.io.*; public class Season { public static void main(String args[]) throws IOException { String month; BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); System.out.println("请输入月份:"); month = br.readLine(); String season; if (month.equals("12") || month.equals("1") || month.equals("2")) season = "冬天"; else if (month.equals("3") || month.equals("4") ||month.equals("5")) season = "春天"; else if (month.equals("6")|| month.equals("7") || month.equals("8")) season = "夏天"; else if (month.equals("9") || month.equals("10") ||month.equals("11")) season = "秋天"; else season = "不存在的月份"; System.out.println(month + "月份是" + season); } }
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯