Java 流程控制
一、用户交互 Scanner
1. 通过 Scanner 对象来获取用户输入
基本语法:Scanner s = new Scanner(system.in);
使用前需要导入 'java.util.Scanner' 这个包,( import java.util.Scanner; )
通过 'Scanner' 类的 'next()' 和 'nextLine()' 方法获取输入的字符串,在读取前我们一般需要使用 'hasNext()' 与 'hasNextLine()' 判断是否还有输入的数据
next() :
- 一定要读取到有效字符后才可以结束输入
- 对输入有效字符之前的空白,会自动将其去掉
- 只有在输入有效字符后,将后面的空白作为分隔符或结束符
- next() 不能得到带有空格的字符串
| import java.util.Scanner; |
| public class Demo01 { |
| public static void main(String[] args) { |
| |
| Scanner s = new Scanner(System.in); |
| |
| System.out.println("使用next()方法接收:"); |
| |
| String str = s.next(); |
| System.out.println("输入的字符串是:" + str); |
| |
| s.close(); |
| } |
| } |
nextLine() :
- 以 Enter 回车为结束符,返回输入回车之前的所有字符
- 可以获得空白
| import java.util.Scanner; |
| public class Demo02 { |
| public static void main(String[] args) { |
| Scanner s = new Scanner(System.in); |
| System.out.println("使用nextLine()方法接收"); |
| |
| String str = s.nextLine(); |
| System.out.println("输入的内容为:" + str); |
| |
| s.close(); |
| } |
| } |
2. Scanner的其他用法
| import java.util.Scanner; |
| public class Demo03 { |
| public static void main(String[] args) { |
| |
| Scanner s = new Scanner(System.in); |
| int i = 0; |
| float f = 0.0f; |
| |
| System.out.println("请输入整数:"); |
| |
| if (s.hasNextInt()){ |
| i = s.nextInt(); |
| System.out.println("整数数据:" + i); |
| } |
| else { |
| System.out.println("输入的不是整数"); |
| } |
| |
| System.out.println("请输入小数:"); |
| |
| if (s.hasNextFloat()){ |
| f = s.nextFloat(); |
| System.out.println("小数数据:" + f); |
| } |
| else { |
| System.out.println("输入的不是小数"); |
| } |
| |
| s.close(); |
| } |
| } |
| import java.util.Scanner; |
| public class Demo04 { |
| public static void main(String[] args) { |
| |
| Scanner s = new Scanner(System.in); |
| double sum = 0; |
| int m = 0; |
| |
| |
| while (s.hasNextDouble()){ |
| double x = s.nextDouble(); |
| m = m + 1; |
| sum = sum + x; |
| } |
| System.out.println(m + "个数的和:"+ sum); |
| System.out.println(m + "个数的平均值:" + sum/m); |
| s.close(); |
| } |
| } |
| |
二、顺序结构
- 语句之间,从上到下依次运行
- 它是任何一个算法都离不开的基本算法结构
三、选择结构
1. if 单选择结构
| import java.util.Scanner; |
| public class Demo05 { |
| public static void main(String[] args) { |
| Scanner s = new Scanner(System.in); |
| System.out.println("请输入:"); |
| String str = s.nextLine(); |
| |
| |
| if (str.equals("Hello")){ |
| System.out.println(str); |
| } |
| System.out.println("end"); |
| s.close(); |
| } |
| } |
2. if 双选择结构
| import java.util.Scanner; |
| public class Demo06 { |
| public static void main(String[] args) { |
| Scanner s = new Scanner(System.in); |
| System.out.println("请输入成绩"); |
| double score = s.nextDouble(); |
| if (score>60){ |
| System.out.println("及格"); |
| } |
| else{ |
| System.out.println("不及格"); |
| } |
| s.close(); |
| } |
| } |
3. if 多选择结构
| import java.util.Scanner; |
| public class Demo07 { |
| public static void main(String[] args) { |
| Scanner s = new Scanner(System.in); |
| System.out.println("请输入成绩:"); |
| double score = s.nextDouble(); |
| if (score<100 && score >= 90){ |
| System.out.println("A"); |
| } |
| else if (score < 90 && score >= 80){ |
| System.out.println("B"); |
| } |
| else if (score < 80 && score >= 70) { |
| System.out.println("C"); |
| } |
| else if (score < 70 && score >= 60){ |
| System.out.println("D"); |
| } |
| else if (score < 60 && score >= 0){ |
| System.out.println("E"); |
| } |
| else { |
| System.out.println("成绩不合法"); |
| } |
| s.close(); |
| } |
| } |
4. if 嵌套选择结构
| import java.util.Scanner; |
| public class Demo08 { |
| public static void main(String[] args) { |
| Scanner s = new Scanner(System.in); |
| double score = s.nextDouble(); |
| if (score > 60){ |
| if (score < 70){ |
| System.out.println("D"); |
| } |
| else if (score < 80 ){ |
| System.out.println("C"); |
| } |
| else if (score < 90){ |
| System.out.println("B"); |
| } |
| else{ |
| System.out.println("A"); |
| } |
| } |
| else{ |
| System.out.println("E"); |
| } |
| s.close(); |
| } |
| } |
5. switch 选择结构
- switch case 语句判断一个变量与一系列值中的某个值是否相等
- switch 语句中的变量类型可以是:byte 、short 、int 、char 或 String
- case 标签必须为字符常量或字面量
| import java.util.Scanner; |
| public class Demo09 { |
| public static void main(String[] args) { |
| Scanner s = new Scanner(System.in); |
| int score = s.nextInt(); |
| switch (score){ |
| case 90: |
| System.out.println("A"); |
| break; |
| case 80: |
| System.out.println("B"); |
| break; |
| case 70: |
| System.out.println("C"); |
| break; |
| case 60: |
| System.out.println("D"); |
| break; |
| default: |
| System.out.println("E"); |
| } |
| } |
| } |
四、循环结构
1. while 循环
while (条件表达式) { 循环体 }
- 只要条件表达式为 true ,循环就会一直执行下去
| public class Demo10 { |
| public static void main(String[] args) { |
| |
| int i = 0; |
| int sum = 0; |
| while (i <= 100){ |
| sum = sum + i; |
| i++; |
| } |
| System.out.println(sum); |
| } |
| } |
2. do ... while 循环
do ... while 循环至少会执行一次
do { 代码块 } while ();
| public class Demo11 { |
| public static void main(String[] args) { |
| int i = 0; |
| int sum = 0; |
| do { |
| sum = sum + i; |
| i++; |
| }while(i <= 100); |
| System.out.println(sum); |
| } |
| } |
3. for 循环
for 循环是支持迭代的一种通用结构,是最有效、最灵活的循环结构
for ( 初始化; 条件表达式; 更新 ) { 代码块 }
| public class Demo12 { |
| public static void main(String[] args) { |
| |
| for (int i=0;i<=10;i++){ |
| System.out.println(i); |
| } |
| } |
| } |
计算 0-100 之间奇数和偶数的和
| public class Demo13 { |
| public static void main(String[] args) { |
| |
| int oddSum = 0; |
| int evenSum = 0; |
| for (int i = 0; i <= 100; i++) { |
| if (i%2 != 0){ |
| oddSum = oddSum + i ; |
| } |
| else{ |
| evenSum = evenSum + i; |
| } |
| } |
| System.out.println(oddSum); |
| System.out.println(evenSum); |
| } |
| } |
输出 1 - 1000 之间能被5整除的数,每行输出3个
| public class Demo14 { |
| public static void main(String[] args) { |
| |
| for (int i = 1; i <= 1000; i++) { |
| if (i%5==0){ |
| System.out.print(i+"\t"); |
| } |
| if (i%(5*3)==0){ |
| System.out.print("\n"); |
| } |
| } |
| } |
| } |
打印九九乘法表:
| public class Demo15 { |
| public static void main(String[] args) { |
| |
| for (int i=1;i<=9;i++){ |
| for (int j=1;j<=i;j++){ |
| System.out.print(j+"*"+i+"="+i*j+"\t"); |
| } |
| System.out.println(); |
| } |
| } |
| } |
4. 增强型 for 循环
主要用于数组或集合
for ( 声明语句 :表达式) { 代码块 }
- 申明语句:申明新的局部变量,该变量的类型必须和数组元素的类型一致,其作用域限定在循环体内,其值与此时数组元素的值相等。
- 表达式:是要访问的数组名,或者是返回值为数组的方法。
| public class Demo16 { |
| public static void main(String[] args) { |
| int[] numbers = {10,20,30,40,50}; |
| |
| for (int i=0;i<5;i++){ |
| System.out.println(numbers[i]); |
| } |
| |
| for (int x:numbers){ |
| System.out.println(x); |
| } |
| } |
| } |
五、break & continue
- 'break' 在任何循环语句的主体部分,均可用 'break' 控制循环的流程,用于强行退出循环。
- 'continue' 用在循环语句中,用于终止某次循环,跳过本次循环体中未执行的语句,接着下一次循环。
| public class Demo17 { |
| public static void main(String[] args) { |
| int i = 0; |
| while (i<100) { |
| i++; |
| if (i == 30) { |
| break; |
| } |
| if (i % 10 == 0) { |
| continue; |
| } |
| System.out.println(i); |
| } |
| } |
| } |
六、练习
打印一个三角形
| public class Demo18 { |
| public static void main(String[] args) { |
| |
| for (int i=1;i<=5;i++){ |
| |
| for (int j=5;j>=i;j--){ |
| System.out.print(" "); |
| } |
| |
| for (int k=1;k<=i;k++){ |
| System.out.print("*"); |
| } |
| |
| for (int m=2;m<=i;m++){ |
| System.out.print("*"); |
| } |
| System.out.println(); |
| } |
| } |
| } |
| * |
| *** |
| ***** |
| ******* |
| ********* |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· winform 绘制太阳,地球,月球 运作规律
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人