首页 > 其他 > 详细

控制流程总结

时间:2021-04-30 20:53:00      阅读:27      评论:0      收藏:0      [点我收藏+]

这里使用了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

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

控制流程总结

原文:https://www.cnblogs.com/marhuman/p/14722885.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!