❣博主主页: 33的博客❣
▶文章专栏分类: Java从入门到精通◀
🚚我的代码仓库: 33的代码仓库🚚
目录
- 1.前言
- 2.顺序结构
- 3.分支循环
- 3.1if语句
- 3.2switch语句
 
- 4.循环结构
- 4.1while循环
- 4.2 break和continue
- 4.3 for循环
- 4.4 do while循环
 
- 5.输入输出
- 5.1输出
- 5.2输入
 
- 6.猜数字游戏
- 7.总结
1.前言
本篇文章主要介绍Java程序中的逻辑控制语句,输入输出方式以及通过一个小游戏-猜数字游戏来巩固。
本章重点
if语句,switch语句,for循环,while循环,do while循环,程序的输入输出语句。
2.顺序结构
顺序结构是比较简单的就是一行接着一行执行:
 System.out.print("a ");
 System.out.print("b ");
 System.out.print("c ");
 //输出结果a b c  
3.分支循环
3.1if语句
if语句可以分为:单分支if语句,双分支if语句,多分支if语句
 单分支格式如下:
if(布尔表达式){
//语句
}
双分支格式如下:
if(布尔表达式){
//语句1
}else{
//语句2
}
多分支格式如下:
if(布尔表达式){
//语句1
}else if{
//语句2
}else{
//语句3
}
我们来进行练习:判断一个数是正数、负数、还是0
int a=10;
 if(a>0){
    System.out.println(a+"是正数");
  }else if(a<0){
     System.out.println(a+"是负数");
   }else{
      System.out.println(a+"是0");
    }
注:else总是与最近的if语句中进行匹配
3.2switch语句
switch(表达式){
case 常量值1:{
语句1;
break;
}
case 常量值2:{
语句2;
break;
}
case 常量值3:{
语句3;
break;
}
.......
default: {
内容不满足时执行;
break;
}
}
注意:
1.多个case后面的值不能相同
2.switch的括号内只能是:byte、char、short、int、String类型、枚举型
注意:break 不要遗漏, 否则会失去 "多分支选择" 的效果,switch 不能表达复杂的条件
4.循环结构
4.1while循环
基本语法格式:
while(布尔表达式){
循环语句;
}
布尔表达式为ture,执行循环语句;否则结束。
 例1:打印1~100的数字
int num=1;
while(num<=100){
  System.out.println(num);
   num++;
}
例2:计算1!+2!+3!+4!+5!
int i=1;
int sum=0;
//外层循环
while(i<=3){
   int j=1;
   int ret=1;
// 内层循环  
    while(j<=i){
        ret *=j;
          j++;
     }
    sum +=ret;
    i++;
 }
 System.out.println(sum);
4.2 break和continue
break 的功能是让循环提前结束。
 continue 的功能是跳过这次循环, 立即进入下次循环。
//找到100-200中第一个3的倍数
int num =100;
 while (num <= 200) {
    if (num % 3 == 0) {
        System.out.println("找到了 3 的倍数, 为:" + num);
        break;
    }
    num++;
 }
4.3 for循环
基本语法格式:
for(表达式1;布尔表达式2;表达式3){
	表达式4;
}
表达式1: 用于初始化循环变量初始值设置,在循环最开始时执行,且只执行一次。
 表达式2: 循环条件,满则循环继续,否则循环结束。
 表达式3: 循环变量更新方式。
 例:计算1~100求和
int sum=0;
        for(int i=1;i<=100;i++){
            sum +=i;
        }
        System.out.println(sum);
    }
4.4 do while循环
基本语法:
do{
循环语句;
}while(循环条件);
例如:打印1~100的数
int num=0;
do{
 System.out.println(num);
    num++;
}while(num<=100)
 do while 循环最后的分号不要忘记,do while 循环至少执行一次
5.输入输出
5.1输出
在之前的程序中,我们已经频繁用刀输出语句了。
System.out.println(msg);            // 输出一个字符串, 带换行
System.out.print(msg);              // 输出一个字符串, 不带换行
System.out.printf(format, msg);     // 格式化输出,类似c语言
5.2输入
先导入从键盘输入需要的包:
 import java.util.Scanner;
 
Scanner sc=new Scanner(System.in);//固定格式 sc是自己取的名字
        System.out.println("输入姓名");
        String name= sc.nextLine();
        System.out.println("输入年龄");
        int age= sc.nextInt();
        System.out.println("姓名"+name+"年龄"+age);
6.猜数字游戏
随机数生成:
Random random = new Random(); // 默认随机种子是系统时间
int R= random.nextInt(100);//生成[1,100)的随机数
具体实现:
public static void main(String[] args) {
    System.out.println("****************");
    System.out.println("*****1.play*****");
    System.out.println("*****0.exit*****");
    System.out.println("****************");
    Scanner sc=new Scanner(System.in);
    Random random = new Random(); // 默认随机种子是系统时间
    int i= sc.nextInt();
    if(i==1){
    int rand= random.nextInt(100);
    while(true){
        System.out.println("请注入猜想数字");
        int guess= sc.nextInt();
        if(guess<rand){
            System.out.println("猜小了");
        }else if(guess>rand){
            System.out.println("猜大了");
        }else{
            System.out.println("猜对了!!!");
            break;
        }
    }
    }
    if(i==0){
        System.out.println("退出游戏");
    }
}
7.总结
本篇文章主要介绍Java程序中的逻辑控制语句,if语句,switch语句,for循环,while循环,do while循环,以及程序的输入输出语句最后通过一个小游戏来巩固。
下期预告:方法的使用







![个人网站制作 Part 13 添加搜索功能[Elasticsearch] | Web开发项目](https://img-blog.csdnimg.cn/direct/379c7f88b2464bfb970984829ff4f114.jpeg#pic_center)
![[论文笔记] Dual-Channel Span for Aspect Sentiment Triplet Extraction](https://img-blog.csdnimg.cn/img_convert/b9f1b3a80a11baf04f6d2162abd203c8.png)


![[Qt学习笔记]Qt下使用Halcon实现采图时自动对焦的功能(Brenner梯度法)](https://img-blog.csdnimg.cn/img_convert/4064a5f4ca724be1204bfb230c3f141c.webp?x-oss-process=image/format,png)







