笔记整理:Java流程控制–狂神说Java
1、用户交互Scanner
-
之前学习的基本语法中我们并没有实现程序和re你的交互,但是Java给我们提供了这样一个工具,我们可以获取用户输入。java.util,Scanner是Java5的新特征,我们可以通过Scanner类来获取用户的输入。
-
基本语法:
Scanner myCan = new Scanner(System.in);
-
通过Scanner类的next()与nextLine()方法获取输入的字符串,在读之前,我们一般用hasNext()与hasNextLine()判断是否还有输入的数据。
public class Demo2 {
public static void main(String[] args){
//创建一个扫描对象,用于接收键盘数据
Scanner scanner = new Scanner(System.in);
System.out.println("使用next()方式接收");
//判断用户有没有输入字符串
if (scanner.hasNext()){
//使用next()方式接收
String str = scanner.next();
//使用nextLine()方式接收
// String str = scanner.nextLine();
System.out.println("输出的内容为:" + str);
}
//凡是属于IO流的类如果不关闭会一直占用资源,要养成用完就关掉
scanner.close();
}
}
next():
- 一定要读到有效字符后才可以结束输出
- 对输入有效字符之前遇到空格,next() 方法会将其自动去掉
- 只有输入有效字符后才将其后输入的空格作为分隔符或者结束符
- next() 不能得到带有空格的字符串
nextLine()
- 可以enter为结束符,也就是说nextLine()方法返回的是输入回车之前的所有字符
- 可以获得空格
import java.sql.SQLOutput;
import java.util.Scanner;
/**
* @author: QinHandan
* @date: 2022/5/19
*判断输入的是整数还是小数
*/
public class Demo2 {
public static void main(String[] args){
//创建一个扫描对象,用于接收键盘数据
Scanner scanner = new Scanner(System.in);
int i = 0;
float f = 1.0f;
System.out.println("请输入整数数据:");
//判断用户有没有输入字符串
if (scanner.hasNextInt()){
i = scanner.nextInt();
System.out.println("整数数据:" + i);
}else {
System.out.println("不是整数数据");
}
System.out.println("请输入小数数据:");
if (scanner.hasNextFloat()){
f = scanner.nextFloat();
System.out.println("小数数据:" + f);
}else {
System.out.println("不是小数数据");
}
//凡是属于IO流的类如果不关闭会一直占用资源,要养成用完就关掉
scanner.close();
}
}
import java.sql.SQLOutput;
import java.util.Scanner;
/**
* @author: QinHandan
* @date: 2022/5/19
*/
public class Demo2 {
public static void main(String[] args){
//创建一个扫描对象,用于接收键盘数据
//输入多个数据,求其总和 平均值 Neo输入一个数组用回车确认,通过输入非数字来结束输入并输出执行结果
Scanner scanner = new Scanner(System.in);
double sun = 0;//总和
int num = 0;//计算输入了多少个数字
System.out.println("请输入:");
while (scanner.hasNextDouble()) {
double x = scanner.nextDouble();
sun += x;
num++;
System.out.println("你输入了第" + num + "个数,输出的结果为sun:" + sun);
}
System.out.println(num +"个数的和为:" + sun);
System.out.println(num + "个数的平均值为:" + (sun/num));
scanner.close();
}
}
2、顺序结构
- Java的基本结构就是顺序结构,除非特别指明,否则就按照顺序一句一句执行
- 顺序与语法之间,框与框之间是按上到下的顺序进行的,它是由若干个依次执行的处理步骤组成的, 它是任何一个算法都离不开的一种基本算法结构
package com.qhd.struct;
public class ShunXuDemo {
public static void main(String[] args) {
System.out.println("hello1");
System.out.println("hello2");
System.out.println("hello3");
System.out.println("hello4");
System.out.println("hello5");
}
}
3、选择结构
- if单选择结构
- if双选择结构
- if多选择结构
- 嵌套的if结构
- switch多选择结构
-
if单选择结构
我们很多时候需要判断一个东西是否可行,然后才去执行,这样一个过程在程序中用if语句来表示
if (布尔表达式) { //如果布尔表达式为true将执行语句 } public class If { public static void main(String[] args) { int a = 10; int b =15; if (a < b){ System.out.println("a>b"); } } }
-
if双选择结构
那现在有一个需求,公司要收购一个软件,成功了,支付每人100万元,失败了,自己找人开发。这样的需求用一个if就搞不定了,我们需要有两个判断,需要选择一个双选择结构,所以就有了if-else结构 语法: if (布尔表达式) { //如果布尔表达式为true将执行语句 }else { //如果布尔表达式为false将执行语句 } public class If{ public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("请输入成绩:"); int score = scanner.nextInt(); if (score >= 60) { System.out.println("成绩及格"); } else { System.out.println("成绩不及格"); } } }
-
if多选择结构
if(布尔表达式 1){ //如果布尔表达1式的值为true }else if(布尔表达式 2){ //如果布尔表达2式的值为true }else if(布尔表达式 3){ //如果布尔表达3式的值为true }else{ //如果布尔表达式的值都不为true } import java.util.Scanner; /** * @author: QinHandan * @date: 2022/5/19 */ public class If{ public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("请输入成绩:"); int score = scanner.nextInt(); if (score == 100) { System.out.println("满分"); } 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("不及格"); }else{ System.out.println("输入的成绩有误"); } scanner.close(); } }
-
嵌套的if结构
-
使用嵌套的if……else语句也是合法的,也就是说你可以在另一个if或者else if语句中使用if或者else if语句。你可以像if语句一样嵌套else if……else。
if(布尔表达式 1){ //如果布尔表达式1的值为true执行代码 if(布尔表达式2){ //如果布尔表达式2的值为true执行代码 } }
-
-
Switch多选择结构
- 多选择结构还有一个实现方法就是switch case语句
- switch case语句判断一个变量与一系列值中某个值是否相等,每个值成称为一个分支
- switch语句中的变量类型可以是:
byte、short、int或者char
从Java SE 7开始
switch支持字符串String类型了 - 同时case标签必须为字符串常量或者字面量
import java.util.Scanner; /** * @author: QinHandan * @date: 2022/5/19 */ public class If{ public static void main(String[] args) { Scanner scanner = new Scanner(System.in); char grade = 'C'; switch (grade) { case 'A': System.out.println("优秀"); break;//可选 case 'B': System.out.println("良好"); break; case 'C': System.out.println("及格"); break; case 'D': System.out.println("再接再厉"); break; case 'E': System.out.println("挂科"); break; default: System.out.println("未知等级"); break; } } }
4、循环结构
- while循环
- do…while循环
- for循环
- 在java5中引入了一种主要用于数组的增强型for循环
-
while循环
while循环是最基本的循环,它的结构为:
while(布尔表达式){ //循环表达式 }
- 只要布尔表达式为true,循环就会一直执行下去
- 我们大多数情况是会让循环停下来的,我们需要让一个表达式失效的方式来结束循环
- 少部分情况下需要循环一直执行,比如服务器请求响应监听等
- 循环条件一直为true就会造成无限循环【死循环】,我们正常的业务编程中应该尽量避免死循环。会影响程序性能或造成程序卡死崩溃!
- 思考:计算1+2+3+……+100=?
public class While { public static void main(String[] args) { int i = 0; int sum = 0; while(i <= 100){ sum += i; i++; } System.out.println(sum); } }
-
do…while循环
对于while语句而言,如果不满足条件,则不能进入循环。但是有时我们需要即使不满足条件,也至少执行一次
do…while循环和while信号相似,不同的是do…while循环至少执行一次
while是先判断后执行,do…while先执行后判断
/** * @author: QinHandan * @date: 2022/5/19 */ public class While { public static void main(String[] args) { int i = 0; while (i < 0){ System.out.println("i" + 1); i++; } System.out.println("====="); do { i++; System.out.println(i); } while(i < 0); } }
-
for循环
-
虽然所有循环结构都可以用while或者do……while表示,但Java提供了另一种语句——for循环,使一些循环结构变得更加简单
-
for循环语句支持迭代的一种通用结构,最有效、最灵活的循环结构
-
for循环执行的次数是在执行前确定的。语法格式如下:
for(初始化;布尔表达式;更新){ //代码语句 }
//计算0到100之间所有奇数偶数的和 public class While { public static void main(String[] args) { int oddSum = 0; int eveSum = 0; for (int i = 0; i < 100; i++) { if (i % 2 == 0) { oddSum += i; }else { eveSum += i; } } System.out.println("oddSum偶数和:" + oddSum); System.out.println("eveSum奇数和:" + eveSum); } }
// 用while或for循环输出1~1000之间能被5整除的数,并且每行输出3个 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.println(); } } } } public class While { public static void main(String[] args) { int i = 1; while (i <= 1000) { if (i % 5 == 0) { System.out.print(i + "\t"); if (i % (5*3) == 0){ System.out.println(); } } i++; } } }
//打印九九乘法表 //1.先打印第一列,这个大家应该都会 //2.我们把固定的1再用一个循环包起来 //3.去掉重复项,j<=i //4.调整样式 public class ForDemo { 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(); } } }
-
5、break&continue
- break在任何循环语句的主体部分,均可用break控制循环的流程。break用于强行退出循环,不执行循环中剩余的语句(break语句也在switch语句中使用
- continue语句在循环语句中,用于终止某次循环过程,即跳出循环体中尚未执行的语句,接着进行下一次是否执行循环的判定。
break
/**
* @author: QinHandan
* @date: 2022/5/20
*/
public class Break {
public static void main(String[] args){
int i = 0;
while (i < 100) {
i++;
System.out.println(i);
if (i == 20){
break;
}
}
System.out.println("break");
}
}
continue
public class ContinueDemo {
public static void main(String[] args) {
int i = 0;
while(i<100){
i++;
if(i==10){
System.out.println("==================");
continue;
}
System.out.println(i);
}
//break在任何语句中的主体部分,均可用吧break控制循环的流程
//break用于强行退出循环,不执行循环中剩余的语句。(break语句也在switch语句中使用);
//continue语句用在循环体语句中,用于终止某次循环过程,即跳过循环体中尚未执行的语句,接着进行下一次是否执行循环的判断
}
}
打印三角形
public class Test {
public static void main(String[] args) {
//打印三角形 5行
for (int i = 0; i < 5; i++) {
for (int j = 5; j >= i ; j--){
System.out.print(" ");
}
for (int j = 1; j <= i ; j++) {
System.out.print("*");
}
for(int j = 1; j < i; j++){
System.out.print("*");
}
System.out.println("");
}
}
}