英文:
While loop doesn't stop even after condition is false
问题
在这段代码中,for循环选择了一个从1到4的随机值(numAleatorio
),根据这个值向密码中添加特定的字符。你创建了一个while循环来检查随机值是否在数组中,如果在数组中就会生成一个新的值给numAleatorio
,但即使条件不再满足也会继续执行while循环,密码就无法生成。这是因为你没有更新数组naoColocar
中的相应索引位置,导致while循环一直处于条件为真的状态。
为了解决这个问题,你可以在while循环内添加更新数组的逻辑。在成功生成一个随机值后,你应该将相应的索引位置的元素设置为已使用,以避免再次生成相同的值。
以下是更新后的代码片段:
for (int x = 0; x < tamanhoDaSenha; x++) {
int numAleatorio = random.nextInt(4) + 1;
while (naoColocar[numAleatorio - 1] == 1) {
numAleatorio = random.nextInt(4) + 1;
}
switch (numAleatorio) {
case 1:
senha += letrasMin[random.nextInt(26)];
break;
case 2:
senha += letrasMai[random.nextInt(26)];
break;
case 3:
senha += numeros[random.nextInt(10)];
break;
case 4:
senha += charEspeciais[random.nextInt(11)];
break;
}
naoColocar[numAleatorio - 1] = 1; // 标记已经使用过的类型
}
这样修改后,当成功生成一个字符并将其添加到密码中后,相应的数组索引位置会被标记为1,以确保不会再次生成相同类型的字符。这应该解决了密码无法生成的问题。
英文:
I am trying to make a code that generates a password for the user, and I wanted the user to be able to select which types of characters it would like in its password.
When the user decides to not have a certain character, it adds a certain number in an array called naoColocar
. By default, the array is 0, 0, 0, 0, but if you want to not add lowercase letters it adds 1 to index 0, uppercase letters adds 2 to index 1, numbers add 3 to index 2, special characters add 4 to index 3.
for (int x = 0; x < tamanhoDaSenha; x++) {
int numAleatorio = random.nextInt(4);
numAleatorio++;
while (naoColocar[0] == numAleatorio || naoColocar[1] == numAleatorio || naoColocar[2] == numAleatorio || naoColocar[3] == numAleatorio) {
numAleatorio = random.nextInt(4);
numAleatorio++;
}
switch (numAleatorio) {
case 1:
senha += letrasMin[random.nextInt(26)];
break;
case 2:
senha += letrasMai[random.nextInt(26)];
break;
case 3:
senha += numeros[random.nextInt(10)];
break;
case 4:
senha += charEspeciais[random.nextInt(11)];
break;
}
}
In the code, the for loop chooses a random value from 1 to 4 (numAleatorio
) and depending on the value, adds a certain character to the password. I made a while loop that checks if the random value is in the array, it would generate a new value to numAleatorio
, but even when the condition is false, the while loop continues and the password is never generated, why?
Edit: Changed so that now the code generates the correct amount of characters and 9, z and Z are now possible, thanks for pointing it out.
答案1
得分: 1
请注意,以下是已翻译的部分:
"You should rely on tests to make sure that the method you wrote works as required, so in this case I wrote some tests based on code you provided.
Please note that this is only example and you should not place model logic in test code and you should test method behaviour.
These tests are passing, so that indicates that there is an error in your method logic when dealing with dataset {1,2,3,4}
In that case I suggest you to run your code with debugger and see why stop condition does not occur in your while loop."
英文:
You should rely on tests to make sure that the method you wrote works as required, so in this case I wrote some tests based on code you provided.
Please note that this is only example and you should not place model logic in test code and you should test method behaviour.
@ParameterizedTest
@CsvSource(value = {
"0,0,0,0", "1,0,0,0", "0,2,0,0", "0,0,3,0", "0,0,0,4", "1,2,0,0", "1,0,3,0", "1,0,0,4",
"0,2,3,0", "0,2,0,4", "0,0,3,4", "1,2,3,0", "1,2,0,4", "0,2,3,4", "1,0,3,4"}, delimiter = ',')
void shouldNaoColocarDoesNotHaveInfiniteWhileLoop(ArgumentsAccessor aa) {
int[] naoColocar = {aa.getInteger(0), aa.getInteger(1), aa.getInteger(2), aa.getInteger(3)};
whileNaoColocar(naoColocar);
}
@ParameterizedTest
@CsvSource(value = {"1,2,3,4"}, delimiter = ',')
void shouldNaoColocarHaveInfiniteWhileLoop(ArgumentsAccessor aa) {
int[] naoColocar = {aa.getInteger(0), aa.getInteger(1), aa.getInteger(2), aa.getInteger(3)};
assertThrows(RuntimeException.class, () -> whileNaoColocar(naoColocar));
}
void whileNaoColocar(int[] naoColocar) {
int counter = 0;
int numAleatorio = random.nextInt(4);
numAleatorio++;
while (naoColocar[0] == numAleatorio || naoColocar[1] == numAleatorio ||
naoColocar[2] == numAleatorio || naoColocar[3] == numAleatorio) {
numAleatorio = random.nextInt(4);
numAleatorio++;
counter++;
if (counter >= 100) throw new RuntimeException();
}
}
These tests are passing, so that indicates that there is an error in your method logic when dealing with dataset {1,2,3,4}
In that case I suggest you to run your code with debugger and see why stop condition does not occur in your while loop.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论