Coretan kuliah dengan mentor sebagai sebahagian daripada kursus Universiti Codegym. Daftar untuk kursus penuh.


"Hai, Amigo. Hari ini kita akan bercakap tentang kenyataan if/else ."

"Program tidak akan berguna jika ia tidak bertindak balas terhadap perubahan keadaan luaran. Sesuatu program perlu mengetahui cara menyesuaikan diri dengan keadaan dan melaksanakan satu tindakan dalam satu kes dan tindakan lain dalam kes lain. Di Jawa, ini dicapai menggunakan 'if/else statement' – binaan khas yang memungkinkan untuk melaksanakan blok kod yang berbeza jika syarat dipenuhi."

"Ia terdiri daripada tiga bahagian: ' syarat ', ' perintah 1 ' dan ' perintah 2 '. Jika syarat itu benar, maka ' perintah 1 ' dilaksanakan, jika tidak 'perintah 2' dilaksanakan. Perintah ini tidak pernah kedua-duanya dilaksanakan. Kenyataannya kelihatan lebih kurang seperti ini:"

Kod untuk pernyataan if/else
if (condition)
    command_1;
else
    command_2;

"Betapa mengujakan! Saya rasa kenyataan itu akan menjadikan pengaturcaraan lebih menarik!"

"Ya. Berikut ialah beberapa contoh untuk anda:"

Kod Penjelasan
1
if (a < b)
    System.out.println("A is less than B");
else
    System.out.println("B is less than  A");
Jika a kurang daripada b, arahan pertama akan dilaksanakan. Jika tidak, arahan kedua akan dilaksanakan . Perintah tidak pernah kedua-duanya dilaksanakan.
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");
}
Anda boleh menggantikan satu arahan dengan blok kod. Selebihnya adalah sama.
3
if (a < b)
{
    a = 0;
}
else
{
}
Anda boleh meninggalkan blok lain jika ia kosong.
Ketiga-tiga contoh ini adalah setara sepenuhnya.
Anda boleh meninggalkan kurungan kerinting jika anda hanya perlu melaksanakan satu arahan. Jika anda mempunyai lebih daripada satu arahan, anda perlu menyimpan kurungan.
4
if (a < b)
{
    a = 0;
}
5
if (a < b)
    a = 0;

"Diego cuma minta saya beri awak beberapa tugas."

2
Tugasan
Java Syntax,  tahappelajaran
Terkunci
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
Tugasan
Java Syntax,  tahappelajaran
Terkunci
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
Tugasan
Java Syntax,  tahappelajaran
Terkunci
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
Tugasan
Java Syntax,  tahappelajaran
Terkunci
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
Tugasan
Java Syntax,  tahappelajaran
Terkunci
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
Tugasan
Java Syntax,  tahappelajaran
Terkunci
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!

Coretan kuliah dengan mentor sebagai sebahagian daripada kursus Universiti Codegym. Daftar untuk kursus penuh.