i want to write a little program, but don´t know why my loop isn´t working correctly.
while ((!ss.legal(myNumbers, colRand, rowRand, numRand)) || (triedNumbers.size() != 9)) {
System.out.println(triedNumbers.size());
// if(triedNumbers.size()==9) break;
numRand = getRandomNumber(1,9);
if (!triedNumbers.contains(Integer.valueOf(numRand))) {
triedNumbers.add(Integer.valueOf(numRand));
}
}
The thing is the second condition. My syso-command tells me the size is already 9 but the loop doesn´t end?
If the random number is fine the legal method returns true
. So the loop should end. If the size of the map gets 9, it should also end the loop because I am only checking the numbers from 1 to 9.
Replace ||
(or) with &&
(and). As it is even if the size is already 9 the loop will still continue as long as the first condition is true
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.