Java 循环和布尔数组?
我在我的第一个Java编程类中,目前我正在尝试编写一个用户创建的方法,该方法将循环通过一个布尔数组,给我一个随机数,检查数组是否表示生成的数字为真或假,然后如果数组具有表示为真的生成的数字的索引,则生成另一个数字(TL;DR:在布尔数组中找到一个随机索引,如果随机找到的数字为真,则重新运行)。我当前的困境是,如果使用do/while循环,如果数组的所有值都为真,则循环将永远不会停止,而使用if/else只会重新运行该数字一次。如何解决此问题?Java 循环和布尔数组?,java,arrays,loops,random,Java,Arrays,Loops,Random,我在我的第一个Java编程类中,目前我正在尝试编写一个用户创建的方法,该方法将循环通过一个布尔数组,给我一个随机数,检查数组是否表示生成的数字为真或假,然后如果数组具有表示为真的生成的数字的索引,则生成另一个数字(TL;DR:在布尔数组中找到一个随机索引,如果随机找到的数字为真,则重新运行)。我当前的困境是,如果使用do/while循环,如果数组的所有值都为真,则循环将永远不会停止,而使用if/else只会重新运行该数字一次。如何解决此问题? 编辑:到目前为止我的代码: public stati
编辑:到目前为止我的代码:
public static int getNextQuestion(boolean[] queue){
int nextq = ((int)((11*Math.random())+1));
if (queue [nextq]){
int nextq = ((int)((11*Math.random())+1));
在调用
getNextQuestion()
之前,您似乎需要检查队列中的所有true。然后我将使用递归直到生成false。您可以跟踪调用递归方法的次数,以查看它是否正常工作
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;
}
结果:
基本答案
正如@Shar1er80所述,在遍历数组之前,您需要检查所有元素是否都为true
相当于
boolean b;
// Code that initiates b
if (b) {
// Code here...
}
您可以使用foreach循环遍历数组(请参阅)
因此,您可以阅读更愉快的内容(最后一种方法保持不变):
向我们展示您到目前为止所做的工作。随机数和布尔数组以何种方式相互关联?我正在尝试生成一个与数组中的索引相对应的数字,该索引的值为false。@SamTebbs33public static void main(String[]args){public static int GetNextQueue(boolean[]queue){int nextq=((int)((11*Math.random)())+1) );如果(queue[nextq]){int-nextq=((int)((11*Math.random())+1));@ericbnWell,你需要定义你的方法在元素为真的情况下应该做什么。无限循环,或者抛出异常,或者……我们无法决定你希望你的代码为你做什么。谢谢!我非常感谢^_^
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);
}
boolean b;
// Code that initiates b
if (b == true) {
// Code here...
}
boolean b;
// Code that initiates b
if (b) {
// Code here...
}
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;
}