我正在接受以下计划的数据排除? [英] I am getting array out of bound expection for the following program?

查看:58
本文介绍了我正在接受以下计划的数据排除?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

public class LeapYear {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
int year=Integer.parseInt(args[0]);
 boolean IsLeapYear;
 IsLeapYear=(year%4==0);
 IsLeapYear=IsLeapYear && (year% 100!=0);
 IsLeapYear=IsLeapYear&&(year % 400 ==0);
 System.out.println(IsLeapYear);
	}

}





我尝试过:



我已宣布LeapYear为公共类

声明为整年。

i将布尔值视为isleapyear

尝试检查我提交的给定年份是否为leapyear。



What I have tried:

I have declared LeapYear as public class
declared an integer year.
i took boolean value as isleapyear
try to check whether the given year i submitted is leapyear or not.

推荐答案

因此,在尝试使用之前,请检查至少有一个参数它!

So check there is at least one argument before you try to use it!
if (args.length > 0) {
    int year = Integer.parseInt(args[0]);
    boolean IsLeapYear;
    IsLeapYear=(year%4==0);
    IsLeapYear=IsLeapYear && (year% 100!=0);
    IsLeapYear=IsLeapYear&&(year % 400 ==0);
    System.out.println(IsLeapYear);
    }


您应该学习尽快使用调试器。而不是猜测你的代码在做什么,现在是时候看到你的代码执行并确保它完成你期望的。



调试器允许你跟踪执行逐行检查变量,你会看到它有一个停止做你期望的点。

调试器 - 维基百科,免费的百科全书 [ ^ ]

掌握Visual Studio 2010中的调试 - A初学者指南 [ ^ ]

http: //docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html [ ^ ]

https://www.jetbrains.com/idea/help/debugging-your -first-java-application.html [ ^ ]



您的代码看起来会像1600年,2000年和2400年一样失败。
You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html[^]
https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html[^]

Your code looks to fail for years like 1600, 2000 and 2400.


这篇关于我正在接受以下计划的数据排除?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆