我如何完成我的作业?我迷茫了。

huangapple go评论65阅读模式
英文:

How do I complete my assignment? I am lost

问题

我正在为学校创建一个程序需要我输入一个年份程序应该告诉我这个年份是否是闰年

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner keyboard = new Scanner(System.in);
        System.out.print("---闰年判断---");
        System.out.print("\n\n输入一个年份(整数):");
        int year = keyboard.nextInt();
        
        if (isLeapYear(year)) {
            System.out.println(year + "年是闰年。");
        } else {
            System.out.println(year + "年不是闰年。");
        }
    }

    public static boolean isLeapYear(int year) {
        if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
            return true;
        } else {
            return false;
        }
    }
}
英文:

I am creating a program for school that requires me to input a year, and the program is supposed to tell me whether or not the year is a leap year.

import java.util.Scanner;
public class Main
{
    public static void main (String[] args)
    {
	    Scanner keyboard = new Scanner;
	    System.out.print("---Leap Year---");
	    System.out.print("\n\nEnter a year(whole number)");
	    int year = keyboard.nextInt();
        public static boolean isLeapYear(int year)
    {
	    boolean leapYear;
	    if (year%4==0)
	    System.out.print("year is not a leap year",year);
    }
  }
}

I am not sure how to finish this, and frankly, I am pretty lost. Please help me.

答案1

得分: 2

我相信这会起作用:

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner keyboard = new Scanner(System.in);
        System.out.print("---闰年判断---");
        System.out.print("\n\n请输入一个年份(整数):");
        int year = keyboard.nextInt();
        System.out.println(isLeapYear(year));
    }

    public static boolean isLeapYear(int year) {
        boolean leapYear;
        if (year % 4 == 0) {
            return true;
        } else {
            return false;
        }
    }
}

你需要将方法移到主方法之外,然后调用它并打印结果。此外,你需要告诉Scanner它正在读取什么。

它在做什么?

它正在检查年份是否能被4整除。如果可以,它将返回true,这意味着该年是闰年。在所有其他情况下,它将返回false

你做错了什么?

最初,你的isLeapYear()方法被定义为返回布尔值。然而,你没有返回任何内容。打印不会返回任何内容,所以我们必须修改它以返回布尔值。

例如,当我输入 1964 时,它会返回:

true

这意味着1964年是一个闰年。

英文:

I believe this shall do the trick:

import java.util.Scanner;
public class Main{
    public static void main (String[] args){
        Scanner keyboard = new Scanner(System.in);
        System.out.print("---Leap Year---");
        System.out.print("\n\nEnter a year(whole number)");
        int year = keyboard.nextInt();
        System.out.println(isLeapYear(year));

  }
  public static boolean isLeapYear(int year){
        boolean leapYear;
        if (year%4==0){
            return true;
         }else{
            return false;
          }
    }
}

You need to move the method outside of the main method, then call it and print the result. Furthermore, you need to tell the Scanner what it's reading.

What's it doing?

It's checking whether the year is divisible by 4. If it is, it will return true, which means that the year is a leap year. In all other cases, it will return false.

What did you do wrong?

Originally, your isLeapYear() method was defined to return a boolean. However, you didn't return anything. Printing doesn't return anything, so we must modify it to return a boolean.

For example, when I input 1964, it returns:

true

Which means that 1964 is a leap year.

huangapple
  • 本文由 发表于 2020年10月6日 09:32:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/64218178.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定