JavaSE:Java流程控制
文章目录
前言
即使再小的帆也能远航
一、用户交互Scanner
- Java5的新特征:java.util.Scanner;
- 可以通过Scanner类这个工具类来获取用户的输入
- 基本语法
Scanner s = new Scanner(System.in);
- 通过Scanner类的 next() 与 nextLine() 方法获取输入的字符串,在读取前我们一般需要使用 hasNext() 与 hasNextLine() 判断是否还有输入的数据
- next():
- 以Enter为结束符,输出有效字符的第一个空格前的有效字符,不含空格
- nextLine():
- 以Enter为结束符,输出回车之前的所有字符,可以含空格
import java.util.Scanner;
public class Demo01 {
public static void main(String[] args) {
//创建一个扫描器对象,用于接收键盘数据
Scanner scanner = new Scanner(System.in); //Alt+回车 快速补充代码
System.out.println("使用Next方式接收:");
//判断用户有没有输入字符串
if(scanner.hasNext()){
//使用Next方式接收
String str = scanner.next(); //程序会等待用户输入完毕
System.out.println("输出的内容为"+str);
}
//凡是属于IO流的类如果不关闭会一直占用资源,要养成好习惯用完就关
scanner.close();
}
}
import java.util.Scanner;
public class Demo02 {
public static void main(String[] args) {
//从键盘接收数据
Scanner scanner = new Scanner(System.in);
System.out.println("使用NextLine方式接收:");
//判断是否还有输入
if(scanner.hasNextLine()){
String str = scanner.nextLine();
System.out.println("输出的内容为"+str);
}
scanner.close();
}
}
import java.util.Scanner;
public class Demo03 {
public static void main(String[] args) {
//从键盘接收数据
Scanner scanner = new Scanner(System.in);
System.out.println("请输入数据:");
String str = scanner.nextLine();
System.out.println("输出的内容为:"+str);
scanner.close();
}
}
二、Scanner进阶使用
import java.util.Scanner;
public class Demo04 {
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("输入的不是整数数据");
}
scanner.nextLine(); //没有这一行代码的话,会出现问题,如第一个走else就会直接第二个也走else
System.out.println("请输入小数:"); //逻辑bug,输入整数也会显示为小数
if(scanner.hasNextFloat()){
f = scanner.nextFloat();
System.out.println("小数数据:"+f);
}else{
System.out.println("输入的不是小数数据");
}
scanner.close();
}
}
import java.util.Scanner;
public class Demo05 {
public static void main(String[] args) {
//输入多个数字,并求其总和与平均数,每输入一个数字用回车确认,通过输入非数字来结束输入,并输出执行结果
Scanner scanner = new Scanner(System.in);
//和
double sum = 0;
//计数项
int m = 0;
//平均数
double avg = 0;
System.out.println("请输入数字:");
//通过循环判断是否还有输入,并在里面对每一次进行求和统计
while(scanner.hasNextDouble()){
double x = scanner.nextDouble();
m++;
sum+=x;
avg=sum/m;
}
System.out.println("====结果====");
System.out.println("计数项:"+m);
System.out.println("和:"+sum);
System.out.println("平均数:"+avg);
scanner.close();
}
}
三、顺序结构
- Java的基本结构
- 除非特别指明,否则按顺序从上到下一句一句执行
- 是最简单的算法结构
- 它是任何一个算法都离不开的一种基本算法结构
public class ShunXuDemo {
public static void main(String[] args) {
System.out.println(1);
System.out.println(2);
System.out.println(3);
}
}
选择结构中,switch只能判断确切值,if可以判断范围
四、If选择结构
boolean值判断
true 执行语句
false 跳过语句
1.if单选择结构
import java.util.Scanner;
public class IfDemo01 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入内容:");
String s = scanner.nextLine();
//equals:判断字符串是否相等(equals判断内容,==判断对象存储地址)
if(s.equals("Hello")){
System.out.println(s);
}
scanner.close();
}
}
2.if双选择结构
import java.util.Scanner;
public class IfDemo02 {
public static void main(String[] args) {
//考试分数大等于60就是及格,小于60分就不及格
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.if多选择结构
import java.util.Scanner;
public class IfDemo03 {
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<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("不及格");
}else {
System.out.println("成绩不合法");
}
scanner.close();
}
}
【拓展】
import java.util.Scanner;
public class IfDemo04 {
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){
System.out.println("A级");
}else if (score>=80){
System.out.println("B级");
}else if (score>=70){
System.out.println("C级");
}else if (score>=60){
System.out.println("D级");
}else if (score>=0){
System.out.println("不及格");
}else {
System.out.println("成绩不合法");
}
scanner.close();
}
}
少了and条件(&&),程序会默认按顺序结构来执行选择语句,按顺序进行判断条件(可减少代码执行复杂度,但有and条件会增加可读性)
4.嵌套的if结构
少套娃,会增加代码的时间复杂度
五、Switch选择结构
public class SwitchDemo01 {
public static void main(String[] args) {
char grade = 'C';
//case穿透,不写break会发生case穿透
//switch匹配一个具体的值
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: //可无default项
System.out.println("未知等级");
break;
}
}
}
public class SwitchDemo02 {
public static void main(String[] args) {
String name = "恰恰";
//JDK7的新特性,表达式结果可以是字符串
//字符的本质还是数字
//反编译 java--class(字节码文件)--反编译(IDEA)
switch (name){
case "恰恰":
System.out.println("今天吃的兔兔");
break;
case "老板":
System.out.println("兔兔保安");
break;
default:
System.out.println("哒哒哒");
break;
}
}
}
【反编译】
(看源码)
打开项目结构
复制项目输出路径
在文件管理器中查找到对应文件夹下的.class文件并复制
再右键IDEA中的包
点击“在explorer中显示”,显示对应文件夹下的.java文件
粘贴.class文件
打开IDEA则出现.class文件并进行查看
即反编译成功
六、While循环结构
- while是最基本的循环
- 循环应有结束条件,否则会死循环,应尽量避免死循环
public class WhileDemo01 {
public static void main(String[] args) {
//输出1~100
int i = 0;
while(i<100){
i++;
System.out.println(i);
}
}
}
public class WhileDemo02 {
public static void main(String[] args) {
//死循环
while(true){
//等待客户端连接
//定时检查
//服务器的请求响应监听
//......
}
}
}
public class WhileDemo03 {
public static void main(String[] args) {
//计算1+2+...+100=?
//高斯的故事
int i = 0;
int sum = 0;
while(i<100){
i++;
sum+=i;
}
System.out.println(sum);
}
}
七、DoWhile循环
- while是先判断后执行,do while是先执行后判断
- 至少执行一次
public class DoWhileDemo01 {
public static void main(String[] args) {
int i = 0;
int sum = 0;
do {
i++;
sum+=i;
}while (i<100);
System.out.println(sum);
}
}
public class DoWhileDemo02 {
public static void main(String[] args) {
int a = 0;
while(a<0){ //先判断后执行
System.out.println(a);
a++;
}
System.out.println("========");
do { //先执行后判断
System.out.println(a); //a=0
a++;
}while(a<0);
}
}
八、For循环
- for循环是支持迭代的一种通用结构,是最有效、最灵活的循环结构
- for循环执行的次数是在执行前就确定的
for(初始化;布尔表达式;更新){
//代码语句
}
public class ForDemo01 {
public static void main(String[] args) {
int a = 1;
while(a<100){
System.out.println(a);
a+=2;//迭代
}
System.out.println("while循环结束!");
//for(初始化;布尔表达式;更新)
for (int i = 1;i<=100;i++){
System.out.println(i);
}
System.out.println("for循环结束!");
//100.for 回车即可快速补充代码块 只有在main方法中
for (int i = 0; i < 100; i++) {
}
//死循环
for ( ; ; ) {
}
}
}
public class ForDemo02 {
public static void main(String[] args) {
//练习1:计算0到100之间的奇数和 与 偶数和
//odd 奇数 不能被2整除的数
int oddSum = 0;
int evenSum = 0;
for (int i = 0; i < 100; i++) {
if(i%2!=0){
oddSum+=i;
}else{
evenSum+=i;
}
}
System.out.println("奇数和:"+oddSum);
System.out.println("偶数和:"+evenSum);
}
}
public class ForDemo03 {
public static void main(String[] args) {
//练习2:用while或for循环输出1-1000之间能被5整除的数,并且每行输出3个
for (int i = 1; i < 1000; i++) {
if (i%5==0){
System.out.print(i+" ");
}
if (i%(5*3)==0){ //每3个换行
System.out.println();
//System.out.print("\n");
}
}
//println 输出完会换行
//print 输出完不会换行
// \n 用于换行
}
}
九、打印九九乘法表
/*
1*1=1
2*1=2 2*2=4
3*1=3 3*2=6 3*3=9
4*1=4 4*2=8 4*3=12 4*4=16
5*1=5 5*2=10 5*3=15 5*4=20 5*5=25
6*1=6 6*2=12 6*3=18 6*4=24 6*5=30 6*6=36
7*1=7 7*2=14 7*3=21 7*4=28 7*5=35 7*6=42 7*7=49
8*1=8 8*2=16 8*3=24 8*4=32 8*5=40 8*6=48 8*7=56 8*8=64
9*1=9 9*2=18 9*3=27 9*4=36 9*5=45 9*6=54 9*7=63 9*8=72 9*9=81
*/
public class ForDemo04 {
public static void main(String[] args) {
//练习3:打印九九乘法表
//外循环一次,内循环一轮
//外循环 行 ,内循环 列
for (int i = 1; i <= 9; i++) {
for (int j = 1; j <= i; j++) {
System.out.print(i+"*"+j+"="+(i*j)+"\t");
}
System.out.println();
}
}
}
十、增强for循环
- Java5 引入了一种主要用于数组或集合的增强型 for 循环
public class ForDemo05 {
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]);
}
System.out.println("========");
for (int number : numbers) { //等价上面的循环
System.out.println(number);
}
}
}
十一、break、continue、goto
1.break、continue
可理解为 break 辞职 和 continue 请假
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");
}
}
}
2.goto(不推荐使用)
- goto是Java的一个保留字
- 标签是指后面跟一个冒号的标识符,如:label:
- 对Java来说唯一用到标签的地方是在循环语句之前,而在循环之前设置标签的唯一理由是:我们希望在其中嵌套另一个循环,由于break和continue关键字通常只中断充当前循环,但若随标签使用,他们就会中断到存在标签的地方
public class LabelDemo {
public static void main(String[] args) {
//打印101到150之间的所有质数
//质数是指在大于1的自然数中,除了1和它本身以外不再有其他因数的自然数
int count = 0;
outer:for (int i = 101; i < 150; i++) {
for (int j = 2;j<i/2;j++) {
if (i % j == 0) {
continue outer;
}
}
System.out.println(i);
}
}
}
十二、打印三角形及Debug
public class TestDemo {
public static void main(String[] args) {
//打印三角形 5行
for (int i = 1; i <= 5; i++) {
for (int j = 5;j>i;j--){
System.out.print(" ");
}
for (int j = 1;j<=2*i-1;j++){
System.out.print("*");
}
System.out.println();
}
}
}
Debug调试
总结
即使再小的帆也能远航