为什么Android Studio坚持让我把这个设置为void?

huangapple go评论115阅读模式
英文:

Why does Android Studio insist that I make this void?

问题

import java.util.Random;

public class RandomSentence {
    public String[] mWords = {
        "string1", "string2", "string3", "string4"};

    public String getSentenceResult() {
        for (int i = 0; i < 10; ++i) {
            return this.mWords[new Random().nextInt(this.mWords.length)];
        }
    } // error appears here 
}
英文:

I have a simple class here that consists of an array of words and what I was hoping to be the start of a method that grabs random words from that array. I do not understand Java as much, so I cannot understand why Android Studio wants me to return null on this.

import java.util.Random;

public class RandomSentence {
    public String[] mWords = {
        &quot;string1&quot;, &quot;string2&quot;, &quot;string3&quot;, &quot;string4&quot;};

    public String getSentenceResult() {
        for (int i = 0; i &lt; 10; ++i) {
            return this.mWords[new Random().nextInt(this.mWords.length)];
        }
    } // error appears here 
}

答案1

得分: 0

你在 for 循环后面没有添加返回操作符因此出现错误
import java.util.Random;

public class RandomSentence {
    public String[] mWords = {
        "string1", "string2", "string3", "string4"};

    public String getSentenceResult() {
        for (int i = 0; i < 10; ++i) {
            return this.mWords[new Random().nextInt(this.mWords.length)];
        }
        return "";
    }
}
英文:

You don't have a return operator after the for loop, thus it's erroring out

import java.util.Random;

public class RandomSentence {
    public String[] mWords = {
        &quot;string1&quot;, &quot;string2&quot;, &quot;string3&quot;, &quot;string4&quot;};

    public String getSentenceResult() {
        for (int i = 0; i &lt; 10; ++i) {
            return this.mWords[new Random().nextInt(this.mWords.length)];
        }
        return &quot;&quot;;
    }
}

答案2

得分: 0

这只是Android Studio,而是基本的Java原则。您需要在本地相对于返回类型声明数据类型,然后将变量分配给您的循环结果。现在,您可以在方法下方返回您的本地变量。

import java.util.Random;

public class RandomSentence {
    public String[] mWords = {
        "string1", "string2", "string3", "string4"};
    
    public String getSentenceResult() {
        String result = ""; // 在本地声明
        for (int i = 0; i < 10; ++i) {
            // 分配给您的返回类型
            result = this.mWords[new Random().nextInt(this.mWords.length)];
        }
        // 返回
        return result;
    }  
}
英文:

It is not just Android studio, it is basic java principle. You need to declare the datatype relative to your return type locally then assigned the variable to your looped results. Now you can return your local variable below your method.

import java.util.Random;

    public class RandomSentence {
        public String[] mWords = {
            &quot;string1&quot;, &quot;string2&quot;, &quot;string3&quot;, &quot;string4&quot;};
    
        public String getSentenceResult() {
        	String result = &quot;&quot;; // declare locally
            for (int i = 0; i &lt; 10; ++i) {
//assign to you return type
            	 result= this.mWords[new Random().nextInt(this.mWords.length)];
            }
//return
            return result;
        }  

答案3

得分: 0

我会建议您两种方法来实现您想要的功能,

public String getSentenceResult() {
    return this.mWords[new Random().nextInt(this.mWords.length)];
}

或者

public String getSentenceResult(String[] words) {
    return words[new Random().nextInt(words.length)];
}
英文:

I would suggest you 2 logic to achieve what you wanted,

public String getSentenceResult() {
    return this.mWords[new Random().nextInt(this.mWords.length)];
}

or

public String getSentenceResult(String[] words) {
    return words[new Random().nextInt(words.length)];
 }

huangapple
  • 本文由 发表于 2020年10月7日 07:32:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/64235163.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定