英文:
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 = {
"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
}
答案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 = {
"string1", "string2", "string3", "string4"};
public String getSentenceResult() {
for (int i = 0; i < 10; ++i) {
return this.mWords[new Random().nextInt(this.mWords.length)];
}
return "";
}
}
答案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 = {
"string1", "string2", "string3", "string4"};
public String getSentenceResult() {
String result = ""; // declare locally
for (int i = 0; i < 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)];
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论