java 流程控制基础之if else

分支结构

根据条件选择性执行某段代码;有if...else和switch-case两种分支

循环结构

根据循环条件循环执行某段代码;有while do while 、for 三种循环语句;注JDK1.5提供了foreach循环。遍历集合、数组元素

分支结构介绍

if 语句的三种格式

1. if (条件表达式){   //表达式结果为真就执行代码块的;否则直接跳过不执行

执行的代码块;

}

2. if(条件表达式){   //表达式结果为真就执行后面的代码块;否则执行else后的代码块

执行的代码块;  

}else{

执行的代码块;

}

3.if(条件表达式){   //条件表达式成立;就执行;否则就继续

代码块;

}

else if (条件表达式2){  // 执行代码块成立;

   代码块;

}

......

else{   //前面的都没有执行这个最后执行

    代码块;

}

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/*
分支结构中的if -else(条件判断结构)
第一种:三种结构
if(条件表达式){
     
    }
第二种,二选一
if(条件表达式){
      
    }else{
    代码块;
        }
 
 第三种多选一
 if(条件表达式){
     代码块;
     }else if(条件表达式){
     代码块2;
     }else if (条件表达式){
      执行代码块3;
     }
     ....
     else{
     执行代码n;
 
     }
*/
 
 
 
 
class IfTest
{
    public static void main(String[] args)
    {
        int heartBeats = 79;
        if (heartBeats < 60 || heartBeats > 100)
        {
            System.out.println("需要做进一步检查");
 
        }
            System.out.println("检查结束");
        //举例二选一
        int age = 23;
        if ( age<18 )
        {
            System.out.println("你还可以看动画片");
        }else{
            System.out.println("你可以看成人了");
            }
        //多选一
        if (age < 0)
        {
            System.out.println("您输入的数据非法");
        }else if(age< 18){
            System.out.println("青少年时期");
        }else if(age <35){
            System.out.println("青壮年");
        }else if(age <60)
        {
            System.out.println("中年时期");
        }else if(age > 120){
            System.out.println("老年时期");
        }else {
            System.out.println("你是要成仙");
        }
    }
}
 
---------- java ----------
检查结束
你可以看成人了
青壮年
 
输出完成 (耗时 0 秒) - 正常终止

  从键盘输入测试

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/*
如何从键盘获取不同的值;使用Scanner类
实现步骤
1.导包:import java.util.Scanner;
2.Scanner 的实例化
3.调用方法获取指定类型变量
 
 
*/
import java.util.Scanner;
 
class  ScannerTest
{
    public static void main(String[] args)
    {
        Scanner scan = new Scanner(System.in);
        int num = scan.nextInt();
        System.out.println(num);
    }
}

  示例2

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
/*
如何从键盘获取不同的值;使用Scanner类
实现步骤
1.导包:import java.util.Scanner;
2.Scanner 的实例化
3.调用方法获取指定类型变量
 
 
*/
import java.util.Scanner;
 
class  ScannerTest
{
    public static void main(String[] args)
    {
        Scanner scan = new Scanner(System.in);
        System.out.println("请输入姓名:");
        String name = scan.next();
        System.out.println(name);
        System.out.println("请输入年龄:");
        int age = scan.nextInt();
        System.out.println(age);
        System.out.println("请输入你的体重:");
        double weight = scan.nextDouble();
        System.out.println(weight);
        System.out.println("性别:");
        boolean islove =scan.nextBoolean();
        System.out.println(islove);
        System.out.println("男/女");
        String gender = scan.next();
        char genderChar = gender.charAt(0);
        System.out.println(genderChar);
 
    }
}

  示例2

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
/*
如何从键盘获取不同的值;使用Scanner类
实现步骤
1.导包:import java.util.Scanner;
2.Scanner 的实例化
3.调用方法获取指定类型变量
 
 
*/
import java.util.Scanner;
 
class  ScannerTest
{
    public static void main(String[] args)
    {
        Scanner scan = new Scanner(System.in);
        System.out.println("请输入成绩:");
        int score = scan.nextInt();
        if (score == 100)
        {
            System.out.println("奖励一辆车");
 
        } else if (score > 80 && score <= 99 )
        {
            System.out.println("奖励一部手机");
        } else if (score >= 60 && score <80)
        {
            System.out.println("奖励一台游戏机");
        } else {
            System.out.println("哪里也不能去在家学习");  
                }
    }
}

  测试

 

 

 比较三个数大小

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import java.util.Scanner;
class IfTest2
{
    public static void main(String[] args)
    {
        Scanner scan = new Scanner(System.in);
        System.out.println("请输入第一个整数:");
        int num1 = scan.nextInt();
        System.out.println("请输入第二个整数:");
        int num2 = scan.nextInt();
        System.out.println("请输入第三个整数:");
        int num3 = scan.nextInt();
        if (num1 >= num2)
        {
            if (num3 >= num1)
            {
                System.out.println(num2 + "," + num1 + "," + num3);
            }
            else if (num3 <= num2)
            {
                System.out.println(num3 + "," + num2 + ","+ num1);
            }
            else
            {
                System.out.println(num2 + "," + num3 + "," + num1);
            }
        }else
        {
            if (num3> num2)
            {
                System.out.println(num1 + ","+ num2 + "," +num3);
            }
            else if (num3 <= num1)
            {
                System.out.println(num3 + "," + num1 + ","+num2);
            }
            else
               {
                System.out.println(num1 + "," +num3 + "," + num2);
            }
        }
    }
}

  测试

 

 

 

 示例2

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import java.util.Scanner;
class IfEtst
{
    public static void main(String[] args)
    {
        Scanner scan = new Scanner(System.in);
        System.out.println("请输入身高:");
        int height = scan.nextInt();
        System.out.println("请输入你的财富:(千万)");
        double wealth = scan.nextDouble();
        System.out.println("请输入你是否帅:(true/false)");
        boolean isHandsome = scan.nextBoolean();
        if (height >= 180 && wealth >=1 && isHandsome)
        {
            System.out.println("我非他不嫁!");
        }else if (height >= 180 || wealth >=1 || isHandsome)
        {
            System.out.println("嫁了吧,比上不足,比下有余!");
        }else {
            System.out.println("不嫁!");
        }
 
    }
}

  测试

 

 示例2

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import java.util.Scanner;
class IfEtst
{
    public static void main(String[] args)
    {
        Scanner scan = new Scanner(System.in);
        System.out.println("请输入身高:");
        int height = scan.nextInt();
        System.out.println("请输入你的财富:(千万)");
        double wealth = scan.nextDouble();
        System.out.println("请输入你是否帅:(是/否)");
        String isHandsome = scan.next();
        if (height >= 180 && wealth >=1 && isHandsome.equals("是"))//#判断变量里的值是否与括号里的值相同
        {
            System.out.println("我非他不嫁!");
        }else if (height >= 180 || wealth >=1 || isHandsome.equals("是"))
        {
            System.out.println("嫁了吧,比上不足,比下有余!");
        }else {
            System.out.println("不嫁!");
        }
 
    }
}

  测试

 从键盘输入获取单个字符的操作

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import java.util.Scanner;
 
public class HelloWorld {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        // 获取char 类型变量,Scanner没有相关方法,只能获取一个字符串
        System.out.println("请输入您的性别:");
        String gender = scan.next();
        char genderChar = gender.charAt(0);//转换成char类型的操作;获取索引为0 为真上的字符
        System.out.println(genderChar);
    }
 
}
 
 
测试
请输入您的性别:

  

posted @   烟雨楼台,行云流水  阅读(257)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· 葡萄城 AI 搜索升级:DeepSeek 加持,客户体验更智能
· 什么是nginx的强缓存和协商缓存
· 一文读懂知识蒸馏
历史上的今天:
2019-07-22 nginx 的磁盘IO优化
点击右上角即可分享
微信分享提示