10. 5.2
5.2 คำสั่ง
คำสั่ง if…then…else…
if…then…else…
//Program If1.java
package chapter5;
public class If1 {
public static void main(String args[ ]) {
int x = Integer.parseInt(args[0]);
int y = Integer.parseInt(args[1]);
if (y == 0) {
System.out.println("Program Exit: Divided by zero!");
System.exit(0);
}
System.out.println("args[0]/args[1] = " + x/y);
}
}
11. 5.2
5.2 คำสั่ง
คำสั่ง if…then…else…
if…then…else…
//Program If1WithElse.java
public class If1WithElse {
public static void main(String args[]) {
int x = Integer.parseInt(args[0]);
int y = Integer.parseInt(args[1]);
if (y == 0)
System.out.println("Program Exit: Divided by zero!");
else
System.out.println("args[0]/args[1] = " + x/y);
}
}
13. //Program IfWeight.java
class Weight{
private int gender;
private float height;
public void setGender(int gender) {
this.gender = gender;
}
public void setHeight(float height) {
this.height = height;
}
public float calculate( ) {
float weight;
if (gender == 1)
weight = height-100;
else
weight = height-110;
return(weight);
}
}
14. //Program IfWeight.java
import java.util.Scanner;
class IfWeight {
static Scanner console = new Scanner(System.in);
public static void main (String [ ] args) {
int gender; float height, weight;
Weight weightCalculator = new Weight( );
System.out.print("Enter '1' for Male or '2' for female : ");
gender = console.nextInt();
if ((gender == 1) || (gender ==2)) {
System.out.print("Please enter your height : ");
height = console.nextFloat();
weightCalculator.setGender(gender);
weightCalculator.setHeight(height);
weight = weightCalculator.calculate();
System.out.print("Your weight should not more
than "+ weight + " kgs.");
} else System.out.print("Invalid gender.");
}
}
15. //Program IfNumber.java
class CheckNumber {
private int number;
private boolean even;
public void setNumber(int number) {
this.number = number;
}
public boolean checkEven() {
if (number % 2 == 0)
even = true; //even number
else
even = false; //odd number
return(even);
}
}
16. //Program IfNumber.java
package chapter5;
import java.util.Scanner;
public class IfNumber {
static Scanner console = new Scanner(System.in);
public static void main ( String [ ] args ) {
int number;
CheckNumber checkNum = new CheckNumber( );
System.out.print("Enter integer number for checking : ");
number = console.nextInt();
checkNum.setNumber(number);
if ( checkNum.checkEven( ) ) {
System.out.println("The no. "+ number + " is an
even number.");
}
else {
System.out.println("The no. "+ number + " is an
odd number.");
}
} //main
}
18. //Program IfGrade.java
public class IfGrade {
public static void main(String args[]) {
int x = Integer.parseInt(args[0]);
char grade;
if (x >= 80) {
grade = 'A';
System.out.println("Score : "+ x + ", Grade = " +
grade);
} else if (x >= 70) {
grade = 'B';
System.out.println("Score : "+ x + ", Grade = " +
grade);
} else if (x >= 60) {
grade = 'C';
System.out.println("Score : "+ x + ", Grade = " +
grade);
} else if (x >= 50) {
grade = 'D';
System.out.println("Score : "+ x + ", Grade = " +
grade);
} else {
grade = 'F';
System.out.println("Score : "+ x + ", Grade = " +
grade);
}
}
}
19. //Program IfGradeClass.java
package chapter5;
public class IfGradeClass {
public static void main ( String [ ] args ) {
int x = Integer.parseInt(args[0]);
char grade;
Grade gradeCal = new Grade();
gradeCal.setScore(x);
grade = gradeCal.calGrade();
System.out.println("Score : "+ x + ", Grade = " + grade);
}
}
20. class Grade {
private int score;
private char grade;
public void setScore(int score) {
this.score = score;
}
public char calGrade( ) {
if ( score <= 50 )
grade = 'F';
else if ( score < 60 )
grade = 'D';
else if ( score < 70 )
grade = 'C';
else if ( score < 80 )
grade = 'B';
else
grade = 'A';
return(grade);
}
}
21. 5.3
5.3 คำสั่ง
คำสั่ง if..then..else
if..then..else แบบต่อเนื่อง
แบบต่อเนื่อง
ตัวอย่างที่ 5.7 ให้พิจารณาข้อมูลนักศึกษา ถ้าเป็นนักศึกษาชั้นปีที่ 1 และมี
เกรดเฉลี่ยไม่ต่ำกว่า 1.75 ให้ขึ้นข้อความแนะนำว่าควรจะลงทะเบียนไม่เกิน
18 หน่วยกิต แต่ถ้าเป็นนักศึกษาชั้นปีอื่น และมีเกรดเฉลี่ยไม่ต่ำกว่า 2.00 ให้
ขึ้นข้อความแนะนำว่าควรจะลงทะเบียนไม่เกิน 21 หน่วยกิต
if (year == 1 && gpa >= 1.75)
System.out.println (“Units
registered should not be
more than 18.”);
else if (year >= 2 && gpa >= 2.00)
System.out.println (“Unit
registered
should not be
more than 21.”);
if (year == 1) {
if (gpa >= 1.75)
System.out.println(“Units
registered should not be
more
than 18.”);
}
else { /* ไม่ใช่ปี 1 */
if (gpa >= 2.00)
System.out.println(“Unit
registered should not be
more than 21.”);
24. //Program SwitchBreak.java
class SwitchBreak {
public static void main(String args[ ]) {
String input = args[0].toUpperCase();
char x = input.charAt(0);
switch(x) {
case 'A' : System.out.println("Hello!");
System.out.println("You entered " + x);
break;
case 'B' : System.out.println("Good Morning");
System.out.println("You entered " + x);
break;
case 'C' : System.out.println("Good Afternoon");
System.out.println("You entered " + x);
break;
case 'D' : System.out.println("Good Evening");
System.out.println("You entered " + x);
break;
case 'E' : System.out.println
("Good Night");
System.out.println
("You entered " + x);
break;
case 'F' : System.out.println
("Good Day");
System.out.println
("You entered " + x);
break;
default : System.out.println
("Good Bye!");
System.out.println
("You entered " + x);
}
}// main
}// class
25. class SwitchGreeting1 {
public static void main(String args[]) {
int x = Integer.parseInt(args[0]);
switch(x) {
case 1 : System.out.println("Action is better than word."); break;
case 2 : System.out.println("Action is better than word."); break;
case 3 : System.out.println("Action is better than word."); break;
case 4 : System.out.println("Beauty is in the eyes of beholder."); break;
case 5 : System.out.println("Beauty is in the eyes of beholder."); break;
case 6 : System.out.println("Beauty is in the eyes of beholder."); break;
case 7 : System.out.println("Life is a long journey."); break;
case 8 : System.out.println("Life is a long journey."); break;
case 9 : System.out.println("Life is a long journey."); break;
case 10 : System.out.println("Life is a long journey."); break;
default : System.out.println("All the best.");
}
}
}
26. class SwitchGreeting2 {
public static void main(String args[]) {
int x = Integer.parseInt(args[0]);
switch(x) {
case 1 :
case 2 :
case 3 : System.out.println("Action is better than word.");
break;
case 4 :
case 5 :
case 6 : System.out.println("Beauty is in the eyes of beholder.");
break;
case 7 :
case 8 :
case 9 :
case 10 : System.out.println("Life is a long journey.");
break;
default : System.out.println("All the best.");
}
}
}
27. class SwitchIfGreeting {
public static void main(String args[]) {
int x = Integer.parseInt(args[0]);
if (x == 1 | x == 2 | x == 3)
System.out.println("Action is better than
word.");
else if (x == 4 | x == 5 | x == 6)
System.out.println("Beauty is in the eyes of
beholder.");
else if (x == 7 | x == 8 | x == 9 | x == 10)
System.out.println("Life is a long journey.");
else System.out.println("All the best.");
}
}
28. class Payment {
private int custType;
private float price, discountRate,
private float discountAmount, pay;
public void setType(int type) {
custType = type;
}
public void setPrice(float price) {
this.price = price;
}
public float getPrice( ) {
return(price);
}
public float getDiscount( ) {
return(discountAmount);
}
public float getPay( ) { return(pay); }
public void totalPay( ) {
switch (custType) {
case 1 : discountRate = 0.05f;
break;
case 2 : discountRate = 0.10f;
break;
}
discountAmount = price * discountRate;
pay = price - discountAmount;
} totalPay()
} //class