我是 Java 新手,为什么我的方法没有返回我输入的变量。我想使用返回的东西,我不想使用 sysout 但它不起作用。
public static void main(String[] args) {
counter("this is a test");
}
public static int counter(String sentence) {
int count = 0;
while (CODE DELETED){
count=count+1;
MORE CODE DELETED
}
CODE DELETED
return count;
}
最佳答案
它确实返回它,但您既不分配该值,也不使用它。
试试这个:
public static void main(String[] args) {
int result = counter("this is a test");
System.out.println("result = " + result);
}
关于java - 公共(public)方法不返回变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54341454/