简体   繁体   English

来自main的调用方法

[英]Calling methods from main

I have a problem with calling a method. 我在调用方法时遇到问题。 For example, I want the following program to have two methods: 例如,我希望以下程序具有两种方法:

  • the first one is to check if the year is leap or not, 第一个是检查年份是否是leap年,
  • the second one is to display the number of days in the month 第二个是显示当月的天数

I can't call the methods in main . 我无法调用main的方法。

import java.util.Scanner;

public class LeapYearCheck {

    public static void main(String[] args) {
        LeapYearCheck ob = new LeapYearCheck();
        ob.isLeapYear();
        ob.daysInMonth();
    }

    static void isLeapYear() {
        Scanner input = new Scanner(System.in);
        int month = input.nextInt();
        System.out.println("Enter a year: ");
        int year = input.nextInt();
        if (year % 4 == 0 || year % 400 == 0) {
            System.out.println(year + " is leap year:");
        } else {
            System.out.println(year + " is not leap year:");
        }
    }

    static void daysInMonth() {
        Scanner input = new Scanner(System.in);
        System.out.println("Enter a month :");
        int month = input.nextInt();
        int year = 0;
        if (month == 2) {
            System.out.println("There are 29 days in February: " + year + " year");
        } else if (month == 1) {
            System.out.println("The are 31 days in January " + year + " year");
        } else if (month == 2) {
            System.out.println("The are 28 days in February " + year + " year");
        } else if (month == 3) {
            System.out.println("The are 31 days in March " + year + " year");
        } else if (month == 4) {
            System.out.println("The are 30 days in April  " + year + " year");
        } else if (month == 5) {
            System.out.println("The are 31 days in May " + year + " year");
        } else if (month == 6) {
            System.out.println("The are 30 days in June  " + year + " year");
        } else if (month == 7) {
            System.out.println("The are 31 days in July  " + year + " year");
        } else if (month == 8) {
            System.out.println("The are 31 days in August " + year + " year");
        } else if (month == 9) {
            System.out.println("The are 30 days in September  " + year + " year");
        } else if (month == 10) {
            System.out.println("The are 31 days in  October " + year + " year");
        } else if (month == 11) {
            System.out.println("The are 30 days in November " + year + " year");
        } else if (month == 12) {
            System.out.println("The are 31 days in December " + year + " year");
        } else {
            System.out.println("Invalid Month, Please enter a number between 1 & 12 Merci: ");
        }
    }
}

The methods are declared "static". 这些方法被声明为“静态”。 In Java, that means the method is available for the class , not for objects of the class. 在Java中,这意味着该方法可用于该类 ,而不适用于该类的对象。

To be clear: I understand your problem to be that when calling the second method, the year from the first method isn't available anymore, right? 明确一点:我理解您的问题是,在调用第二种方法时,第一种方法的年份不再可用,对吗?

That's because you don't store it in an instance variable, but in a variable local to the method. 那是因为您没有将其存储在实例变量中,而是存储在方法本地的变量中。 Local variables are gone once the method has completed. 方法完成后,局部变量将消失。 Instead, create an instance variable for your class, eg "private int year;". 而是为您的类创建一个实例变量,例如“ private int year;”。 In the first method, then use "this.year = ..." to assign a value to the variable. 在第一种方法中,然后使用“ this.year = ...”为变量分配一个值。 In the second method, use "this.year" to access it. 在第二种方法中,使用“ this.year”进行访问。

Your code will compile and run (I've just tried it), but you shouldn't call static methods via expressions like this: 您的代码将编译并运行(我已经尝试过了),但是您不应该通过这样的表达式来调用静态方法:

LeapYearCheck ob = new LeapYearCheck();
ob.isLeapYear();
ob.daysInMonth();

You should either make these instance methods or call them as static methods, optionally qualifying them with the type name: 应该使这些实例方法, 称他们为静态方法,任选类型名进行限定:

LeapYearCheck.isLeapYear(); // Explicit
daysInMonth(); // Implicit

Calling static methods as if they were instance methods leads to confusion - it looks like it depends on the instance, but it doesn't. 调用静态方法就好像它们是实例方法一样会引起混乱- 看起来它取决于实例,但事实并非如此。

The next oddity is here: 下一个奇怪的地方是:

static void isLeapYear() {
    Scanner input = new Scanner(System.in);
    int month = input.nextInt();
    System.out.println("Enter a year: ");

Your waiting for user input but without telling the user why - and you're then ignoring the month anyway (which makes sense, as it's irrelevant to working out whether a year is a leap year or not). 您正在等待用户输入,但没有告诉用户原因-然后您无论如何都会忽略月份(这很有意义,因为与确定年份是否为a年无关紧要)。 Just get rid of this line: 只需摆脱这一行:

int month = input.nextInt();

Additionally, this logic is broken: 此外,此逻辑已损坏:

if (month == 2) {
    System.out.println("There are 29 days in February: " + year + " year");
} else if (month == 1) {
    System.out.println("The are 31 days in January " + year + " year");
} else if (month == 2) {
    System.out.println("The are 28 days in February " + year + " year");

Then for the daysInMonth method: 然后对于daysInMonth方法:

  • You're not asking about the year 你不是问年份
  • You're not trying to detect leap years before you report that there are 29 days in February 在您报告2月有29天之前,您并没有尝试检测leap年。

Basically the code is a bit of a mess at the moment, but your problem of not being able to call methods from main should not be an issue... 基本上,目前代码有点混乱,但是您无法从main调用方法的问题应该不是问题...

EDIT: As noted, your leap year calculations are wrong anyway - I'm assuming that the calendar side of things is part of the goal of the exercise, but normally it would be better to use Calendar (and its subclasses) or Joda Time to start with. 编辑:如前所述,您的calculation年计算无论如何都是错误的-我假设日历的一面是练习目标的一部分,但通常最好使用Calendar (及其子类)或Joda Time进行计算。从...开始。

This should run. 这应该运行。 Maybe not correctly, but it'll run. 也许不正确,但是它将运行。 Perhaps you're referring to the warning you get on the two method calls in your main method. 也许您指的是在main方法中对两个方法调用的警告。 You see, your two methods are static. 您会看到,您的两个方法是静态的。 A static method doesn't require an object instance to be called. 静态方法不需要调用对象实例。 Yet you're calling them on a LeapYearCheck instance. 但是,您是在LeapYearCheck实例上调用它们的。 This is allowed (it'll delegate to the proper class), just not necessary. 这是允许的(它将委托给适当的类),只是没有必要。

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

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