For the drink menu, my first answer to the challenge is:
val drinkMenu = "Wine, " +"Mead, "+“LaCroix”
but this doesnt seem right because its just adding the drinks together. The obvious answer was to put the drinks into a list but I couldnt find anything online that shows me how to create array lists with strings like java
After searching, I found this: https://stackoverflow.com/questions/44239869/whats-the-kotlin-equivalent-of-javas-string
val drinks = arrayOf(“wine”, “mead”, “lacroix”)
I used arrayOf to store my drinks - however, running the code shows nothing on the log. What’s another way to list the drinks?
Storing the drinks in an Array or some collection type is the right idea here, but the declaration of the array won’t print the elements (drink names) that it holds. If you want to print the menu, then you’ll need a way to iterate through the array - perhaps a loop or the forEach function?
Here’s how I did it:
var pubDrinks: List<String> = listOf("mead", "wine", "LaCroix")
Then when I do a println(pubDrinks) it returns: [mead, wine, LaCroix]
var drinkMenu = listOf(“wine”, “mead”, “lacroix”)
drinkMenu.forEach{
println(it)
}
Output
wine
mead
lacroix
what doesit
do here? why we have used it?
fun main() {
val playerName = "Estragon"
var experiencePoints =5
var hasSteed= false
var pubName = "Unicor\'s Horn"
var publicanName = ""
var gold = 50
val drink = listOf<String>("mead","wine","LaCroix")
experiencePoints+=5
println(experiencePoints)
println(playerName)
if(!hasSteed)
println("The player does not have a steed yet.")
println(playerName.reversed())
}
const val MAX_EXPERIENCE: Int = 5000
fun main() {
val playerName = “Estragon”
var experiencePoints = 5
var hasSteed = false
val pubName = “Unicorn’s Horn”
var currentPublican: String
var goldAmount = 50
var drinksMenu = listOf(“mead”, “wine”, “LaCroix”)
playerName.reversed()
experiencePoints += 5
println(experiencePoints)
println(playerName)
}
The most correct solution is here: GitHub - AlexeyYuditsky/TheBigNerdRanchGuide
const val MAX_EXPERIENCE: Int = 5000
fun main() {
val playerName = "Estragon"
val hasSteed = false
val experiencePoints = 5
val tavernName = "Unicorn horn"
val innkeeperName = "Ivan"
val numberCoins = 50
val drinksMenu = mapOf("wine" to 30, "beer" to 25, "honey" to 15)
println("Player name: $playerName\n" +
"The presence of a horse: ${if (hasSteed) "there is" else "there is none"}\n" +
"Experience points: $experiencePoints\n" +
"Name of the tavern: $tavernName\n" +
"Innkeeper's name: $innkeeperName\n" +
"Number of coins: $numberCoins\n")
println("Drinks menu:")
for ((key, value) in drinksMenu)
println("Drink: $key, Cost: $value")
println("\nReverse name: ${playerName.reversed()}")
}