【问题标题】:Printing out a non-variable int in a different method in Java在 Java 中以不同的方法打印出非变量 int
【发布时间】:2011-10-12 18:18:54
【问题描述】:

我正在尝试测试我在数组中查找奇数个数的方法是否适用于 System.out.println() 调用。我知道数组本身没有问题,因为我已经使用 toString() 调用成功打印了它。这是我的方法:

public static int ODD(int[] oddnumbers)
{
    int countOdds = 0;
    for(int i = 0; i < oddnumbers.length; i++)
    {
    if(oddnumbers[i] % 2 == 1) // check if it's odd
          countOdds++;        // keep counting
      }
      return countOdds;

}

然后在之前的 main 方法中,我调用了 ODD 并使用 System.out.println 对其进行了测试:

public static void main(String args[])
{

    ODD(randomThirty);  // will find how may numbers in the given numbers (from the array) are ODD numbers and return this count to main method.
    System.out.println("And here are how many odd numbers there are in that array: " + countOdds);

}

基本上我的问题是,如何将return countOdds 放入一个变量中,我可以通过main 方法将其传递到System.out.println() 中打印?

【问题讨论】:

    标签: java string variables


    【解决方案1】:

    要么使用临时变量来存储返回值并打印它,要么在打印语句中包含方法调用。如需更多信息,请参阅Returning a Value from a Method

    【讨论】:

      【解决方案2】:

      只要做:

      System.out.println("And here are how many odd numbers there are in that array: " + ODD(randomThirty));

      【讨论】:

      • 谢谢 Saket,这是有道理的,我只是将 ODD 方法返回的任何内容放入其中。我会记住这一点的,谢谢!
      【解决方案3】:

      在你的主要方法中使用int countOdds = ODD(randomThirty);

      您的函数中的countOdds 变量是该函数的本地变量。 java中函数中定义的变量是局部的而不是全局的。

      【讨论】:

        【解决方案4】:

        你只需要将ODD方法调用的结果赋值给一个变量:

        public static void main(String args[])
        {
            int result = ODD(randomThirty);  // will find how may numbers in the given numbers (from the array) are ODD numbers and return this count to main method.
            System.out.println("And here are how many odd numbers there are in that array: " + result);
        }
        

        【讨论】:

          【解决方案5】:

          您需要将调用 ODD 的返回结果存储在一个变量中,如下所示:

          public static void main(String args[])
          {
          
              int countOdds = ODD(randomThirty);  // will find how may numbers in the given numbers (from the array) are ODD numbers and return this count to main method.
              System.out.println("And here are how many odd numbers there are in that array: " + countOdds);
          }
          

          【讨论】:

            【解决方案6】:

            只需像保存任何其他变量一样保存结果:

            int countOdds = ODD(randomThirty);
            

            【讨论】:

              【解决方案7】:
              public static void main(String args[]) {
                  int countOdds = ODD(randomThirty);
                  ...
              }
              

              【讨论】:

                【解决方案8】:

                要获得返回值,您必须将参数传递给方法并调用它。

                    int ans = ODD(randomThirty); 
                    System.out.println(ans);
                

                这就是你真正需要做的。您可以在传递参数时调用方法并将变量分配给返回的答案。

                【讨论】:

                  猜你喜欢
                  • 2013-10-08
                  • 1970-01-01
                  • 1970-01-01
                  • 2011-04-01
                  • 1970-01-01
                  • 1970-01-01
                  • 2020-02-11
                  • 2021-04-16
                  • 1970-01-01
                  相关资源
                  最近更新 更多