【发布时间】:2014-09-19 09:59:32
【问题描述】:
import java.util.Random;
public class DotComTestDrive {
public static void main(String [] args) {
String stringOfWords[] = {"Do", "You", "Like", "Me"};
boolean correctOrder = true;
int numberOfResets = 0;
String correctString;
String realString[];
while (correctOrder == true) {
System.out.println();
for (int x = 0 ; x < 4 ; x++) {
int rand = (int) (Math.random() * 4);
System.out.print(stringOfWords[rand]);
System.out.print(" ");
numberOfResets++;
}
// Place If statement here to change correctOrder to false when the new string is "Do You Like Me "
}
System.out.println(numberOfResets);
}
}
我的主要目标是尝试获得新的随机四个单词
它打印到 String[] 中,所以我可以使用 If 语句
查看字符串是否与原始字符串匹配。然后我会制作boolean
"correctOrder" 为false,结束循环。
如果它不是一个很好或明确的问题,我知道这很简单,很抱歉。 只是尝试学习基础知识,任何帮助,谢谢!
【问题讨论】:
-
你只想随机排列数组中的单词吗?
-
顺便说一句,为什么有人会在布尔语句中使用
boolean == true?为什么不只是while(correctOrder)? -
首先,我建议您将
correctOrder的名称更改为incorrectOrder,或者切换布尔状态。该变量名具有误导性,正确Order=false 意味着字符串的顺序正确是没有意义的。 -
看看Collections#Shuffle。这是一个静态方法,这意味着您可以从任何地方调用它:)