作為 Codegym 大學課程一部分的導師授課片段。報名參加完整課程。


“嗨,阿米戈。今天我們將討論if/else 語句。”

“如果程序不響應不斷變化的外部環境,它們將毫無用處。程序需要知道如何適應環境並在一種情況下執行一種操作,在其他情況下執行其他操作。在 Java 中,這是通過使用'if/else 語句'——一種特殊的結構,可以在滿足條件時執行不同的代碼塊。”

“它由三部分組成:'條件','命令1 '和'命令2 '。如果條件為真,則執行'命令1 ',否則執行'命令2' 。這些命令永遠不會同時執行。該聲明或多或少是這樣的:”

if/else 語句的代碼
if (condition)
    command_1;
else
    command_2;

“多麼令人興奮!我認為這句話會讓編程變得更有趣!”

“是的。這裡有幾個例子給你:”

代碼 解釋
1個
if (a < b)
    System.out.println("A is less than B");
else
    System.out.println("B is less than  A");
如果 a 小於 b,將執行第一個命令。否則將執行第二個命令這些命令永遠不會同時執行。
2個
if (a < b)
{
    System.out.println("A is less than B");
    System.out.println("B is greater than A");
}
else
{
     System.out.println("B is less than A");
     System.out.println("A is greater than B");
}
您可以用一個代碼塊替換一個命令。其餘的都是一樣的。
3個
if (a < b)
{
    a = 0;
}
else
{
}
如果else塊為空,則可以省略它。
這三個例子是完全等價的。
如果只需要執行一個命令,可以省略大括號。如果您有多個命令,則需要保留括號。
4個
if (a < b)
{
    a = 0;
}
5個
if (a < b)
    a = 0;

“迭戈剛剛讓我交給你幾個任務。”

2
任務
Java Syntax,  等級 4課堂 4
上鎖
Good or bad?
Student robot Peter is an overachiever. Previously, his server was configured to receive scores on a five-point scale, but now his teachers have switched to a 12-point scale. But Peter doesn't know this. He's still focused on getting fives. Let's write him a compare method that compares any number with five.
4
任務
Java Syntax,  等級 4課堂 4
上鎖
Closest to 10
Ten is extremely popular and attractive number. Everyone wants to be a ten. Or at least as close to it as possible. Two numbers are standing around wondering which of them is cooler. Answer: whichever is closer to ten. Let's write these numbers a displayClosestToTen method that will determine which of them is cooler.
4
任務
Java Syntax,  等級 4課堂 4
上鎖
Come on, lucky seven!
Dice games are popular on the planet Foggy Multidimensions. The rules are different than the Earth version: Multidimensionals perceive far more dimensions than primitive three-dimensional earthlings. Their die has 4294967295 faces. Players win only if they roll a number between 50 and 100. We'll write a method that checks whether the die roll is in this range.
4
任務
Java Syntax,  等級 4課堂 4
上鎖
Seasons on Terra
An Earth year consists of four seasons, each of which lasts 3 months. While our ship was parked on this cradle of humanity, the Interplanetary Tax Service asked us to write a program to determine the season based on a number corresponding to the month of the year. We don't know why they want it. They say that it's none of our business. But they promised not to remain in our debt.
4
任務
Java Syntax,  等級 4課堂 4
上鎖
Positive and negative numbers
Diego is tall, but Alfredo is short. Rishi is experienced, but you're a "noob" programmer. Comparisons are unavoidable in life. And the same thing is true in programs. So we'll continue practicing comparisons and displaying stuff on the screen. This time let's compare the entered number with zero and manipulate it based on the result of the comparison.
4
任務
Java Syntax,  等級 4課堂 4
上鎖
Day of the week
Planet Terra still has "offices"—an outdated type of workspace. With current technology, there is no need for them, but earthlings tend to be nostalgic and haven't been in a hurry to eradicate them. Terrestrial office workers develop "TGIF syndrome": they constantly want to know what day of the week it is. Let's write a program for them!

作為 Codegym 大學課程一部分的導師授課片段。報名參加完整課程。