IF与Switch的结构
IF单选择结构
package com.JIE.struct;
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();
}
}
.equals:判断字符串是否相等
IF选择结构
package com.JIE.struct;
import java.util.Scanner;
public class IfDemo02 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入成绩");
int i = scanner.nextInt();
if (i==100){
System.out.println("优秀");
}else if(i<100 && i>=90) {
System.out.println("A");
}else if(i>=80 && i<=90) {
System.out.println("B");
}else if(i>=70 && i<=80) {
System.out.println("C");
}else if(i>=60 && i<70) {
System.out.println("D");
}else if(i<60 && i>=0){
System.out.println("成绩不合格");
}else {
System.out.println("输入不合法");
}
scanner.close();
}
}
编写代码时,一定要严谨!
嵌套循环判断
public class IfDemo03 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入查找名:");
String i = scanner.next();
if (i.equals("张泽龙")){
System.out.println("请输入成绩:");
int o = scanner.nextInt();
if (o>=600){
System.out.println("恭喜考入带本成为 人上人上人");
}else if(o<600 && o>=300){
System.out.println("恭喜考入带专 成为人上人");
}else {
System.out.println("来世再做带专人");
}
}else {
System.out.println("输入有误");
}
scanner.close();
}
}
Switch多选择结构
public class Switch {
public static void main(String[] args) {
//switch 匹配一个具体的值
char grade = 'A';
switch (grade){
case'A': //case穿透 不加break结束则会全部输出
System.out.println("优秀");
break;
case'B':
System.out.println("良好");
break;
case'C':
System.out.println("及格");
break;
case'D':
System.out.println("再接再厉");
break;
default:
System.out.println("未知等级");
}
}
}