java 更改静态布尔值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13481194/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Changing static boolean
提问by TH3Mitch
I have an assignment for school to make a program which results in either true or false. It's about wether a year is a leap year or not. The problem I have at the moment is that i'm using a public static boolean instead of a public boolean. This is my code:
我有一个学校的任务,要制作一个结果为真或假的程序。这是关于一年是否是闰年。我目前的问题是我使用的是公共静态布尔值而不是公共布尔值。这是我的代码:
public class Assignment {
static boolean isLeapYear;
public static void main(String[] args)
{
int year = 2000;
isLeapYear(year);
}
public static boolean isLeapYear(int year) {
if (((year/100)%4 == 0 && year%4 ==0) || (year % 400 == 0))
isLeapYear = true;
else
isLeapYear = false;
System.out.println(isLeapYear);
return isLeapYear;
}
}
The int year is 2000 at the moment but the rules are like this: A leap year is a year wich can be divided by 4 unless the year is the beginning of a new century (1700, 1800, 1900.....). So even though you can divide 1900 by 4 you can't divide it by 400 so it's false. So again the question: What do I need to do so i'm able to use a public boolean instead of a public static boolean?
整型年目前是 2000 年,但规则是这样的:闰年是可以被 4 整除的年份,除非该年份是新世纪的开始(1700、1800、1900.....)。因此,即使您可以将 1900 除以 4,也不能将其除以 400,因此它是错误的。那么问题又来了:我需要做什么才能使用公共布尔值而不是公共静态布尔值?
采纳答案by Rohit Jain
You would need to create an instance of your class to invoke that method from your main method, if you want to make your method non-static. And then you can make your isLeapYear
variable non-static: -
如果要使方法非静态,则需要创建类的实例以从主方法调用该方法。然后你可以让你的isLeapYear
变量非静态: -
boolean isLeapYear;
public static void main(String[] args)
{
int year = 2000;
new Assigment().isLeapYear(year);
}
public boolean isLeapYear(int year) {
// access isLeapYear as `this.isLeapYear` or just `isLeapYear`
}
But, precisely, you don't need to store your result in a boolean variable. If you want to return a boolean value of some expression, then you can just return that expression.
但是,准确地说,您不需要将结果存储在布尔变量中。如果你想返回某个表达式的布尔值,那么你可以只返回那个表达式。
So, just having this code in your method would also work fine, and it is more readable, and let that method be static: -
因此,只要在您的方法中使用此代码也可以正常工作,并且更具可读性,并且让该方法保持静态:-
return (((year/100)%4 == 0 && year%4 ==0) || (year % 400 == 0))
And from your main call: -
从你的主要电话中: -
System.out.println("Year : " + year + ", is leap year: " + isLeapYear(year));
回答by matt-dot-net
You don't need to store this result anywhere.
您无需将此结果存储在任何地方。
Use:
利用:
public static boolean isLeapYear(int year)
{
return (((year/100)%4 == 0 && year%4 ==0) || (year % 400 == 0));
}
回答by Subin Sebastian
Static methods can only access static variables, only instance methods can access instance methods, which you can infer if you think Object oriented.
静态方法只能访问静态变量,只有实例方法才能访问实例方法,如果你认为面向对象,你可以推断出这一点。
Just in case you should store the Boolean isLeapYear
以防万一你应该存储布尔值 isLeapYear
public class Testing {
boolean isLeapYear;
public static void main(String[] args)
{
int year = 2000;
new Testing().isLeapYear(year);
}
public boolean isLeapYear(int year) {
if (((year/100)%4 == 0 && year%4 ==0) || (year % 400 == 0))
isLeapYear = true;
else
isLeapYear = false;
System.out.println(isLeapYear);
return isLeapYear;
}
}
回答by jlordo
Does your assignment say it has to be stored in a class or instance variable? If not, there is no need for public boolean isLeapYear
or public static boolean isLeapYear
, just return the result of the calculationand store it in a local variable like this:
您的作业是否说它必须存储在类或实例变量中?如果不是,则不需要public boolean isLeapYear
or public static boolean isLeapYear
,只需返回计算结果并将其存储在局部变量中,如下所示:
public static boolean isLeapYear(int year) {
return ((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0);
}
in main
method:
在main
方法中:
int year = 2000;
boolean isLeap = isLeapYear(year);
System.out.println(isLeap);