0

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?

1
  • 1
    Your for x loop only contains one statement: the for y loop. Use {} if you want multiple statements within the loop (it a good idea to use braces even for single-statement loops to make it clear where it begins and ends). Commented Oct 27, 2017 at 18:39

1 Answer 1

3

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)
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

@GLHF you asked how to solve the error, I can't know what's the purpose of your code

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.