HughJanus HughJanus - 1 month ago 6
Vb.net Question

Using Or / And in Do While

I'm trying to create a solution where I push a button and two random numbers appear, that are divisible and leave no remainder. Below is the following code;

Dim E1 As Integer = CInt(Int((11 * Rnd()) + 1)) 'Random number between 1 and 10
Dim E2 As Integer = CInt(Int((11 * Rnd()) + 1)) 'Random number between 1 and 10

Do While E1 Mod 2 <> 0
E1 = CInt(Int((11 * Rnd()) + 1))
Loop

Do While E1 Mod E2 <> 0 And _
E2 <> 1 And _
E2 <> 0 And _
E1 <> E2
E2 = CInt(Int((11 * Rnd()) + 1))
Loop

lstDivVar1.Items.Add(E1)
lstDivVar2.Items.Add(E2)


The plan is to make a question that a student can answer - by being asked to divide E1 by E2. However, I don't want E2 to have (in chronological order to the Do While statement for E2);

(1) To be a number that causes a remainder

(2) To be 1

(3) To be 0

(4) To be the same number as E1


  • These are for obvious mathematical reasons to provide questions that challenge the students more.



Unfortunately, only the first logic in my Do While for E2 is being applied (E1 Mod E2 <> 0)

Any suggestions how to apply multiple conditions to a loop - it's a mickey mouse mistake I'm sure but I can't find the answer online - so please answer kindly :)

Hugh

Answer

You just had your logic wrong. This should work for you.

Do While E1 < 4 OrElse E1 Mod 2 <> 0
    E1 = CInt(Int((11 * Rnd()) + 1))
Loop

Do While E1 Mod E2 <> 0 OrElse _
         E2 = 1 OrElse _
         E2 = 0 OrElse _
         E1 = E2
    E2 = CInt(Int((11 * Rnd()) + 1))
Loop