控制流程总结

2021/4/30 18:25:11

本文主要是介绍控制流程总结,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

这里使用了Scanner类来进行交互式输入。

流程就是顺序,判断,循环,一个是执行顺序,一个进行判断,一个是进行多次。在循环中有终止循环break, 停止当次循环continue,在方法中也有return来终止方法的执行。

1.Scanner类

方法

//判断有没有输入
hasNext()
//以空格为结束符
next()
//判断有没有输入    
hasNextLine()
//以回车为结束符
nextLine()    
    
这个是阻塞式输入
判断的时候将数据存入缓存,在next()或者nextLine(),不用再次输入,直接放入数据。
    
 hasNextInt()

2.顺序结构

从上到下执行。

3.选择结构

if

判断一个范围。

  1. 单选
  2. 双选
  3. 多选
  4. 嵌套
if (condition) {
  // block of code to be executed if the condition is true
}

switch

case穿透

匹配一个值

变量类型:byte, short, int, char, String

switch(expression) {
  case x:
    // code block
    break;
  case y:
    // code block
    break;
  default:
    // code block
}

反编译

将class文件直接放在IDEA文件夹即可

4.循环结构

while

while (condition) {
  // code block to be executed
}

do,,,while

do {
  // code block to be executed
}
while (condition);

至少执行一次循环。

for

for (statement 1; statement 2; statement 3) {
  // code block to be executed
}

快捷键:生成100次循环:100.for

九九乘法表

for (int j = 1; j <= 9; j++) {
    for (int i = 1; i <= j; i++) {
        System.out.print(""+i+"X"+j+"="+i*j+'\t');
    }
    System.out.println();
}

增强for循环

快捷键:array.for

for (statement 1: statement 2) {
  // code block to be executed
}

可用于数组,集合,列表等枚举

5.break contionue

break

强制退出循环,不执行剩下的语句。

contionue

终止当前循环,重新开始执行。



这篇关于控制流程总结的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程