格式1:if (条件表达式) { 代码 }
//需求:心跳(60-100)之间是正常的,否则系统提示进一步检查
int heartbeat = 0;
if (heartbeat < 60 || heartbeat >100) {
System.out.println("您的心跳数据是:" + heartbeat + ",你可能需要进一步检查!");
}
System.out.println("检查结束");
格式2:if (条件表达式) {代码…} else { 代码…}
//需求:大红包。
double money = 520;
//发送一个1314的红包
if (money >=1314) {
System.out.println("您当前发送红包成功~~~~~~~~");
}else {
System.out.println("您自己都没钱,就别发了~~~~~~~~~~");
}
//格式3:if(条件表达式){代码…}else if(条件表达式){代码…}…else{代码…}
//绩效系统:0-60 C 60-80 B 80-90 A 90-100 A+
double score;
Scanner sc = new Scanner(System.in);
System.out.println("请输入你的绩效");
score = sc.nextDouble();
if (score >= 0 && score < 60){
System.out.println("你的绩效等级为:C");
}else if (score >= 60 && score <80){
System.out.println("你的绩效等级为:B");
}else if (score >=80 && score <90){
System.out.println("你的绩效等级为:A");
}else if (score > 100 || score < 0){
System.out.println("你的绩效等级有毛病");
}else {
System.out.println("你的绩效等级为:A+");
}
本小结全部代码
package com.mumei.branch;
import java.util.Scanner;
/*
目标:学会使用if分支结构解决问题,理解其流程
*/
public class IfDemo1 {
public static void main(String[] args) {
//需求:心跳(60-100)之间是正常的,否则系统提示进一步检查
//格式1:if (条件表达式) { 代码 }
int heartbeat = 0;
if (heartbeat < 60 || heartbeat >100) {
System.out.println("您的心跳数据是:" + heartbeat + ",你可能需要进一步检查!");
}
System.out.println("检查结束");
//格式2:if (条件表达式) {代码...} else { 代码...}
//需求:大红包。
double money = 520;
//发送一个1314的红包
if (money >=1314) {
System.out.println("您当前发送红包成功~~~~~~~~");
}else {
System.out.println("您自己都没钱,就别发了~~~~~~~~~~");
}
//格式3:if(条件表达式){代码...}else if(条件表达式){代码...}...else{代码...}
//绩效系统:0-60 C 60-80 B 80-90 A 90-100 A+
double score;
Scanner sc = new Scanner(System.in);
System.out.println("请输入你的绩效");
score = sc.nextDouble();
if (score >= 0 && score < 60){
System.out.println("你的绩效等级为:C");
}else if (score >= 60 && score <80){
System.out.println("你的绩效等级为:B");
}else if (score >=80 && score <90){
System.out.println("你的绩效等级为:A");
}else if (score > 100 || score < 0){
System.out.println("你的绩效等级有毛病");
}else {
System.out.println("你的绩效等级为:A+");
}
}
}