if 和 else
else 为不满足任意条件时执行
package com.mek.demo;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// 创建一个Scanner实例对象
Scanner scanner = new Scanner(System.in);
System.out.println("input passwd:");
String passwd = scanner.nextLine();
// equals 字符串比较
if(passwd.equals("admin")){
System.out.println("passwd Yes!");
}else {
System.out.println("passwd Fail!");
}
// 关闭Scanner实例对象
scanner.close();
}
}
if和else if 嵌套
多条件判断
package com.mek.demo;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// 创建一个Scanner实例对象
Scanner scanner = new Scanner(System.in);
System.out.println("input your wallet(int class):");
int wallet = scanner.nextInt();
if(wallet >= 500000){
System.out.println("affluence!");
}else if (wallet >= 200000){
System.out.println("ordinary!");
}else if (wallet >= 100000){
System.out.println("So so!");
}else if (wallet >= 50000){
System.out.println("poor!");
}else {
System.out.println("Go to hell!");
}
// 关闭Scanner实例对象
scanner.close();
}
}