Java控制流程
Scanner对象
-
next()
- 读取有效字符后才会结束输入
- 在有效字符前遇到空白,next()方法会自动跳过空白
- 只有输入有效字符后才将其后面输入的空白作为分隔符或结束符
-
nextline()
- 以Enter为结束符,也就是返回所有回车前的所有字符
- 可以获得空白
实例
- next
public static void main(String[] args) {
//创建扫描器Scanner对象 用于接受键盘数据
Scanner scanner = new Scanner(System.in);
//判断用户是否输入字符串
String str=scanner.next();//程序等待用户输入完毕
System.out.println("使用next接收");
System.out.println("输出的内容为:"+str);
//凡是io流用后关闭 否则会占用资源
//io流 有输入和输出
scanner.close();
- nextline
Scanner scanner=new Scanner(System.in);
String str=scanner.nextLine();//就这里改一下就行
System.out.println("使用nextLine接收");
System.out.println("输出的内容为:"+str);
scanner.close();
- 判断用法实例
Scanner scanner = new Scanner(System.in);
System.out.println("使用next接收");
//这里是判断
if (scanner.hasNext()) {
String str = scanner.next();
System.out.println("输出的内容为:"+str);
}
scanner.close();
结构
顺序结构
-
java的算法流程都是从上往下进行的(一行一行的,自己乱写除外)
//运行后会输出1-5 System.out.println(1); System.out.println(2); System.out.println(3); System.out.println(4); System.out.println(5);
条件结构(重点)
If条件语句
- 单选择 if(){}
- 双选择 if(){}else{}
- 多选择 if(){}else if(){}
- 嵌套 if(){ if(){ }}
//语法
/*
单选择
if(布尔表达式){
//true执行
}
------------------------------------
双选择
if(布尔表达式){
//true执行
}else{
//false执行
}
-------------------------------------
多选择
if(布尔表达式1){
//布尔表达式1为true执行
}else if(布尔表达式2){
//布尔表达式2为true执行
}
else{
//false执行
}
------------------------------------------
嵌套
if(布尔表达式1){
//布尔表达式1为true执行
if(布尔表达式2){
//布尔表达式2为true执行
}
}
*/
Swich多选择结构(分支)
- 格式
/*
switch (比较变量){
case 值1:
break;//跳出循环 防止case穿透 (会输出所有结果)
....(可添加多个case 值: break;)
default:
//默认执行
}
*/
- 实例
//优秀等级
char grade = 'A';
switch (grade){
case 'A':
System.out.println("优秀");
break;
case 'B':
System.out.println("良好");
break;
case 'C':
System.out.println("及格");
break;
case 'D':
System.out.println("再接再厉");
break;
case 'E':
System.out.println("挂科");
break;
default:
System.out.println("未知等级");
}
循环结构
-
while 先判断后执行
-
dowhile先执行后判断
-
for(用处较多)
1.快捷输入
100.for 回车自动生成
for (int i = 0; i < 100; i++) { }
/*while(布尔表达式){
//循环内容
}
*/
while(true){
//死循环
}
/*
do{
//会先走一遍
}while(布尔表达式);
*/
/*
初始化;条件判断;迭代(迭代:每次循环执行一次)
for(初始化;布尔表达式;更新){
}
*/
for(;;){
//死循环
}
-
For实例
System.out.println("=============正乘法表==========="); for (int i = 1; i <= 9; i++) { System.out.print("\t");//间隔空隙 for (int a = 1; a <=i; a++) { System.out.print(a+"*"+i+"="+i*a+"\t");//每个式子 } System.out.println();//自动换行 }
-
For玩法
//乘法表 System.out.println("================正乘法表================="); for (int i = 1; i <= 9; i++) { System.out.print("\t");//间隔空隙 for (int a = 1; a <=i; a++) { System.out.print(a+"*"+i+"="+i*a+"\t");//每个式子 } System.out.println();//自动换行 } System.out.println("=============趴地乘法表================"); for (int i = 9; i >= 1; i--) { System.out.print("\t"); for (int a = 9; a>=i; a--) { System.out.print(a+"*"+i+"="+i*a+"\t"); } System.out.println(); } System.out.println("=============倒立乘法表==============="); for (int i = 9; i >= 1; i--) { System.out.print("\t"); for (int a = 1; a<=i; a++) { System.out.print(i+"*"+a+"="+i*a+"\t"); } System.out.println(); } System.out.println("==============平躺乘法表================"); for (int i = 1; i <= 9; i++) { System.out.print("\t"); for (int a = 9; a>=i; a--) { System.out.print(i+"*"+a+"="+i*a+"\t"); } System.out.println(); } }
-
还有很多自己试试吧!
增强For循环
-
格式(目前能理解就行)
for (int 序列变量:数组){ System.out.println(序列变量);//数组中的值 }
int[] num = {10,20,30,40,50};//定义了一个数组
/*
* 上面的数组相当于
* int num1=10;
* int num2=20;
* int num3=30;
* int num4=40;
* int num5=50;
*
* */
for (int i=0;i<5;i++){
System.out.println(num[i]);
/*取出数组里的所有值[这里是是数组里的第n-i跟数字)]
比如:
10 对应 num[0]
20 对应 num[1]
30 对应 num[2]
...以此类推
*/
}
//遍历数组元素(上面的简化版)
for (int x:num){
System.out.println(x);
}
break 和 continue 区别
-
break
停止所有循环
-
continue
跳过某次循环
-
实例测试
int i=0;
while(true){
i++;
if(i==30){
System.out.println("========我跳过了30这个数=========");
continue;//只是不输出30(跳过这一次)
}
else if(i==60){
break;//跳出循环(终止循环)
}
System.out.println(i);
}
goto语句
- 不建议使用
- 麻烦
- 效率低
- 其实就是在for循环前加个代号
- 然后在for内部
- 能够使用外部循环
- continue 代号;
/*质数定义为在大于1的自然数中,除了1和它本身以外不再有其他因数。也可以理解为:这个数与除1之外小于它的数取余不为0,则这个数为质数。
*/
//取100-150内的质数
top:for (int i = 100; i < 150; i++) {
for (int j = 2; j < i ; j++) {
if (i%j==0) {
continue top;//不是质数时执行
}
}
System.out.print(i+" ");
}
-
实例练习玩法
//输出一个三角形 for (int i = 0; i <= 5; i++) { System.out.println("");//字符间隔 for (int j = 5; j >= i; j--) { System.out.print(" ");//把三角形左侧空白用空白补 } for (int j = 1; j <= i; j++) { System.out.print("*");//打印三角形左侧 } for (int j = 1; j < i; j++) { System.out.print("*");//打印右侧 } } /* //视觉化 这 * 是 *** 左 ***** 侧 ******* ********* */ //输出一个大箭头 for (int i = 1; i <= 5; i++) { System.out.println(" ");//字符间隔 for (int j = 5; j >= i; j--) { System.out.print(" ");//把三角形左侧空白用空白补 } for (int j = 1; j <= i; j++) { System.out.print(" .*. ");//打印三角形左侧 } for (int j = 1; j < i; j++) { System.out.print(" .*. ");//打印右侧 } System.out.println(); } for (int i = 0; i < 10; i++) { System.out.print("\n ========"); } /* .*. .*. .*. .*. .*. .*. .*. .*. .*. .*. .*. .*. .*. .*. .*. .*. .*. .*. .*. .*. .*. .*. .*. .*. .*. ======== ======== ======== ======== ======== ======== ======== ======== ======== ======== */