ตัวอย่างการบรรยายพร้อมผู้ให้คำปรึกษาซึ่งเป็นส่วนหนึ่งของหลักสูตร Codegym University ลงทะเบียนสำหรับหลักสูตรเต็ม


"สวัสดี Amigo วันนี้เราจะมาพูดถึงคำสั่ง 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,  ระดับบทเรียน
ล็อค
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,  ระดับบทเรียน
ล็อค
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,  ระดับบทเรียน
ล็อค
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,  ระดับบทเรียน
ล็อค
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,  ระดับบทเรียน
ล็อค
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,  ระดับบทเรียน
ล็อค
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 University ลงทะเบียนสำหรับหลักสูตรเต็ม