DAY 7
0
Software Development

## [Day07] CH05：如果我有一座新冰箱——if/else 條件判斷

``````float quotient = (float) num1 / num2;
``````

``````System.out.printf("Quotient: %.2f%n", quotient);
``````

``````import java.util.Scanner;

public class Division {
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
System.out.print("Enter first number: ");
int num1 = sc.nextInt();
System.out.print("Enter second number: ");
int num2 = sc.nextInt();
float quotient = (float) num1 / num2;
System.out.printf("Quotient: %.2f%n", quotient);
sc.close();
}
}
``````

``````if(條件式1){
System.out.println(“條件式1成立”);
}
``````

``````if(條件式1){
System.out.println(“條件式1成立”);
}
else{
System.out.println(“條件式1不成立”);
}
``````

``````if(條件式1){
System.out.println(“條件式1成立”);
}
else if(條件式2){
System.out.println(“條件式2成立”);
}
else{
System.out.println(“條件式1、條件式2不成立”);
}
``````

``````import java.util.Scanner;

public class Division2 {
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
System.out.print("Enter first number: ");
int num1 = sc.nextInt();
System.out.print("Enter second number: ");
int num2 = sc.nextInt();
if(num2 == 0){
System.out.println("除數不得為 0");
}
else{
float quotient = (float) num1 / num2;
System.out.printf("Quotient: %.2f%n", quotient);
}
sc.close();
}
}
``````