Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Challenge: Variables and Data Types | Kotlin Basics
Android Development with Kotlin
course content

Conteúdo do Curso

Android Development with Kotlin

Android Development with Kotlin

1. Kotlin Basics
2. Control Flow and Collections
3. Object-Oriented Programming in Kotlin

bookChallenge: Variables and Data Types

Let's move on to practice!

You already know what variables and data types exist in Kotlin, so it's time to test your knowledge with a simple task.

In this task, you need to determine the way of declaration for each variable (var or val). Then, you should assign data types to these variables. Afterward, subtract the discount amount from the item price, store this value in a variable named price, and finally, print the value of this variable to the console.

It's not as difficult as it sounds, and you can always use hints or peek at the solution for guidance!

kt

Main

copy
123456789
fun main() { val productName: ___ = "Bread" var price: ___ = 13.80F val discount: ___ = 4 // calculate the price after discount and print it to the console price = ___ print("Price after the discount is ___") }
  1. Determine the declaration type for each variable (var or val).
  2. Assign data types to these variables.
  3. Subtract the discount amount from the product price and save this value in the variable price.
  4. Print the value of the price variable to the console.
Input:
fun main() {
val productName: String = "Bread"
var price: Float = 13.80F
val discount: Int = 4

// calculate the price after discount and print it to the console
price = price - discount
print("Price after the discount is $price")
}
Output:

Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 1. Capítulo 4
some-alt