变量必须初始化Kotlin数字

时间:2019-09-03 18:35:08

标签: kotlin initialization int

首先,我向用户询问他在学校的成绩。他选择学校科目,然后在上次考试中写下分数。我想在结束od程序中进行总结并添加注释。但是问题是“变量必须初始化”,您能帮我吗?

fun main(args: Array<String>) {

    var Math_mark: Int
    var Biology_mark: Int
    var school_subject: String

    println("Choose school subject: math or biology")
    school_subject = readLine()!!.toString().toUpperCase()
    if (school_subject == "MATH") {
           println("Choose your last exam mark: ")
        Math_mark = readLine()!!.toInt()

    }
if (school_subject == "BIOLOGY"){
    println("Choose your last exam mark: ")
    Biology_mark = readLine()!!.toInt()
}

    println("Your results:")
    if (Math_mark > 2) {
    println("Good boi")
    }
    else{
        println("Bad boi")
    }
    if (Biology_mark > 2){
        println("Good boi")
    }
    else{
        println("Bad boi")
    }

}


1 个答案:

答案 0 :(得分:1)

Kotlin不允许未初始化的变量,因此您必须使用默认值对其进行初始化:

var Math_mark: Int = 0
var Biology_mark: Int = 0
var school_subject: String = ""