如何在java的main方法中声明一个具有2个不同类型参数的方法?

发布于 2024-10-27 19:51:10 字数 887 浏览 0 评论 0原文

如果我有这个方法: 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 ");
    }
  }
}

If i have this method: public static int numberMonth(int parseMonth, String leapYear)

how would i print it out in this method:

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

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

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

发布评论

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

评论(2

醉生梦死 2024-11-03 19:51:10

看看您之前的问题和代码片段,我认为您需要阅读类似 Oracle/Sun Java 教程的内容: http://download.oracle.com/javase/tutorial/java/index.html
其实一切都有答案。还有更多。

Looking on your previous questions and code snippets I think you need to read something like Oracle/Sun Java Tutorial: http://download.oracle.com/javase/tutorial/java/index.html
There are all answers in fact. And much more.

猫腻 2024-11-03 19:51:10

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

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

其中 anIntint > 和 aString 是一个字符串。您还可以使用特定值来执行此操作,如下所示:

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

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

例如,在您的 相关问题,其中详细展示了 numberMonth 函数,虽然很多东西都很突出,但最引人注目的细节是使用 String为您的 leapYear 值。当您处理真或假的信息时,您需要使用布尔数据类型。布尔变量只能包含两个值: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
    }
}

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

The correct way to do what you've asked is to change System.out.println(numberMonth) to the following:

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

Where anInt is an int and aString is a string. You could also do this with specific values, like so:

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

There's a much larger issue at play here, being that it seems you lack a foundation in the most basic aspects of Java syntax. I would highly recommend taking a class, checking out an online tutorial, or getting a book to learn the basics of computer programming in general and the Java language more specifically.

For instance, in your related question where you show the numberMonth function in detail, while a lot of things stand out, the most striking detail is using a String for your leapYear value. When you're dealing with information that is either true or false, you want to use the boolean data type. Boolean variables can only contain two values: true or false. So, rather than storing a string with the values "leap" or "no leap", you could declare a boolean variable. Here's a brief example:

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

Take the time now to learn these basic, fundamental techniques. It will save you a mountain of frustration and wasted time in the future.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文