如何打印方法?

时间:2018-11-25 07:02:16

标签: java

如何将methodA输出到我的主要方法?

public class A{
    public static void methodA(int c){
        int b = 7;
        System.out.println("output: " + b + " and " + c);
    }
    public static void main(String args[]){
        System.out.println(methodA()); // the error that I recieve is cannot be applied to ()
        System.out.println(c); // the error that I receive is Expression expected
        System.out.println(methodA); // the error that I recieve is Expression expected
    }
}

我的代码复杂得多,但这是我需要帮助的概述。如何将methodA中的信息打印到main方法中?

我一直遇到的错误是

2 个答案:

答案 0 :(得分:1)

不能。

如果需要打印大量信息以了解应用程序的流程,则应调查日志记录。我建议使用nlog或log4net,这两个都是在运行代码的计算机上登录的好选择。

如果需要集中式日志记录,请查看Serilog和Elastichsearch。

我想你问的是艾哈迈德(Ahmed),是如何打印传递给methodA的参数,这很简单。

public class A
{
    public static void methodA(int c)
    {
       int b = 7;
       System.out.println("output: " + b + " and " + c);
    }

    public static void main(String args[])
    {
       int c = 12;
       methodA(12);
       System.out.println(c); 
    }
}

答案 1 :(得分:0)

由于您的方法需要一个整数变量作为参数。您必须传递一个整数作为参数来执行methodA方法。例如:

public class A{
    public static void methodA(int c){
       int b = 7;
       System.out.println("output: " + b + " and " + c);
    }
    public static void main(String args[]){
        int integerVariable = 5;
        methodA(integerVariable); 
    }
 }
  

注意:由于给定的方法是void类型,因此您不能编写   System.out.println(methodA(integerVariable))在main方法内部。

相关问题