Practice variables, arithmetic, and string concatenation by printing a tiny one-item receipt.
You start with:
val item = "Coffee Mug"
val price = 12
val quantity = 3
Build:
Coffee Mug x3 Total: $36
Rules:
- Compute
total = price * quantity. - First line:
"<item> x<quantity>"(e.g."Coffee Mug x3"). - Second line:
"Total: $<total>". - Join the two lines with a newline
"\n"into one stringresult, then print it withprintln(result).
Kotlin's
+ on a String and an Int converts the integer to text for you.
line1 + "\n" + line2 joins both lines into one string.
val item = "Coffee Mug"
val price = 12
val quantity = 3
val total = price * quantity
val line1 = item + " x" + quantity
val line2 = "Total: $" + total
val result = line1 + "\n" + line2
println(result)
Free plan: 30 tutor messages. Premium is unlimited.
Saved
Ctrl + Enter to check
