作为程序设计的核心骨架,Java流程控制通过三大结构搭建代码执行框架:顺序确保基础流程,选择实现业务分支,循环处理批量操作,共同构建出稳定可靠且易于维护的软件系统。
1.用户交互Scanner
1.1 next()
- 一定要读到有效字符后才可以结束输入
- 对输入有效字符之前遇到的空白,next()方法会自动将其去掉
- 只有输入有效字符后才将其后面输入的空白作为分隔符或者结束符
- next()不能得到带有空格的字符串
package com.snmwyl.scanner;
import java.util.Scanner;
public class Demo1 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);//创建一个扫描器对象,用于接收键盘数据
System.out.println("使用next方式接收:");
if(scanner.hasNext()) {//判断用户有没有输入字符串
String str = scanner.next();//使用next方式接收,程序会等待用户输入(hello world)
System.out.println("输出的内容为:" + str);//输出的内容为:hello
}
scanner.close();//防止属于IO流的类一直占用资源
}
}
1.2 nextLine()
package com.snmwyl.scanner;
import java.util.Scanner;
public class Demo2 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);//创建一个扫描器对象,用于接收键盘数据
System.out.println("使用nextLine方式接收:");
if(scanner.hasNextLine()){//判断用户有没有输入字符串
String str = scanner.nextLine();//使用nextLine方式接收,程序会等待用户输入)(hello world)
System.out.println("输入的内容为:"+str);//输入的内容为:hello world
}
scanner.close();//防止属于IO流的类一直占用资源
}
}
1.3其他用法
package com.snmwyl.scanner;
import java.util.Scanner;
public class Demo3 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int i = 0;
float f = 0.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("输入的不是小数数据!");
}
scanner.close();
}
}
1.4 例子
package com.snmwyl.scanner;
import java.util.Scanner;
public class Demo4 {
public static void main(String[] args) {
//输入多个数,求和与平均值,每输入一个数字用回车确认,通过输入非数字结束输入并输出结果
Scanner scanner = new Scanner(System.in);
double sum = 0; //和
int n = 0; //个数
System.out.println("请输入多个数: ");
while(scanner.hasNextDouble()){
sum += scanner.nextDouble();
n ++;
}
double avg = sum / n;
System.out.println("和为:" + sum);
System.out.println("平均数为:" + avg);
scanner.close();
}
}
2.顺序结构
package com.snmwyl.struct;
public class Sequence {
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");
System.out.println("hello6");
}
}
3.选择结构
3.1 if单选择结构
if(布尔表达式){
statement
}
package com.snmwyl.struct;
import java.util.Scanner;
public class IfDemo1 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入内容: ");
String s = scanner.nextLine();
if(s.equals("Hello")) {
System.out.println(s);
}
System.out.println("End");
scanner.close();
}
}
3.2 if双选择结构
if(布尔表达式){
statement1
}else{
statement2
}
package com.snmwyl.struct;
import java.util.Scanner;
public class IfDemo2 {
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("不及格");
}
scanner.close();
}
}
3.3 if多选择结构
if(布尔表达式1){
statement1
}else if(布尔表达式2){
statement2
}else{
statement3
}
package com.snmwyl.struct;
import java.util.Scanner;
public class IfDemo3 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入成绩: ");
int score = scanner.nextInt();
if(score < 0 || score > 100) {
System.out.println("输入不合法");
}else if(score >= 90 ) {
System.out.println("优秀");
}else if(score >= 80) {
System.out.println("良好");
}else if(score >= 60) {
System.out.println("及格");
}else{
System.out.println("不及格");
}
scanner.close();
}
}
3.4 嵌套的if结构
if(布尔表达式1){
if(布尔表达式2){
statement
}
}
package com.snmwyl.struct;
import java.util.Scanner;
public class IfDemo4 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入成绩: ");
int score = scanner.nextInt();
if (score >= 60) {
if(score > 100){
System.out.println("输入不合法");
}else if (score >= 90) {
System.out.println("优秀");
}else if (score >= 80) {
System.out.println("良好");
}else{
System.out.println("及格");
}
}else{
if(score < 0){
System.out.println("输入不合法");
}else{
System.out.println("不及格");
}
}
scanner.close();
}
}
3.5 switch多选择结构
//switch case 语句判断一个变量与一系列值中某个值是否相等,每个值称为一个分支
switch(expression){
case value1:
statement1
break; //可选
case value2:
statement2
break;
default: //可选
statement3
}
package com.snmwyl.struct;
import java.util.Scanner;
public class SwitchDemo1 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入成绩等级(大写): ");
char grade = scanner.next().charAt(0);
//!!!不加break,会出现case穿透现象!!!
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;
default:
System.out.println("未知等级");
break;
}
scanner.close();
}
}
//JDK7的新特性,表达式可以是字符串
//字符的本质还是数字
//java---->class(字节码文件)---->反编译(IDEA)
package com.snmwyl.struct;
import java.util.Scanner;
public class SwitchDemo2 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入一个字符串: ");
String s = scanner.nextLine();
switch (s) {
case "你":
System.out.println("好");
break;
case "早上":
System.out.println("好");
break;
default:
System.out.println("未成功匹配");
}
scanner.close();
}
}
4.循环结构
4.1 while循环
//先判断后执行
while(布尔表达式){
statement
}
package com.snmwyl.struct;
public class WhileDemo1 {
public static void main(String[] args) {
//输出1到100
int i = 1;
while (i <= 100) {
System.out.println(i);
i++;
}
}
}
4.2 do...while...循环
//先执行后判断
//保证循环体至少被执行一次
do{
statement
}while(布尔表达式)
package com.snmwyl.struct;
public class WhileDemo3 {
public static void main(String[] args) {
//计算1+2+...+99+100=?
int i = 1;
int sum = 0;
do {
sum += i;
i++;
}while (i <= 100);
System.out.println(sum); //5050
}
}
4.3 for循环
//for循环语句是最有效、最灵活的循环结构
//for循环语句的次数是在执行前就确定的
//快捷方式:100.for---->
for(初始化;布尔表达式;更新){
statement
}
//死循环
for(;;){
statement
}
package com.snmwyl.struct;
public class ForDemo1 {
public static void main(String[] args) {
int i = 0; //初始化条件
while(i <= 100){ //条件判断
System.out.println(i); //循环体
i += 2; //迭代
}
System.out.println("while循环结束!");
for(int j = 0; j <= 100; j += 2){
System.out.println(j);
}
System.out.println("for循环结束!");
}
}
for循环练习
package com.snmwyl.struct;
public class ForDemo2 {
public static void main(String[] args) {
//练习1:计算0到100之间的奇数和偶数的和
int oddSum = 0;
int evenSum = 0;
for(int i = 0; i <= 100; i++){
if(i % 2 == 0){
evenSum += i;
}else{
oddSum += i;
}
}
System.out.println("奇数的和为:" + oddSum); //2500
System.out.println("偶数的和为:" + evenSum); //2550
}
}
package com.snmwyl.struct;
public class ForDemo3 {
public static void main(String[] args) {
//练习2:用while或for循环输出0~1000能被5整除的数,并且每行输出三个
for(int i = 0; i <= 1000; i++){
if(i % 5 == 0){
System.out.print(i + "\t"); //print打印不换行
}
if(i % 15 == 10){
System.out.println(); //println打印换行
}
}
System.out.println("--------------------");
int j = 0;
while(j <= 1000){
if(j % 5 == 0){
System.out.print(j + "\t");
}
if(j % 15 == 10){
System.out.println();
}
j++;
}
}
}
package com.snmwyl.struct;
public class ForDemo4 {
public static void main(String[] args) {
//练习3:打印九九乘法表
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.4 增强for循环
for(声明语句:表达式){
statement
}
package com.snmwyl.struct;
public class ForDemo5 {
public static void main(String[] args) {
//定义了一个数组
int[] numbers = {10,20,30,40,50};
//遍历数组的元素
for(int n : numbers) {
System.out.println(n);
}
System.out.println("--------------------");
for (int i = 0; i < 5; i++) {
System.out.println(numbers[i]);
}
}
}
5.break & continue
package com.snmwyl.struct;
public class BreakDemo {
public static void main(String[] args) {
int i = 1;
while (i <= 100) {
System.out.println(i);
if(i == 30){
break; //强制退出循环
}
i++;
}
System.out.println("Hello World!");
}
}
package com.snmwyl.struct;
public class ContinueDemo {
public static void main(String[] args) {
int i = 0;
while (i < 100) {
i++;
if(i % 10 == 0){
System.out.println();
continue; //跳出某次循环
}
System.out.print(i+"\t");
}
System.out.println();
System.out.print("Hello World!");
}
}
带标签的continue (goto关键字)
package com.snmwyl.struct;
public class LabelDemo {
public static void main(String[] args) {
//找出101~150之间的质数
//质数是指除了1和它本身以外不再有其他因数的大于1的自然数
outer:for (int i = 101; i < 150; i++) {
for (int j = 2; j < i/2; j++) {
if(i % j == 0){
continue outer;
}
}
System.out.print(i+"\t");
}
}
}
6.练习
package com.snmwyl.struct;
public class TestDemo {
public static void main(String[] args) {
//打印三角形
for (int i = 0; i < 5; i++) {
for(int j = 5; j > i; j--){
System.out.print(" ");
}
for(int j = 0; j < 2 * i + 1; j++){
System.out.print("*");
}
System.out.println();
}
}
}