Java 如何打印方法?
如何将methodA输出到主方法Java 如何打印方法?,java,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
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方法
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
method。例如:
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))
主方法内部
你不能
如果需要打印大量信息以了解应用程序的流程,那么应该查看日志记录。我建议使用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);
}
}
您需要像下面这样将参数传递给methodA
System.out.println(methodA(1))代码>从methodA返回字符串,并在Main中打印该值。请阅读并相应增强您的问题。请举例说明您想要打印的内容。