Java学习第十五课:if选择结构
作者:互联网
if选择结构
语法:
if(布尔表达式){
//如果布尔表达式为true将执行的语句
}
if单选择结构:
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入内容:");
String s = scanner.nextLine();
if (s.equals("人类消灭暴政")){
System.out.println("世界属于三体!");
}
}
if双选择结构:
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入内容:");
String s = scanner.nextLine();
if (s.equals("人类消灭暴政")){
System.out.println("世界属于三体!");
}else {
System.out.println("回答错误");
}
}
public static void main(String[] args) {
while (true) {
System.out.println("请输入您的成绩:");
Scanner scanner = new Scanner(System.in);
int a = scanner.nextInt();
if (a >= 60) {
System.out.println("成绩及格");
} else {
System.out.println("成绩不及格");
}
}
}
if多选择结构:
public static void main(String[] args) {
while (true){
System.out.println("请输入你的成绩:");
Scanner scanner = new Scanner(System.in);
double a = scanner.nextDouble();
if (a == 100){
System.out.println("恭喜你获得了满分!");
}else if (a < 100 && a >= 90){
System.out.println("优秀");
}else if (a < 90 && a >=80){
System.out.println("良好");
}else if (a < 80 && a >=70){
System.out.println("良好");
}else if (a < 70 && a >=60){
System.out.println("良好");
}else if (a < 60 ){
System.out.println("不及格");
}else {
System.out.println("请输入0-100之间的数:");
}
}
}
标签:Java,scanner,System,else,学习,println,第十五,Scanner,out 来源: https://www.cnblogs.com/jindaowei/p/16033562.html