Variable "initializer is Redundant"

Asked

Viewed 122 times

0

hello good day I am starting my studies in Kotlin but when I declare the variable is accusing that the variable is redundant

package com.example.calculadoraaposentadoria

import android.app.Activity
import android.os.Bundle
import android.support.v7.app.ActionBarDrawerToggle
import android.widget.*
import kotlin.properties.Delegates
import kotlin.Int as Int1
import kotlin.Int as Int2

class MainActivity: Activity(){
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val spn_sexo= findViewById<Spinner>(R.id.spn_sexo)
        val txt_idade=findViewById<EditText>(R.id.txt_idade)
        val btn_button=findViewById<Button>(R.id.btn_enviar)
        val txt_resultado=findViewById<TextView>(R.id.txt_resultado)


        spn_sexo.adapter= ArrayAdapter<String>(this,android.R.layout.simple_spinner_dropdown_item, listOf("masculino","feminino"))

        btn_button.setOnClickListener {
            val sexo=spn_sexo.selectedItem as String
            val idade=txt_idade.text.toString().toInt()

            var resultado=0
            if (sexo=="masculino"){
                resultado=65-idade
            }else{
                resultado=60-idade
            }
            txt_resultado.text="Faltam $resultado anos para você se aposentar."

        }
    }
}

1 answer

1

It is not the variable that is redundant.

What’s redundant is starting it with zero.

When declaring a variable of a numeric type it is automatically initialized with zero.

Instead of

var resultado=0

use only

var resultado

Browser other questions tagged

You are not signed in. Login or sign up in order to post.