萌新小白学习遇到问题,求各位大佬帮忙看一下哪里出错了?

ShawdowYang 2017-10-30 12:04:51
这个是题目




package edu.exam;

import java.util.Scanner;

public class Exam1 {

public static void main(String[] args) {
// TODO Auto-generated method stub
String Date;
Scanner in=new Scanner(System.in);
System.out.println("请按照XXXX-XX-XX格式输入一个生日日期");
Date=in.next();
String[] Dates=Date.split("-");
try{
int year=Integer.parseInt(Dates[0]);
int month=Integer.parseInt(Dates[1]);
int day=Integer.parseInt(Dates[2]);
}
catch(Exception e){
System.out.println("输入错误");
}

if((year>=1900)&&(year<=2013)){
if((month)>=1&&(month<=12)){
if((day>=1)&(day<=31)){
System.out.println("您的生日格式正确");
}
else{
System.out.println("日期格式不正确");
}
}
else{
System.out.println("月份格式不正确");
}
}
}

}



这个是出错的地方
...全文
302 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
java小白脸 2017-10-30
  • 打赏
  • 举报
回复
year,month同上
java小白脸 2017-10-30
  • 打赏
  • 举报
回复
你的day变量定义在了try语句块内,成了该语句块的局部变量了,建议你试着对day提前声明!
自由自在_Yu 2017-10-30
  • 打赏
  • 举报
回复
下面用到year,month这些变量的代码要放到try里面
public static void main(String[] args) {
		String Date;
		Scanner in = new Scanner(System.in);
		System.out.println("请按照XXXX-XX-XX格式输入一个生日日期");
		Date = in.next();
		String[] Dates = Date.split("-");
		try {
			int year = Integer.parseInt(Dates[0]);
			int month = Integer.parseInt(Dates[1]);
			int day = Integer.parseInt(Dates[2]);
			if ((year >= 1900) && (year <= 2013)) {
				if ((month) >= 1 && (month <= 12)) {
					if ((day >= 1) & (day <= 31)) {
						System.out.println("您的生日格式正确");
					} else {
						System.out.println("日期格式不正确");
					}
				} else {
					System.out.println("月份格式不正确");
				}
			}
		} catch (Exception e) {
			System.out.println("输入错误");
		}

		
	}
dangyb 2017-10-30
  • 打赏
  • 举报
回复
变量超出了作用域,另外注意一下命名规范
ShawdowYang 2017-10-30
  • 打赏
  • 举报
回复
引用 4 楼 yuxiangaaaaa 的回复:
下面用到year,month这些变量的代码要放到try里面
public static void main(String[] args) {
		String Date;
		Scanner in = new Scanner(System.in);
		System.out.println("请按照XXXX-XX-XX格式输入一个生日日期");
		Date = in.next();
		String[] Dates = Date.split("-");
		try {
			int year = Integer.parseInt(Dates[0]);
			int month = Integer.parseInt(Dates[1]);
			int day = Integer.parseInt(Dates[2]);
			if ((year >= 1900) && (year <= 2013)) {
				if ((month) >= 1 && (month <= 12)) {
					if ((day >= 1) & (day <= 31)) {
						System.out.println("您的生日格式正确");
					} else {
						System.out.println("日期格式不正确");
					}
				} else {
					System.out.println("月份格式不正确");
				}
			}
		} catch (Exception e) {
			System.out.println("输入错误");
		}

		
	}
多谢大神相助,已经可以了
引用 7 楼 o0sxdo0 的回复:
[quote=引用 6 楼 ShawdowYang 的回复:] [quote=引用 1 楼 lqb421484327 的回复:] 你的day变量定义在了try语句块内,成了该语句块的局部变量了,建议你试着对day提前声明!
怎么对year、month、day提前声明[/quote] 在try的外面 int year 这叫声明 在try里面 year=。。。 这叫赋值 声明位置不同,作用域就不同,同样 if,for内声明的变量也不能在外部使用 [/quote] 哦哦,好的。多谢!
残风炫舞 2017-10-30
  • 打赏
  • 举报
回复
引用 6 楼 ShawdowYang 的回复:
[quote=引用 1 楼 lqb421484327 的回复:] 你的day变量定义在了try语句块内,成了该语句块的局部变量了,建议你试着对day提前声明!
怎么对year、month、day提前声明[/quote] 在try的外面 int year 这叫声明 在try里面 year=。。。 这叫赋值 声明位置不同,作用域就不同,同样 if,for内声明的变量也不能在外部使用
ShawdowYang 2017-10-30
  • 打赏
  • 举报
回复
引用 1 楼 lqb421484327 的回复:
你的day变量定义在了try语句块内,成了该语句块的局部变量了,建议你试着对day提前声明!
怎么对year、month、day提前声明

51,411

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧