if分支结构:
若成立,则执行语句块
若不成立,则跳过语句块
if(条件表达式){
语句块;
}
//简单的if判断代码
import java.util.Scanner;
public class IfTest{
public static void main(String[] args){
System.out.println("请输入您的年龄:");
Scanner sc = new Scanner(System.in);
int age = sc.nextInt();
if(age >= 18){
System.out.println("可以上网");
}
System.out.println("未到上网年龄");
}
}
/*
编程找到两个数的最大值
*/
import java.util.Scanner;
public class TwoNumComperTest{
public static void main(String[] args){
System.out.println("请输入两个整数");
Scanner sc = new Scanner(System.in);
int a1 = sc.nextInt();
int a2 = sc.nextInt();
if(a1 >= a2){
System.out.println("最大的数为:" + a1);
}
if(a1 < a2){
System.out.println("最大的数为:" + a2);
}
System.out.println("-------------------------方法二,第三变量替换---------------------------------" );
int max = a1;
if(a2 > max){
max = a2;
}
System.out.println("最大的数为:" + max);
}
}
/*
编程找到三个数的最大值
*/
import java.util.Scanner;
public class test{
public static void main(String[] args){
System.out.println("输入三个数:");
Scanner sc = new Scanner(System.in);
int a1 = sc.nextInt();
int a2 = sc.nextInt();
int a3 = sc.nextInt();
int max = a1;
if(a2 > a1){
max = a2;
}
if(a3 > a2){
max = a3;
}
System.out.println("最大是为:" + max);
}
}
if else分支结构:
若成立,则执行语句块1
若不成立,则执行语句块2
if(条件表达式){
语句块1;
}else {
语句块2;
}
import java.util.Scanner;
/*
编程使用if else 分支结构来模拟考试成绩查询的过程
*/
public class IfElseTest{
public static void main(String[] args){
//1、提示用户输入考试成绩并用变量记录
System.out.println("请输入你的考试成绩:");
Scanner sc = new Scanner(System.in);
int source = sc.nextInt();
//2、使用if else分支结构判断考试成绩是否及格并给出提示
if(source >= 60){
System.out.println("考试成绩通过:"+ source);
}else{
System.out.println("考试未通过:"+ source);
}
}
}
if else if else分支结构:
(3)判断条件表达式1是否成立
===》若成立,则执行语句块1;
===》若不成立,则判断条件表达式2是否成立
===》若成立,则执行语句块2;
===》若不成立,则执行语句块n;
if(条件表达式1){
语句块1;}
else if(条件表达式2){
语句块2;}
else{
语句块n;
}
/*
编程实现if else if else分支结构的使用,来模拟购买火车票的过程
*/
import java.util.Scanner;
public class IfElseIfTest{
public static void main(String[] args){
//1、提示用户输入身份证信息并使用变量记录
System.out.print("请输入您的身份证信息:");
Scanner sc = new Scanner(System.out);
String str = sc.next();
//2、使用if else if else分支结构判断身份信息并给出对应的提示
if("军人".equals(str){
Syetem.out.println("请免费乘车");
}else if("学生".equals(str)){
System.out.println("请购买半价票!");
}else {
System.out.println("请购买全价票!");
}
}
}
/*
根据用户输入的薪水计算个人所得税并打印出来,使用if else if else
*/
import java.util.Scanner;
public class Tax{
public static void main(String[] args){
//1、提示用户输入个人的薪水并使用变量记录
System.out.print("请输入您的薪水:");
Scanner sc = new Scanner(System.in);
//这里是局部变量,作用范围从声明开始一直到方法体结束
int salary = sc.nextInt();
//2、使用if else if else分支结构判断薪水所在的范围并计算对应的个人所得税
double salaryPrice = 0.0;
if(salary <= 5000){
System.out.println("无需纳税!");
}else if(salary <= 8000){
//块变量:作用范围从声明开始一直到当前语块结束
salaryPrice = (salary - 5000 ) * 0.03;
}else if(salary <= 17000){
salaryPrice = (salary - 8000 ) * 0.1 + (8000 - 5000) * 0.03;
}else if(salary <= 30000){
salaryPrice = (salary - 17000) * 0.2 + (17000 - 8000) * 0.1 + (8000 - 5000) * 0.03;
}
//3、打印最终的计算结果
System.out.println("最终的个人所得税是:"+ salaryPrice);
}
}
/*
出租车计费方式
*/
import java.util.Scanner.
public class TaxiTest{
public static void main(String[] args){
//1、提示用户输入公里数和等待的秒数并用变量记录
System.out.println("请输入公里数和等待的秒数");
Scanner sc = new Scanner(System.in);
int km = sc.nextInt();
int second = sc.nextInt();
//2、根据公里数计算对应的里程费用使用变量记录
int kmPrice = 0;
if(km <= 3){
kmPrice = 13;
}else if(km <= 15){
kmPrice = 13 + (km - 3) * 2;
}else{
kmPrice = 13 + (15 - 3) * + (km - 15) * 3;
}
//3、根据等待的秒数计算对应的等待费并使用变量记录
int secondPrice = second / 150;
//4、计算总费用并打印
int sumPrice = kmPrice + secondPrice;
System.out.println(“总费用”+ sumPrice);
}
}
Switch case分支结构:
switch(变量/表达式){
case 字面值1:语句块1;break;
case 字面值2:语句块2;break;
…
default:语句块n;
}
/*
使用switch case分支结构实现考试等级判断
*/
import java.util.Scanner;
public class SoreTest{
public static void main(String[] args){
//1、提示用户输入考试成绩并用变量记录
System.out.println(“请输入您的考试分数:”);
Scanner sc = new Scanner(System.in);
int score = sc.nextInt();
//2、使用switch case分支结构实现考试成绩的等级判断
switch(score / 10){
//如果后面没break 将会实现case穿透输出
case 10;System.out.println(“等级A”);break;
case 9;System.out.println(“等级B”);break;
case 8;System.out.println(“等级C”);break;
case 7;System.out.println(“等级D”);break;
case 6;System.out.println(“等级E”);break;
default;System.out.println(“等级A”);break;
}
}
}
这篇博客详细介绍了Java编程中if、if...else、if...elseif...else和switch...case等条件分支结构的使用。通过示例代码展示了如何判断年龄、找到两个数或三个数的最大值、模拟考试成绩查询、购买火车票过程、计算个人所得税、出租车计费以及考试等级判断等实际应用场景。这些例子有助于理解条件分支在解决实际问题中的重要作用。
590

被折叠的 条评论
为什么被折叠?



