如何打印这个布尔值的值? (爪哇)

发布于 2024-12-28 19:20:00 字数 944 浏览 3 评论 0原文

我尝试了几种不同的方法,例如 print(boolean isLeapYear) 和其他一些方法,但我不知道如何让它工作。它总是说我缺少一个类(布尔值是原始的,它需要一个吗?)无论如何,如果 isLeapYear if-else 语句是错误的,我不担心这些..我只需要弄清楚如何打印输出布尔值;非常感谢任何帮助/指出正确方向=]

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;
    }
}

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. 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; 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;
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

风透绣罗衣 2025-01-04 19:20:00

有几个问题。

一是有风格;类名始终大写。这是普遍遵守的 Java 约定。如果不这样做,其他程序员就会感到困惑。

其次,该行

System.out.println(boolean isLeapYear);

是语法错误。删除它。

第三。

您永远不会从主例程中调用该函数。这就是为什么你永远看不到任何回复
到输入。

There are several problems.

One is of style; always capitalize class names. This is a universally observed Java convention. 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.

涙—继续流 2025-01-04 19:20:00
System.out.println(isLeapYear);

应该可以正常工作。

顺便说一下,在

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

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

year % 400 部分永远不会到达,因为 if (year % 4 == 0) && (年份 % 100 == 0) && (year % 400 == 0) 为 true,则 (year % 4 == 0) && (year % 100 == 0) 一定成功了。

也许交换这两个条件或重构它们:

else if ((year % 4 == 0) && (year % 100 == 0))
    isLeapYear = (year % 400 == 0);
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.

Maybe swap those two conditions or refactor them:

else if ((year % 4 == 0) && (year % 100 == 0))
    isLeapYear = (year % 400 == 0);
誰認得朕 2025-01-04 19:20:00

您应该删除布尔变量前面的“布尔值”。

这样做:

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

或者

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

另一件事是你似乎根本没有调用该方法!
该方法和变量都不是静态的,因此,您必须首先创建类的实例。或者,您只需将两者设为静态,然后直接从 maim 方法中调用您的方法即可。

因此,代码中存在一些错误。也许您应该从一个更简单的示例开始,然后对其进行修改,直到它达到您想要的效果为止。

例子:

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;
    }
}

you should just remove the 'boolean' in front of your boolean variable.

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.

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;
    }
}
请止步禁区 2025-01-04 19:20:00

有几种方法可以解决您的问题,但这可能是最简单的:

您的 main 方法是静态的,因此它无法访问实例成员(isLeapYear纠正此问题的一种方法是将字段和方法都设为静态:

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

最后,您实际上并没有调用 isLeapYear 方法(该方法) 。这就是为什么你不看到任何结果)。在 intyear = kboard.nextInt(); 之后添加此行:

isLeapYear(year);

这应该是一个开始。您可以遵循,但现在只需专注于让您的代码正常工作;您可以稍后进行重构。

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:

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). Add this line after 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.

北城半夏 2025-01-04 19:20:00

首先,您的变量“isLeapYear”与方法同名。这只是不好的做法。

其次,您没有将“isLeapYear”声明为变量。
Java 是强类型的,所以你需要一个
布尔值 isLeapYear;
在你的方法的开头。

这个电话:
System.out.println(boolean isLeapYear);
是错的。方法调用中没有声明。

一旦您将 isLeapYear 声明为布尔变量,您可以调用
System.out.println(isLeapYear);

更新:
我刚刚看到它被声明为一个字段。因此,只需删除该行 System.out.println(boolean isLeapYear);
您应该明白,您不能从 main() 方法调用 isLeapYear。您不能从具有实例的静态方法调用非静态方法。
如果你想调用它,你需要添加

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

我真的建议你使用Eclipse,它会让你即时知道此类编译错误,并且这样更容易学习。

First of all, your variable "isLeapYear" is the same name as the method. That's just bad practice.

Second, you're not declaring "isLeapYear" as a variable.
Java is strongly typed so you need a
boolean isLeapYear;
in the beginning of your method.

This call:
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);

UPDATE:
I just saw it's declared as a field. So just remove the line System.out.println(boolean isLeapYear);
You should understand that you can't call isLeapYear from the main() method. 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.

猫烠⑼条掵仅有一顆心 2025-01-04 19:20:00
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;
}
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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文