ในความเป็นจริงทั่วไปแล้ว
เมื่อมีสิ่งเร้าอะไรสักอย่าง จึงทำให้เราต้องเลือกเดินไปทางใดทางหนึ่งเช่น
คะแนนของคณะบัญชีสูงมากๆ (สมมุติว่า 80 ) เป็นเหตุผลหนึ่งที่ทำให้เราไม่เลือกอาชีนี้ก็ได้
ในตรงกันข้ามถ้าคณะเศรษฐศาสตร์คะแนนแค่ 50 ก็มีหลายต่อหลายคนเลือกอาชีพนี้พอสมควรเลย
เครื่องหมายในภาษา Java
เครื่องหมายที่ใช้กับประโยคเงื่อนไขใน Java ทั้งหมด มี
< น้อยกว่า
> มากกว่า
== เท่ากับ
<= น้อยกว่าหรือเท่ากับ
>= มากกว่าหรือเท่ากับ
!= ไม่เท่ากับ
เครื่องหมายที่ใช้กับประโยคเงื่อนไขใน Java ทั้งหมด มี
< น้อยกว่า
> มากกว่า
== เท่ากับ
<= น้อยกว่าหรือเท่ากับ
>= มากกว่าหรือเท่ากับ
!= ไม่เท่ากับ
เช่นเดียวกัน
กับภาษาคอมพิวเตอร์ก็มีคำสั่งในการตัดสินใจเหมือนมนุษย์โดย
คำสั่งของคอมพิวเตอร์นั้นจะใช้คำสั่ง if แล้วตามด้วยข้อมูล
เช่น
if (a)
System.out.print("ซา-หวาด-ดี");
ก็หมายความว่าถ้าข้อมูล a เป็นจริง จาขึ้นคำว่า ซา-หวาด-ดี เป็นต้น
เมื่อเราอยากให้โปรแกรม Run ได้หลายคำสั่ง ให้เราใส่วงเล็บปีกกาหลังสิ้นสุดคำสั่ง if อีกที
เช่น
if (a) {
System.out.println("x");
System.out.println("y");
System.out.println("z");
}
โปรแกรมก็จะ Run คำสั่ง x y และ z
ในทางตรงข้าม เมื่อมันไม่ใช่คำสั่ง a โปรแกรมจะไม่ Run อะไรเลย
ยกเว้นว่าเราจะเพิ่มคำสั่ง else ไปอีกตัว จะทำให้เพิ่มคำสั่งที่ไม่เป็นจริงขึ้นมาอีกตัว
เช่น
เช่น
if (a) {
System.out.println("x");
System.out.println("y");
System.out.println("z");
}
System.out.println("y");
System.out.println("z");
}
else
System.out.println("Error code in system win33");
เมื่อเป็นอย่างนี้แล้ว
เมื่อเรา Run โปรแกรมด้วยคำสั่ง a แล้ว โปรแกรมจำพิมพ์ x y z ขึ้น
แต่ถ้าไม่ใช่คำสั่ง a เครื่องแสดง Error code in system win33 ขึ้นมาทันที
พิเศษ
นอกเหนือจากที่ผมได้เรียนมาแล้วนั้น จากม.5 ผมได้จำคำสั่ง else if ของ Delphi 6 ได้
ซึ่งในโปรแกรม Java นั้น สามารถใช้ได้เช่นกัน โดย else if จะทำหน้าที่คล้าย else มาก
ก็คือ สามารถแจกแจงความที่ได้มากกว่า else ธรรมดาโดย
สามารถใช้กับการคิดเกรดได้ หรือทายตัวเลขโดยบอกคำใบ้ด้วย
เช่น
import java.util.*;
public class Spec{
public static void main (String[]args){
public static void main (String[]args){
Scanner a = new Scanner(System.in);
float x;
System.out.print("Please enter your score. ");
x = a.nextFloat();
if(x>100){
System.out.println("Error by enter out of limited score (maximum is 100).);
}
else if(x>=80){
System.out.println("Your score is" + x);
System.out.println("Your grade is 4");
}
else if(x>=75){
System.out.println("Your score is" + x);
System.out.println("Your grade is 3.5");
System.out.println("Your grade is 3.5");
}
else if(x>=70){
System.out.println("Your score is" + x);
System.out.println("Your grade is 3");
System.out.println("Your grade is 3");
}
else if(x>=65){
System.out.println("Your score is" + x);
System.out.println("Your score is" + x);
System.out.println("Your grade is 2.5");
}
}
else if(x>=60){
System.out.println("Your score is" + x);
System.out.println("Your score is" + x);
System.out.println("Your grade is 2");
}
}
else if(x>=55){
System.out.println("Your score is" + x);
System.out.println("Your score is" + x);
System.out.println("Your grade is 1.5");
}
}
else if(x>=50){
System.out.println("Your score is" + x);
System.out.println("Your score is" + x);
System.out.println("Your grade is 1");
}
}
else{
System.out.println("Your score is" + x);
System.out.println("Your score is" + x);
System.out.println("You have failed this subject!");
}
}
System.out.println("Thankyou for investigating!");
}
}
}
ก็เป็นโปรแกรมคิดเกรดทั่วไปที่ใช้ตามโรงเรียนต่างๆ
รูปภาพการแสดงผล
คนนี้ได้เกรด 4 (80)
คนนี้ได้เกรด 2.5 (67.25)
คนนี้สอบตก (30)
สามารถติดตามผลงานได้ที่นี่
1 ความคิดเห็น:
มีความคิดสร้างสรรค์ ดีนะครับ เข้าใจเชื่อมโยงกับชีวิตดี
ตั้งใจทำงานดี
แสดงความคิดเห็น