Reputation: 33
I wanted to use && with while loop in Kotlin but it's not working. First, I generated 2 random numbers, then checked if they are more than 5 and if isn't generate new numbers until they're more than 5.
fun main(args : Array<String>){
val rand = Random()
var a:Int = rand.nextInt(10)
var b:Int = rand.nextInt(10)
while (a<5 && b<5){
a = rand.nextInt(10)
b = rand.nextInt(10)
}
println("a is "+a)
println("b is "+b)
}
output is:
a is 1
b is 6
Upvotes: 0
Views: 260
Reputation: 30605
You are using incorrect logical condition. If you want both numbers to be more than 5 use condition a<5 || b<5
in the while
loop:
val rand = Random()
var a:Int = rand.nextInt(10)
var b:Int = rand.nextInt(10)
while (a < 5 || b < 5) {
a = rand.nextInt(10)
b = rand.nextInt(10)
}
Upvotes: 1