if语句的相关概念
package com.itheima;
import java.util.Scanner;
public class 分支结构 {
public static void main(String[] args) {
//键盘录入
int wine;
Scanner sc = new Scanner(System.in);
System.out.println("请输入:");
wine = sc.nextInt();
//注意事项:
//如果if下有多条语句,必须加上大括号
//如果只有一条可以省略
if(wine > 2) {
System.out.println("可以");
}
else{
System.out.println("不可以");
}
//对bool类型类型进行判断事,不要用==号
boolean flag = false;
if(flag = true) {
System.out.println("flag的值位true");
}
if(flag==true){
System.out.println("flag的值为ture");
}
//if (if else) (else)
Scanner sc1=new Scanner(System.in);
System.out.println("请录入成绩");
int score = sc1.nextInt();
//对异常数据进行校验
//0-100合法数据
if(score>=0&&score<=100) {
//开分化等级
if (score >= 90 && score <= 100) {
System.out.println("A");
} else if (score < 90 && score >= 60) {
System.out.println("B");
} else {
System.out.println("C");
}
}
else {
System.out.println("数据不合法");
}
}
}
if语句的相关练习
package com.itheima;
import java.util.Scanner;
public class 判断与循环的习题 {
public static void main(String[] args) {
//简单练习
int rank=1;
if(rank==1){
System.out.println("可以");
}
else{
System.out.println("不可以");
}
//键盘录入会员的级别,商品总价为10000
//会员1级 7折
//会员2级 6折
//会员3级 5折
//无会员 不打折
int price =10000;
Scanner sc=new Scanner(System.in);
System.out.println("请输入会员的级别");
int vip= sc.nextInt();
if(vip==1){
System.out.println("支付的金额为"+(price*0.7));
}
else if(vip==2){
System.out.println("支付的金额为"+(price*0.6));
}
else if(vip==3){
System.out.println("支付的金额为"+(price*0.5));
}
else {
System.out.println("支付的金额为"+price);
}
}
}