GLHF
GLHF

Reputation: 4035

Kotlin unresolved reference issue

I was able to do everything so far except this, don't know why but I got this error Unresolved reference:x for last line print(x).

fun main(args:Array<String>) {

    var liste = IntRange(3,19)

    var bolundu = 1

    for (x in liste)
        for (y in IntRange(2,x))
            if (x % y != 0)
                bolundu = 0

        if (bolundu == 1)
            print(x)
}

I don't understand what is the problem, why it doesn't match that x with the one in the for loop?

Upvotes: 0

Views: 1326

Answers (1)

Giorgio Antonioli
Giorgio Antonioli

Reputation: 16214

It happens because you must specify the parenthesis in Kotlin if you have more than one statement to evaluate within the loop.

Actually, your code is exactly the same of this one:

fun main(args:Array<String>) {
    var liste = IntRange(3,19)

    var bolundu = 1

    for (x in liste) {
        for (y in IntRange(2,x)) {
            if (x % y != 0) {
                bolundu = 0
            }
        }
    }
    if (bolundu == 1) {
        print(x)
    }
}

Instead, you want something like this:

fun main(args:Array<String>) {
    var liste = IntRange(3,19)

    var bolundu = 1

    for (x in liste) {
        for (y in IntRange(2,x)) {
            if (x % y != 0)
                bolundu = 0
        }
        if (bolundu == 1)
            print(x)
    }
}

Upvotes: 3

Related Questions