简体   繁体   中英

do while statement to check two conditions

I have written a Java code as below, which generates random numbers and sums them to give correct answer. However, I am also trying to generate to two false answers that are not equal to each other or the correct answer using do while statement. However, it is not working. The last do part of the java code is not being run. It is possible to have two random number ans_wrong1 and ans_wrong2 which are the same.

In simple english, if ans_wrong2 equals ans_wrong1 OR ans_correct, generate another random number. Continue this process until number is generated which is not equal to any of the other two numbers. Can someone spot what I am doing wrong and offer suggestion?

Lets try to simplify the question further, because, the suggested solutions are not working.

I want t generate two random numbers that do not equal to each other or the correct answer.

Code updated - still not working

Thanks

int ans_correct = 8;

int ans_wrong1;
    do {
        ans_wrong1 = randomGenerator.nextInt(20);
    } while(ans_wrong1 == ans_correct);

    int ans_wrong2;
    do {
        ans_wrong2 = randomGenerator.nextInt(10);
    } while ( ans_wrong2 == ans_correct || ans_wrong2 == ans_wrong1 );

Your problem is that first you generate a number that is not equal to ans_correct , and then you throw it away, and generate a new one which is not equal to ans_wrong1 , without even looking at ans_correct .

To do what you want, you need to generate a number, and then test it for both conditions at the same time:

do {
    ans_wrong2 = randomGenerator.nextInt(10);
} while ( ans_wrong2 == ans_correct || ans_wrong2 == ans_wrong1 );

Your code has a couple of bugs... firstly, what you already know:

Value Overwrite

Your code blocks may unintentionally undo each other:

 int ans_wrong2; do { ans_wrong2 = randomGenerator.nextInt(10); } while(ans_wrong2 == ans_correct); do { ans_wrong2 = randomGenerator.nextInt(10); } while(ans_wrong2 == ans_wrong1); 

The second while loop will replace the ans_wrong value always, and it may replace it with the value that was excluded in the first loop (ie the second loop may produce ans_correct .

The simple solution to this would be:

int ans_wrong2;
do {
    ans_wrong2 = randomGenerator.nextInt(10);

} while(ans_wrong2 == ans_correct || ans_wrong2 == ans_wrong1);

Impossible Answers

The second bug is that your ans_correct can never be the value 9. This is because randomGenerator.nextInt(5) will always return a value from 0 to 4.

Adding two of them together, will produce a value from 0 to 8.

randomGenerator.nextInt(10), however, will possibly produce the value 9.... which would never be an option.

Code Example:

private static final Random randomGenerator = new Random();

public static final void testRand() {
    int ans_correct = randomGenerator.nextInt(5) + randomGenerator.nextInt(5);

    int ans_wrong1;
    do {
        ans_wrong1 = randomGenerator.nextInt(10);
    } while(ans_wrong1 == ans_correct);

    int ans_wrong2;
    do {
        ans_wrong2 = randomGenerator.nextInt(10);

    } while(ans_wrong2 == ans_correct);

    do {
        ans_wrong2 = randomGenerator.nextInt(10);
    } while(ans_wrong2 == ans_wrong1);

    System.out.printf("Correct %d  WA1 %d WA2 %d\n", ans_correct, ans_wrong1, ans_wrong2);
}

public static void main(String[] args) {
    for (int i = 0; i < 20; i++) {
        testRand();
    }
}

working at Ideone

This should work fine.In your code the 2nd do while loop is having no effect as the value of ans_wrong2 is anyhow getting replaced in the 3rd loop and in the last loop you are only checking if it's equal to ans_wrong1. Thus there's still a chance that it may be equal to ans_correct.

Random randomGenerator = new Random();

int random1 = randomGenerator.nextInt(5);
int random2 = randomGenerator.nextInt(5);

int ans_correct = random1 + random2;

int ans_wrong1;
do {
    ans_wrong1 = randomGenerator.nextInt(10);
} while(ans_wrong1 == ans_correct);

int ans_wrong2;
do {
    ans_wrong2 = randomGenerator.nextInt(10);

} while(ans_wrong2 == ans_correct || ans_wrong2 == ans_wrong1);

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM