【问题标题】:How to declare a method with 2 parameters of different types in the main method in java?java - 如何在java的main方法中声明一个带有2个不同类型参数的方法?
【发布时间】:2011-03-26 17:10:56
【问题描述】:

如果我有这个方法:public static int numberMonth(int parseMonth, String leapYear)

我将如何用这种方法打印出来:

public static  void main(String[] args)
{
  Boolean correctDate = false;
  String date;

  while (!correctDate)
  {
    // It is OK to embed the way you called the method checkInput(getInput())
    // but for troubleshooting, it is easier for me to break into smaller steps.

    // Request Date and get user response
    date = getInput();

    // Verfiy that the date entered contains a valid........
    correctDate = checkInput(date);

    // Display meesage to user
    if (correctDate == true)
    {
      System.out.println("The date you entered is: " + date);
      System.out.println(numberMonth); 
      System.out.println("The numerical date: " );
    }
    else
    {
      System.out.println("Please enter valid date ");
    }
  }
}

【问题讨论】:

  • 问题标题和正文似乎不匹配。你能解释一下你的问题吗?
  • int nextmonth = MyClass.numberMonth(month, "some string"); System.out.println("Next month : " + nextmonth);
  • parsemonthleapYear 表示什么?
  • @spookyjon 'public static String jumpYear(String input) and public static int parseMonth(String input)

标签: java methods parameters


【解决方案1】:

查看您之前的问题和代码 sn-ps 我认为您需要阅读 Oracle/Sun Java 教程之类的内容:http://download.oracle.com/javase/tutorial/java/index.html 事实上,所有的答案都有。还有更多。

【讨论】:

    【解决方案2】:

    按照您的要求进行操作的正确方法是将System.out.println(numberMonth) 更改为以下内容:

    System.out.println(numberMonth(anInt, aString));
    

    其中anIntintaString 是一个字符串。您也可以使用特定值执行此操作,如下所示:

    System.out.println(numberMonth(5, "leap"));
    

    这里有一个更大的问题,就是您似乎缺乏 Java 语法最基本方面的基础。我强烈建议您参加课程、查看在线教程或购买一本书来学习一般的计算机编程基础知识以及更具体的 Java 语言。

    例如,在您的related question 中,您详细显示了numberMonth 函数,虽然很多东西都很突出,但最引人注目的细节是使用String 作为您的leapYear 值。当您处理真假信息时,您希望使用boolean data type。布尔变量只能包含两个值:truefalse。因此,您可以声明一个布尔变量,而不是存储具有值"leap""no leap" 的字符串。这是一个简短的例子:

    public static int numberMonth(int parseMonth, boolean leapYear)
    {
        if(leapYear)
        {
            //if leapYear is true, this code will be executed
        }
        else
        {
            //if leapYear is false, this block will be executed
        }
    }
    

    现在花点时间学习这些基本的基本技术。它将为您节省大量的挫败感和未来浪费的时间。

    【讨论】:

    • 我目前正在学习 Java 课程。这是我目前正在研究实施方法的一个项目。该项目的范围是输入 MM/DD/YYYY 并将其转换为月、日年,然后是 365 数字中的日期编号。如果你想看看我来自哪里,我非常愿意与你分享代码
    猜你喜欢
    • 1970-01-01
    • 2012-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-24
    • 1970-01-01
    • 1970-01-01
    • 2011-07-09
    相关资源
    最近更新 更多