简体   繁体   English

为什么此方法不返回值

[英]Why this method doesn't return the value

public class Main {

   public static void main(String []args){
      Scanner reader = new Scanner(System.in);
      System.out.print("Enter word: ");
      String text = reader.nextLine();
      System.out.println(calculateCharacters(tex));
      reader.close();
   }

  public static int calculateCharacters(String text, int tex){    
     tex = text.length();
     return tex;
  }
}

So I receive a string from String text, then I send it to the method to calculate it's length and return a number which should be intercepted by System.out.println(calculateCharacters(tex)); 因此,我从String文本中收到一个字符串,然后将其发送到该方法以计算其长度,并返回一个应由System.out.println(calculateCharacters(tex));拦截的数字System.out.println(calculateCharacters(tex)); and the probram should show me the number of letters in the string that was entered, the problem is: nothing reaches System.out.println(calculateCharacters(tex)); 并且probram应该告诉我输入的字符串中的字母数,问题是:没有东西到达System.out.println(calculateCharacters(tex)); why ? 为什么呢? where is this return tex; 这个return tex;在哪里return tex; returning it then ? 那还吗?

Well I'm not entirely sure why you've got an int tex variable, but removing it lets this work perfectly. 好吧,我不完全确定为什么int tex一个int tex变量,但是删除它可以使其正常工作。 Try rewriting your method: 尝试重写您的方法:

public static int calculateCharacters(String text) {
    int tex = text.length();
    return tex;
}

or if you're ready to be snazzy: 或者,如果您准备好时髦:

public static int calculateCharacters(String text) {
    return text.length();
}

Java is pass-by-value ( Is Java "pass-by-reference" or "pass-by-value"? ), so keeping an extra int in there that you only use to store things locally won't actually change your value of tex. Java是按值传递的( Java是“按引用传递”还是“按值传递”? ),因此在其中保留一个额外的int(仅用于本地存储内容)实际上不会改变您的值的tex。

Also, you instantiate the String text , but then pass tex to calculateCharacters() . 另外,您实例化String text ,但随后将tex传递给calculateCharacters() Since you haven't created a tex variable before this, your compiler doesn't know what to pass to calculateCharacters() . 由于在此之前尚未创建tex变量,因此编译器不知道将什么传递给calculateCharacters() Try changing that to: 尝试将其更改为:

System.out.println(calculateCharacters(text)); 

instead. 代替。

public class Main {
    public static void main(String[] args) {
        Scanner reader = new Scanner(System.in);
        System.out.print("Enter word: ");
        String text = reader.nextLine();
        System.out.println(calculateCharacters(text));
        reader.close();
    }

    public static int calculateCharacters(String text) {
        int tex = text.length();
        return tex;

    }
}

It works 有用

For counting the string use below code... 要计算字符串,请使用以下代码...

public class Main {
    static int stringCount;
public static void main(String []args){
Scanner reader = new Scanner(System.in);
System.out.print("Enter word: ");
String text = reader.nextLine();
calculateCharacters(text);
System.out.println(stringCount);
reader.close();
}
public static int calculateCharacters(String text){
    stringCount = text.length();
    return stringCount;

}
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM