How to use a variable produced in a method in other methods?

前端 未结 1 860
情歌与酒
情歌与酒 2021-01-22 14:30

Basically, i need to use the value of spaces from this method spacesCount in another method, or in the main void run. But it won\'t let me use outside?

相关标签:
1条回答
  • 2021-01-22 14:50

    You are providing the value of spaces as the return value from the method. So you need to store the result in a variable and use it in your other method:

    public static void main(String args[]) {
        String myString = "this is a test";
    
        int spaces = spacesCount(myString);
        // use spaces to do something
    }
    
    0 讨论(0)
提交回复
热议问题