JavaSE-流程控制
Scanner 包装类
Scanner 类的基本使用
- 创建实例对象
public class Demo{
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
}
}
- 判断是否输入
public class Demo{
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
System.out.println(scanner.hasNext())
System.out.println(scanner.hasNextLine())
}
}
- 获取输入
public class Demo{
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
System.out.println(scanner.next()) // 遇到空格就会停止
System.out.println(scanner.nextLine()) // 直接输出整一行,可以获取空白
System.out.println(scanner.nextInt()) // ...输出指定类型的输入
}
}
- 关闭IO 流
public class Demo{
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
scanner.close();
}
}
顺序结构
- 说明:
语句与语句之间,框与框之间按从上到下的顺序进行的,它是由若干个依次执行的处理步骤组成的,顺序结构是任何一个算法的离不开的一种基本算法结构。
选择结构
if(){}else if(){}else{}
- if 语句
public class Main{
public static void main(String[] args){
int a = 10;
if(a>=1){
System.out.println("这个数大于等于1")
}
}
}
- if - else 语句
public class Main{
public static void main(String[] args){
int a = 0;
if(a>=1){
System.out.println("这个数大于等于1");
}else{
System.out.println("这个数小于1");
}
}
}
- if - else - if 语句
public class Main{
public static void main(String[] args){
int a = 0;
if(a>=1){
System.out.println("这个数大于等于1");
}else if(a==0){
System.out.println("这个数等于0");
}else{
System.out.println("这个数小于1");
}
}
}
- 嵌套 if 语句
switch([表达式]){case:…break;}
- 表达式类型
- int、byte、short、char、String、枚举类。
- break 跳过中止执行
public class Main{
public static void main(String[] args){
int a = 10;
switch(a){
case 10:
System.out.println("a 为 10");
break;
}
}
}
循环结构
while(expression){expressionBody…}
// 循环累加 [0-100]
public class Main{
public static void main(String[] args){
int sum=0;
int i=1;
while(i<=100){
sum+=i;
i++;
}
System.out.println(sum);
}
}
public class Main{
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int com = new Random().nextInt(101);
System.out.println(com);
int num = scanner.nextInt();
while (num != com) {
if (num > com) {
System.out.print("too high");
} else {
System.out.print("too low");
}
num = scanner.nextInt();
}
System.out.print("输入正确:");
}
}
转换成 for:
public class Main{
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int com = new Random().nextInt(101);
System.out.println(com);
for (int num = scanner.nextInt(); num != com; num = scanner.nextInt()) {
if (num > com) {
System.out.print("too high");
} else {
System.out.print("too low");
}
System.out.print("");
}
System.out.print("输入正确:");
}
}
无限循环:
public class Main{
public static void main(String[] args) {
/*
1.输入密码 5次输入错误则退出 666666
2.主界面
3.查询余额
4.取钱(钱充足)
5.存钱(整钱100)
*/
Scanner scanner =new Scanner(System.in);
int errorCount=0;
while(true){
System.out.print("请输入密码:");
int pwd=scanner.nextInt();
if(pwd!=666666){
errorCount++; //5
if(errorCount==5){
System.out.println("账号已被冻结!");
return;
}
System.out.println("密码错误,请重新输入!您还有"+(5-errorCount)+"次输入机会");
}else{
break;
}
}
//主界面
int summury=0;//总金额
while(true){
System.out.println("===========欢迎使用ATM自动存取款机===========");
System.out.println("1.存钱");
System.out.println("2.取钱");
System.out.println("3.查询余额");
System.out.println("0.退出");
System.out.print("请选择办理的业务:");
int choice=scanner.nextInt();
switch(choice){
case 1:
System.out.print("请输入存款金额:");
int inmoney=scanner.nextInt();
summury+=inmoney;
System.out.println("存钱完毕!");
break;
case 2:
System.out.println("你的余额为"+summury+"元");
System.out.print("请输入取款金额:");
int outmoney=scanner.nextInt();
if(outmoney>summury){
System.out.println("您的余额不足!");
}else{
summury-=outmoney;
System.out.println("取款完毕!");
}
break;
case 3:
System.out.println("你的余额为"+summury+"元");
break;
case 0:
System.out.println("谢谢使用,请收好您的卡片!");
return;
}
}
}
}
do{expressionBody…}while(expression)
public class Main {
public static void main(String[] args) {
int i = 1;
do {
System.out.println(i);
i++;
} while (i <= 10);
}
}
无限循环:
public class Main {
public static void main(String[] args) {
do {
System.out.println("infinitive do while loop");
} while (true);
}
}
while 与 dowhile循环的区别在于while先判断后执行,而dowhile是先执行后判断,也就是说dowhile永远都会执一次。
for(声明语句(初始化);循环范围(布尔表达式);变量更迭(更新)){expressionBody…}
- 简单 for 循环
public class Main {
public static void main(String[] args) {
for(int i=1;i<=10;i++){
System.out.println(i);
}
}
}
- forEach 循环(增强for 循环)
常用于数组相关遍历。
public class Main {
public static void main(String[] args) {
int arr[] = { 12, 23, 44, 56, 78 };
for (int i : arr) {
System.out.println(i);
}
}
}
- 标记 for 循环
为此,在for循环之前使用标签。它是有用的,如果在嵌套for循环中,可以使用break/continue指定循环。
通常,break和continue关键字断开/继续最内循环。
public class Main {
public static void main(String[] args) {
aa: for (int i = 1; i <= 3; i++) {
bb: for (int j = 1; j <= 3; j++) {
if (i == 2 && j == 2) {
break aa;
}
System.out.println(i + " " + j);
}
}
}
}
输出:
1 1
1 2
1 3
2 1
如果使用break bb;它将打断内循环,这是任何循环的默认行为。
public class Main {
public static void main(String[] args) {
aa: for (int i = 1; i <= 3; i++) {
bb: for (int j = 1; j <= 3; j++) {
if (i == 2 && j == 2) {
break bb;
}
System.out.println(i + " " + j);
}
}
}
}
输出:
1 1
1 2
1 3
2 1
3 1
3 2
3 3
- 无限 for 循环
public class Main {
public static void main(String[] args) {
for (;;) {
System.out.println("infinitive loop");
}
}
}
输出:
infinitive loop
infinitive loop
infinitive loop
infinitive loop
infinitive loop
ctrl+c
for 循环打印乘法表
public class Demo{
public static void main(String[] args){
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()
}
}
}
break continue
-
break:跳出循环,中止循环。
-
continue:中止某次循环的过程,接着进行下一次循环判定。
-
goto:实际上就是标签型for 循环。