循环和布尔数组?
Looping and a boolean array?
我第一次 Java 编程 class,我目前正在尝试编写一个用户自定义的方法,它将循环遍历布尔数组,给我一个随机数,检查数组表示生成的数字是真还是假,然后如果数组具有生成的数字的索引为真,则生成不同的数字(TL; DR:在布尔数组中查找随机索引,如果随机找到的数字为真)。我目前的困境是,如果我使用 do/while 循环,如果数组的所有值都为真,循环将永远不会停止,而使用 if/else 只会重新运行该数字一次。我该如何解决这个问题?
编辑:到目前为止我的代码:
public static int getNextQuestion(boolean[] queue){
int nextq = ((int)((11*Math.random())+1));
if (queue [nextq]){
int nextq = ((int)((11*Math.random())+1));
看来您需要在调用您的 getNextQuestion()
之前检查队列中的所有内容。然后我会只使用递归,直到你生成一个错误。您可以跟踪调用递归方法的次数以查看它是否正常工作...
static Random rand = new Random();
static int recursiveCount = 0;
public static void main(String[] args) throws Exception {
boolean[] queue = new boolean[] { true, false, true, true, false, true };
if (checkIfAllTrue(queue) == false) {
getNextQuestion(queue);
}
System.out.println("Recursive Count: " + recursiveCount);
System.out.println("Done!");
}
public static void getNextQuestion(boolean[] queue) {
recursiveCount++;
int nextQ = rand.nextInt(queue.length);
if (queue[nextQ]) {
getNextQuestion(queue);
}
}
public static boolean checkIfAllTrue(boolean[] queue) {
// Check your queue for all trues before proceeding
boolean allTrue = true;
for (int i = 0; i < queue.length; i++) {
if (queue[i] == false) {
allTrue = false;
break;
}
}
return allTrue;
}
结果:
更新
在看到关于返回随机错误索引索引的评论后,我更新了main()
和getNextQuestion()
。其他一切都保持不变。
public static void main(String[] args) throws Exception {
boolean[] queue = new boolean[]{true, false, true, true, false, true};
if (checkIfAllTrue(queue) == false) {
System.out.println("False index: " + getNextQuestion(queue));
}
System.out.println("Recursive Count: " + recursiveCount);
System.out.println("Done!");
}
public static int getNextQuestion(boolean[] queue) {
recursiveCount++;
int nextQ = rand.nextInt(queue.length);
if (queue[nextQ]) {
return getNextQuestion(queue);
} else {
return nextQ;
}
}
结果:
基本答案
如@Shar1er80 所述,您需要在遍历数组之前检查所有元素是否为真。这是一个使用循环(迭代,非递归)和 Math.random()
的解决方案
public static void main(String[] args) {
boolean[] array = new boolean[] { true, false, true, true, false, true };
int iterationNumber = 0;
int nextItem;
if (checkIfAllTrue(array) == false) {
boolean retry;
do {
nextItem = generateRandomInt(array.length);
retry = array[nextItem];
iterationNumber++;
} while (retry == true);
System.out.println("The false is found at index " + nextItem);
}
System.out.println("iterationNumber = " + iterationNumber);
}
/**
* Checks if all the elements of the given boolean array are true.
* @param queue The boolean array to check.
* @return True if all elements are true, false if at least an element is false.
*/
public static boolean checkIfAllTrue(boolean[] queue) {
// Check your queue for all trues before proceeding
boolean allTrue = true;
for (int i = 0; i < queue.length; i++) {
if (queue[i] == false) {
allTrue = false;
break;
}
}
return allTrue;
}
/**
* Generates an int between 0 included and range excluded.
* @param range The maximum value of the range (excluded from generation).
* @return An integer between 0 included and range excluded.
*/
public static int generateRandomInt(int range) {
return (int) (Math.random() * range);
}
代码改进
鉴于这是您的第一个 Java 课程,上面的代码很冗长。可以简化前两种方法,以考虑到
boolean b;
// Code that initiates b
if (b == true) {
// Code here...
}
相当于
boolean b;
// Code that initiates b
if (b) {
// Code here...
}
并且您可以使用 foreach 循环遍历数组(参见 http://docs.oracle.com/javase/8/docs/technotes/guides/language/foreach.html)。
所以你可以有更愉快的阅读(最后的方法不变):
public static void main(String[] args) {
boolean[] array = new boolean[] { true, false, true, true, false, true };
int iterationNumber = 0;
int nextItem;
if (!checkIfAllTrue(array)) {
boolean retry;
do {
nextItem = generateRandomInt(array.length);
retry = array[nextItem];
iterationNumber++;
System.out.println("nextItem = " + nextItem);
System.out.println("retry = " + retry);
System.out.println("iterationNumber = " + iterationNumber);
} while (retry);
System.out.println("The false is found at index " + nextItem);
}
System.out.println("iterationNumber = " + iterationNumber);
}
public static boolean checkIfAllTrue(boolean[] queue) {
// Check your queue for all trues before proceeding
boolean allTrue = true;
for (boolean element : queue) {
if (!element) {
allTrue = false;
break;
}
}
return allTrue;
}
我第一次 Java 编程 class,我目前正在尝试编写一个用户自定义的方法,它将循环遍历布尔数组,给我一个随机数,检查数组表示生成的数字是真还是假,然后如果数组具有生成的数字的索引为真,则生成不同的数字(TL; DR:在布尔数组中查找随机索引,如果随机找到的数字为真)。我目前的困境是,如果我使用 do/while 循环,如果数组的所有值都为真,循环将永远不会停止,而使用 if/else 只会重新运行该数字一次。我该如何解决这个问题?
编辑:到目前为止我的代码:
public static int getNextQuestion(boolean[] queue){
int nextq = ((int)((11*Math.random())+1));
if (queue [nextq]){
int nextq = ((int)((11*Math.random())+1));
看来您需要在调用您的 getNextQuestion()
之前检查队列中的所有内容。然后我会只使用递归,直到你生成一个错误。您可以跟踪调用递归方法的次数以查看它是否正常工作...
static Random rand = new Random();
static int recursiveCount = 0;
public static void main(String[] args) throws Exception {
boolean[] queue = new boolean[] { true, false, true, true, false, true };
if (checkIfAllTrue(queue) == false) {
getNextQuestion(queue);
}
System.out.println("Recursive Count: " + recursiveCount);
System.out.println("Done!");
}
public static void getNextQuestion(boolean[] queue) {
recursiveCount++;
int nextQ = rand.nextInt(queue.length);
if (queue[nextQ]) {
getNextQuestion(queue);
}
}
public static boolean checkIfAllTrue(boolean[] queue) {
// Check your queue for all trues before proceeding
boolean allTrue = true;
for (int i = 0; i < queue.length; i++) {
if (queue[i] == false) {
allTrue = false;
break;
}
}
return allTrue;
}
结果:
更新
在看到关于返回随机错误索引索引的评论后,我更新了main()
和getNextQuestion()
。其他一切都保持不变。
public static void main(String[] args) throws Exception {
boolean[] queue = new boolean[]{true, false, true, true, false, true};
if (checkIfAllTrue(queue) == false) {
System.out.println("False index: " + getNextQuestion(queue));
}
System.out.println("Recursive Count: " + recursiveCount);
System.out.println("Done!");
}
public static int getNextQuestion(boolean[] queue) {
recursiveCount++;
int nextQ = rand.nextInt(queue.length);
if (queue[nextQ]) {
return getNextQuestion(queue);
} else {
return nextQ;
}
}
结果:
基本答案
如@Shar1er80 所述,您需要在遍历数组之前检查所有元素是否为真。这是一个使用循环(迭代,非递归)和 Math.random()
public static void main(String[] args) {
boolean[] array = new boolean[] { true, false, true, true, false, true };
int iterationNumber = 0;
int nextItem;
if (checkIfAllTrue(array) == false) {
boolean retry;
do {
nextItem = generateRandomInt(array.length);
retry = array[nextItem];
iterationNumber++;
} while (retry == true);
System.out.println("The false is found at index " + nextItem);
}
System.out.println("iterationNumber = " + iterationNumber);
}
/**
* Checks if all the elements of the given boolean array are true.
* @param queue The boolean array to check.
* @return True if all elements are true, false if at least an element is false.
*/
public static boolean checkIfAllTrue(boolean[] queue) {
// Check your queue for all trues before proceeding
boolean allTrue = true;
for (int i = 0; i < queue.length; i++) {
if (queue[i] == false) {
allTrue = false;
break;
}
}
return allTrue;
}
/**
* Generates an int between 0 included and range excluded.
* @param range The maximum value of the range (excluded from generation).
* @return An integer between 0 included and range excluded.
*/
public static int generateRandomInt(int range) {
return (int) (Math.random() * range);
}
代码改进
鉴于这是您的第一个 Java 课程,上面的代码很冗长。可以简化前两种方法,以考虑到
boolean b;
// Code that initiates b
if (b == true) {
// Code here...
}
相当于
boolean b;
// Code that initiates b
if (b) {
// Code here...
}
并且您可以使用 foreach 循环遍历数组(参见 http://docs.oracle.com/javase/8/docs/technotes/guides/language/foreach.html)。
所以你可以有更愉快的阅读(最后的方法不变):
public static void main(String[] args) {
boolean[] array = new boolean[] { true, false, true, true, false, true };
int iterationNumber = 0;
int nextItem;
if (!checkIfAllTrue(array)) {
boolean retry;
do {
nextItem = generateRandomInt(array.length);
retry = array[nextItem];
iterationNumber++;
System.out.println("nextItem = " + nextItem);
System.out.println("retry = " + retry);
System.out.println("iterationNumber = " + iterationNumber);
} while (retry);
System.out.println("The false is found at index " + nextItem);
}
System.out.println("iterationNumber = " + iterationNumber);
}
public static boolean checkIfAllTrue(boolean[] queue) {
// Check your queue for all trues before proceeding
boolean allTrue = true;
for (boolean element : queue) {
if (!element) {
allTrue = false;
break;
}
}
return allTrue;
}