【Use Kotlin to write your first program】

1. Preparation

  • computer with internet access
  • A newer version of a web browser (such as the latest version of Chrome) installed on the computer

Important things you will learn in this section

How to create, change, understand and run the simplest Kotlin program for displaying messages.

2. Run your first program written in Kotlin

If you use a compiler to run, it may involve the installation of related compilers, the configuration of environment variables, etc., which is not very friendly to friends who are new to programming, so this summary recommends that you run https in browser-based programming tools: //developer.android.com/training/kotlinplayground
After opening the browser, you will normally see the following interface
Show results
Click the green triangle icon on the right of the compiler insert image description hereto run the program
At this time, the running result will be displayed below the compiler

insert image description here

3. Modify your program

Change the Hello World code

you can put

Change "Hello World" to the text you want, such as "Hello Lengzuiquan!"
Click the green triangle to run the program
and you will see your running results

insert image description here
After you click the "Run button", the system will send the code you wrote to a third-party server controlled by JetBrains for compilation.

What is the working mechanism

It seems that if we want to output a sentence, we need to use two lines of code. Some students may find it more complicated.

If you ask your friend to write "Hello World!" on a piece of paper, there may be a lot of implicit information in it, for example, they will assume that they need to use a pen, and you want them to use the alphabet to write! The computer doesn't make these assumptions, so you have to give precise instructions covering each step.

Just like English has structures, so do programming languages. If you've ever learned another language, you know the challenges of learning grammar, spelling, and perhaps a new alphabet of symbols and vocabulary. Learning to program has similar challenges, but fortunately, it's much simpler and more logical than learning (eg, English).

Understand the components of the program

Now, take a look at the code. Each part of the program has a specific purpose, and you need all of them to run the program. Let's start with the first word.

fun
  • funRepresents a word in the Kotlin programming language. funfunction. A function is a part of a program that performs a specific task.

Note: Kotlin has many special words with very specific meanings. When you learn to program in the Kotlin language, you will learn these words. They are often called keywords or reserved words.

fun main
  • mainis the name of the function. Functions have names so they can be distinguished from each other. Call this function mainbecause it is the first or main function called when running the program. Kotlin programs are required to have a mainfunction name.
fun main()
  • A function name is always followed by ()two parentheses.
  • Inside the brackets, you can put information about the function you want to use. The inputs to the function are called "arguments" or argssimply for short. You'll learn more about parameters later.
fun main() {
    
    }
  • Note ()the pair of curly braces after the parentheses {}. Inside the function is the code that does the job. These curly braces surround these lines of code. Look at the lines of code between
    the curly braces :{}
println("Hello 冷醉泉!")

This line of code prints out Hello 冷醉泉!the text.

  • println tells the system to print a line of text.
  • Put the text you want to print in brackets
  • Note that the text to be printed must be placed in English double quotes, which tells the system that the content in the quotes must be printed exactly as given (in layman’s terms, it prints whatever is in the quotes)

To actually print the text, this entire printlncommand line must be placed maininside the function

As shown below, this is a minimal Kotlin program.

fun main() {
    
    
    println("Hello 冷醉泉!")
}

4. Extend your program

print multiple messages

marvelous! You have used println()the function to output a line of text. However, you can add any line directives inside the function as needed or as required by the task.

  1. Copy the line println("Hello 冷醉泉!")of code and paste it below twice. Make sure that the pasted line of code is within curly braces of the main function.
  2. Change one of the texts to be output to a person's name, such as "Li Hua".
  3. Change the other text to output to read "You are 18!".
    Your code should look like the code below.
fun main() {
    
    
    println("Hello 冷醉泉!")
    println("李华")
    println("You are 18!")
}

What do you think would be the result of running this code?

  1. Run your program and see what results it produces.
  2. Go to the output pane and you should see 3 lines of output in the console window as shown below.
Hello 冷醉泉!
李华
You are 18!

Great!

Handle errors

It's normal to make mistakes while programming, and most tools provide feedback to help you correct mistakes. In this step, make a bug and see what happens.

  1. In your program, remove the quotation marks 李华around so that the line of code looks like this.
println(李华)
  1. Run the program. You should see the to be output 李华displayed in red.
    insert image description here
  2. Look at the output pane. A message is displayed in the pane explaining the error in the code.
  3. The message Unresolved reference: 李华tells you what the system thinks is wrong with your code. Even if you don't understand the error message, you may be able to figure out what's wrong. In this example, you know that println()the directive is used to output text. As you learned earlier, text must be enclosed in quotation marks. An error occurs if the text is not quoted.
  4. Now add the quotes.
  5. Run the program to make sure it runs normally again.

Congratulations, you've run and changed your first Kotlin program!

5. Solution code

Below is the complete code for the program you write in this codelab.

fun main() {
    
    
    println("Hello 冷醉泉!")
    println("李华")
    println("You are 18!")
}

6. Summary

  1. https://developer.android.com/training/kotlinplayground is a web-based interactive code editor where you can practice writing Kotlin programs.
  2. All Kotlin programs need to have a main()function :fun main() {}
  3. println()function to output a line of text.
  4. Enclose the text you want to output in double quotes, eg "Hello".
  5. The repeat println()command can output multiple lines of text.
  6. Errors in the program are marked in red. Error messages appear in the output pane to help you determine where and why the error occurred.

Seven, self-practice

Please do the following exercises:

  1. Change the println() instruction to print().
  2. Run the program. What will happen?

Tip: print() The directive will only output text and will not add a newline at the end of each string.

  1. Please correct the text so that each part of the message is on its own line.

Tip: Use \nto , eg "line \n break". Adding newlines changes the output as shown below.

Tip: You can output blank lines by providing no text: println("").

code:

fun main() {
    
    
    println("no line break")
    println("")
    println("with line \n break")
}

output:

no line break

with line 
 break

Check your program:
the following is a possible solution:

fun main() {
    
    
    print("Happy 冷醉泉!\n")
    print("李华\n")
    print("You are 18!")
}

Next: [Create birthday greetings in Kotlin]

The content of this series of articles is all from the official website of Android Studio

Guess you like

Origin blog.csdn.net/Jasonhso/article/details/125842715