选择结构
- if单选择结构
import java.util.Scanner; public class IfDemo01 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("请输入内容:"); String s = scanner.nextLine(); //equals:判断字符串是否相等 if (s.equals("Hello")){ System.out.println(s); } System.out.println("End"); scanner.close(); }
if双选择结构
import java.util.Scanner;
public class IfDemo02 {
public static void main(String[] args) {
// 考试分数大于60就是及格,小于60就是不及格
Scanner scanner = new Scanner(System.in);
System.out.println("请输入成绩:");
int score = scanner.nextInt();
if (score > 60 ){
System.out.println("及格");
}else {
System.out.println("不及格");
}
scanner.close();
}
}