if选择结构
if单选择结构
package struct;
import java.util.Scanner;
**/
public class IfDemo01 {
public static void main(String[] args) {
//单if结构
Scanner scanner= new Scanner(System.in);
System.out.println("请输入内容:");
String s= scanner.nextLine();
if (s.equals("Hello")){
System.out.println("hello");
}
System.out.println("End");
scanner.close();
}
}
if双选择结构
package struct;
import java.util.Scanner;
public class IfDemo02 {
public static void main(String[] args) {
//if双选择结构
Scanner scanner=new Scanner(System.in);
System.out.println("请输入成绩:");
int score=scanner.nextInt();
if (score>=60){
System.out.println("成绩及格!");
}else{
System.out.println("成绩不合格!");
}
}
}
if多选择结构
package struct;
import java.util.Scanner;
/**
* TODO
*
* @version V3.0
* @authorAdministrator
* @since14:28
**/
public class IfDemo03 {
public static void main(String[] args) {
//给成绩分级,A.B.C.D
Scanner scanner= new Scanner(System.in);
System.out.println("请输入成绩:");
int score=scanner.nextInt();
if (score==100){
System.out.println("恭喜满分!!");
}else if(score>=90 && score<100){
System.out.println("A级");
}else if (score>=80 && score<100){
System.out.println("B级");
}else if(score>=70 && score<100){
System.out.println("C级");
}else if(score>=60 && score<100){
System.out.println("D级");
}else if(score<60 && score<100){
System.out.println("成绩不及格");
}else
System.out.println("非法成绩!");
scanner.close();
}
}
嵌套的if结构
·
package struct;
import java.util.Scanner;
public class IfDemo04 {
public static void main(String[] args) {
Scanner scanner= new Scanner(System.in);
//需要找一个数,在1和100之间
System.out.println("请输入数字");
int i=scanner.nextInt();
if (i>1 && i<100){
if(i>50 && i<100){
System.out.println("在50和100之中");
if(i<50 && i>1){
System.out.println("在1和50之中");
}else if (i==1){
System.out.println("这个数为1");
}else if (i==50){
System.out.println("这个数为50");
}else if (i==100){
System.out.println("这个数为100");
}
}
}else
System.out.println("不在范围内!");
scanner.close();
}
}