一、扫描器
import java.util.Scanner;
public class Text{
publics static void main(String[] args){
//创建一个扫描器
Scanner scanner = new scanner(System.in);
//输入一个数字
int scanner.nextInt();
System.out.println(i);
二、if,switch,do,while,for流程控制语句
if条件控制语句
//if流程控制语句
if(条件){
System.out.println();
} else if(条件){
System.out.println();
}
switch(开关)流程控制语句
- 不能用long,byte,shorte,int可以
switch (变量){
case 1:
System.out.println();
break;
} case 2:
System.out.println();
break;
do语句
- 先执行一下,然后判断,看要不要继续执行
do{
System.out.println();
}while (i > 0);
System.out.println();
while循环语句(再、、、什么的时候)
如果直接写true,死循环
循环条件和退出机制
先判断一下,是不是满足条件,然后执行
while(满足条件){
执行输出;
i--;
}
for语句
for(;条件;){
System.out.println(1);
}
- 有一个变量,进入条件,退出机制。
for(int i = 0 ;i < 10 ; i++){
System.out.println(i);
}
- 创建变量
- 判断是否满足条件
- 加1
- 判断是否满足条件
- 直接执行
- 加一
- 直到不满足条件
练习
三角形
for(int j = 0; j < 10; j++){
for(int i = 1; i <= j; i++){
System.out.print(" *");
}
System.out.pringln();
}
[图片上传失败...(image-77dba0-1603619616571)]
正方形
for(int j = 0; j < 10;j++){
for(int i = 1; i <= 10; i++){
System.out.print(" *");
}
System.out.println();
}
[图片上传失败...(image-d44b71-1603619616571)]
九九表
for(int j = 0; j < 10;j++){
for(int i = 1; i < j; i++){
System.out.print(i+"*"+j+"="+i*j+"");
}
System.out.println();
}
[图片上传失败...(image-158879-1603619616571)]
三、break,contiune关键字
-
break:结束当前循环;
-
contiune:跳过本次循环,继续下一次循环
for (int j = 1; j <= 100;j++){ if(j > 10){ break; } System.out.println(j); } System.out.println("&&&&&&&&"); for (int j = 1;j <= 100;j++){ System.out.println(j); }