简体   繁体   English

如何打印出这个布尔值? (JAVA)

[英]How do I print out the value of this boolean? (Java)

I have tried a few different methods, like print(boolean isLeapYear) and a few others, but I can not figure out how to get it to work. 我尝试了一些不同的方法,比如print(boolean isLeapYear)和其他一些方法,但我无法弄清楚如何使它工作。 It always says that I have a missing class (boolean is primitive, does it need one?) Anyways, If the isLeapYear if-else statements are wrong, I'm not worried about those.. I just need to figure out how to print out the value of the boolean; 它总是说我有一个缺少的类(布尔是原始的,它需要一个吗?)无论如何,如果isLeapYear if-else语句是错误的,我不担心那些..我只需要弄清楚如何打印超出布尔值; any help / point in the right direction is greatly appreciated =] 任何帮助/点在正确的方向非常感谢=]

import java.util.Scanner;

public class booleanfun    {
    boolean isLeapYear;

    public static void main(String[] args)
    {
        System.out.println("Enter a year to determine if it is a leap year or not: ");
        Scanner kboard = new Scanner(System.in);
        int year = kboard.nextInt();
    }
public boolean isLeapYear(int year)
  {
    if (year % 4 != 0)
        isLeapYear = false;

    else if ((year % 4 == 0) && (year % 100 == 0))
        isLeapYear = false;

    else if ((year % 4 == 0) && (year % 100 == 0) && (year % 400 == 0))
        isLeapYear = true;

    else
        isLeapYear = false;

System.out.println(isLeapYear);
System.out.println(boolean isLeapYear);

    return isLeapYear;
    }
}

There are several problems. 有几个问题。

One is of style; 一个是风格; always capitalize class names. 总是大写班级名称。 This is a universally observed Java convention. 这是一种普遍观察到的Java约定。 Failing to do so confuses other programmers. 不这样做会让其他程序员感到困惑。

Secondly, the line 其次,线

System.out.println(boolean isLeapYear);

is a syntax error. 是语法错误。 Delete it. 删除它。

Thirdly. 第三。

You never call the function from your main routine. 您永远不会从主程序中调用该函数。 That is why you never see any reply to the input. 这就是为什么你永远不会看到对输入的任何回复。

 System.out.println(isLeapYear); 

should work just fine. 应该工作得很好。

Incidentally, in 顺便说一下,在

 else if ((year % 4 == 0) && (year % 100 == 0)) isLeapYear = false; else if ((year % 4 == 0) && (year % 100 == 0) && (year % 400 == 0)) isLeapYear = true; 

the year % 400 part will never be reached because if (year % 4 == 0) && (year % 100 == 0) && (year % 400 == 0) is true, then (year % 4 == 0) && (year % 100 == 0) must have succeeded. year % 400部分永远不会达到,因为如果(year % 4 == 0) && (year % 100 == 0) && (year % 400 == 0)为真,那么(year % 4 == 0) && (year % 100 == 0)必须成功。

Maybe swap those two conditions or refactor them: 也许交换这两个条件或重构它们:

else if ((year % 4 == 0) && (year % 100 == 0))
    isLeapYear = (year % 400 == 0);

you should just remove the 'boolean' in front of your boolean variable. 你应该删除布尔变量前面的'boolean'。

Do it like this: 像这样做:

boolean isLeapYear = true;
System.out.println(isLeapYear);

or 要么

boolean isLeapYear = true;
System.out.println(isLeapYear?"yes":"no");

The other thing ist hat you seems not to call the method at all! 另一件事是你似乎根本不打电话给这个方法! The method and the variable are both not static, thus, you have to create an instance of your class first. 方法和变量都不是静态的,因此,您必须首先创建类的实例。 Or you just make both static and than simply call your method directly from your maim method. 或者您只是将两者都设置为静态,而不是直接从您的maim方法调用您的方法。

Thus there are a couple of mistakes in the code. 因此代码中存在一些错误。 May be you shoud start with a more simple example and than rework it until it does what you want. 可能是你从一个更简单的例子开始,而不是重做它,直到它做你想要的。

Example: 例:

import java.util.Scanner;

public class booleanfun    {
    static boolean isLeapYear;

    public static void main(String[] args)
    {
        System.out.println("Enter a year to determine if it is a leap year or not: ");
        Scanner kboard = new Scanner(System.in);
        int year = kboard.nextInt();
        isLeapYear(year);
    }
    public static boolean isLeapYear(int year) {
        if (year % 4 != 0)
        isLeapYear = false;

        else if ((year % 4 == 0) && (year % 100 == 0))

        isLeapYear = false;

        else if ((year % 4 == 0) && (year % 100 == 0) && (year % 400 == 0))
            isLeapYear = true;

        else
            isLeapYear = false;

        System.out.println(isLeapYear);

        return isLeapYear;
    }
}

There are a couple of ways to address your problem, however this is probably the most straightforward: 有几种方法可以解决您的问题,但这可能是最直接的:

Your main method is static, so it does not have access to instance members ( isLeapYear field and isLeapYear method. One approach to rectify this is to make both the field and the method static as well: 您的main方法是静态的,因此它无权访问实例成员( isLeapYear字段和isLeapYear方法。纠正这种情况的一种方法是使字段和方法都是静态的:

static boolean isLeapYear;
/* (snip) */
public static boolean isLeapYear(int year)
{
  /* (snip) */
}

Lastly, you're not actually calling your isLeapYear method (which is why you're not seeing any results). 最后,您实际上并未调用isLeapYear方法(这就是您没有看到任何结果的原因)。 Add this line after int year = kboard.nextInt(); int year = kboard.nextInt();之后添加这一行int year = kboard.nextInt(); :

isLeapYear(year);

That should be a start. 这应该是一个开始。 There are some other best practices you could follow but for now just focus on getting your code to work; 您可以遵循一些其他最佳实践,但现在只关注让您的代码工作; you can refactor later. 你以后可以重构。

First of all, your variable "isLeapYear" is the same name as the method. 首先,您的变量“isLeapYear”与方法的名称相同。 That's just bad practice. 这只是不好的做法。

Second, you're not declaring "isLeapYear" as a variable. 其次,你没有将“isLeapYear”声明为变量。 Java is strongly typed so you need a boolean isLeapYear; Java是强类型的,因此你需要一个boolean isLeapYear; in the beginning of your method. 在你的方法的开头。

This call: System.out.println(boolean isLeapYear); 这个调用: System.out.println(boolean isLeapYear); is just wrong. 是错的。 There are no declarations in method calls. 方法调用中没有声明。

Once you have declared isLeapYear to be a boolean variable, you can call System.out.println(isLeapYear); 一旦将isLeapYear声明为布尔变量,就可以调用System.out.println(isLeapYear);

UPDATE: I just saw it's declared as a field. 更新:我刚看到它被声明为一个字段。 So just remove the line System.out.println(boolean isLeapYear); 所以只需删除System.out.println(boolean isLeapYear);System.out.println(boolean isLeapYear); You should understand that you can't call isLeapYear from the main() method. 您应该明白,您不能从main()方法调用isLeapYear。 You cannot call a non static method from a static method with an instance. 您不能使用实例从静态方法调用非静态方法。 If you want to call it, you need to add 如果要调用它,则需要添加

booleanfun myBoolFun = new booleanfun();
System.out.println(myBoolFun.isLeapYear);

I really suggest you use Eclipse, it will let you know of such compilation errors on the fly and its much easier to learn that way. 我真的建议你使用Eclipse,它会让你知道这些编译错误,并且更容易学习。

public boolean isLeapYear(int year)
{
    if (year % 4 != 0){
        isLeapYear = false;
        System.out.println("false");
    }
    else if ((year % 4 == 0) && (year % 100 == 0)){
        isLeapYear = false;
        System.out.println("false");
    }
    else if ((year % 4 == 0) && (year % 100 == 0) && (year % 400 == 0)){
        isLeapYear = true;
        System.out.println("true");
    }
    else{
        isLeapYear = false;
        System.out.println("false");
    }
    return isLeapYear;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM