Meta

Comments

Popular

Text/Html

Data Protection Terms

Link

Some Preview

Android Developers menu Your first program in Kotlin Sign in access_time13 mins remaining Before you begin Get started Open Kotlin Playground Run your first program Parts of a function Modify your program Kotlin style guide Fix errors in your code Exercises Solutions Conclusion 10. Solutions The output of the program is: 1 2 3 The code in your program should look like: fun main() { println("I'm") println("learning") println("Kotlin!") } This is the correct code for the program: fun main() { println("Monday") println("Tuesday") println("Wednesday") println("Thursday") println("Friday") } The closing curly brace that indicates the end of the function body for the main function is missing on the third line of the program. Correct code: fun main() { println("Tomorrow is rainy") } Output: Tomorrow is rainy When you run the program, you see an Unresolved reference: printLine error. This is because printLine() isn't a recognized function in Kotlin. You can also see the part of the code causing the error highlighted in red in the Kotlin Playground. Change the function name to println to print a line of text to the output, which fixes the error. Correct code: fun main() { println("There is a chance of snow") } Output: There is a chance of snow When you run the program, you see an Unresolved reference: println error. This message doesn't directly say how to fix the problem. This can sometimes happen when you troubleshoot an error, and it requires you to take a deeper look at the code to resolve the unexpected behavior. Upon closer look, the second println() function call in the code is red, which signals that's where the problem is. Kotlin expects just one statement on each line. In this case, you can move the second and third println() function calls onto separate new lines to solve the issue. Correct code: fun main() { println("Cloudy") println("Partly Cloudy") println("Windy") } Output: Cloudy Partly Cloudy Windy If you run the program, you see the error: Function 'main' must have a body. A function body should be enclosed within an opening curly brace and a closing curly brace { }, not opening and closing parentheses ( ). Correct code: fun main() { println("How's the weather today?") } Output: How's the weather today? Back English Next bug_report Report a mistake Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates. The new page has loaded.

Website Training