这里使用了Scanner类来进行交互式输入。
流程就是顺序,判断,循环,一个是执行顺序,一个进行判断,一个是进行多次。在循环中有终止循环break, 停止当次循环continue,在方法中也有return来终止方法的执行。
//判断有没有输入
hasNext()
//以空格为结束符
next()
//判断有没有输入
hasNextLine()
//以回车为结束符
nextLine()
这个是阻塞式输入
判断的时候将数据存入缓存,在next()或者nextLine(),不用再次输入,直接放入数据。
hasNextInt()
从上到下执行。
判断一个范围。
if (condition) {
// block of code to be executed if the condition is true
}
case穿透
匹配一个值
变量类型:byte, short, int, char, String
switch(expression) {
case x:
// code block
break;
case y:
// code block
break;
default:
// code block
}
将class文件直接放在IDEA文件夹即可
while (condition) {
// code block to be executed
}
do {
// code block to be executed
}
while (condition);
至少执行一次循环。
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();
}
快捷键:array.for
for (statement 1: statement 2) {
// code block to be executed
}
可用于数组,集合,列表等枚举
强制退出循环,不执行剩下的语句。
终止当前循环,重新开始执行。
原文:https://www.cnblogs.com/marhuman/p/14722885.html