首页 > 解决方案 > 公共方法不返回变量

问题描述

我是 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;
}

标签: javareturnreturn-value

解决方案


它确实返回它,但您既不分配值,也不使用它。

尝试这个:

public static void main(String[] args) {
    int result = counter("this is a test");
    System.out.println("result = " + result);
}

推荐阅读