变量必须初始化Kotlin数字 [英] Variable must be initialized Kotlin numbers

查看:107
本文介绍了变量必须初始化Kotlin数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

At first i ask user about his marks in school. He choose school subject and then write a mark from last exam. I want summarize in the end od program and add commentary. But the problem is "Variable must be initialized" can you help me?

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")
    }

}


推荐答案

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

Kotlin doesn't allow uninitialized variables, so you have to initialize them with default values:

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

这篇关于变量必须初始化Kotlin数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆