流程控制
1.Scanner类
步骤
1.导包
2.创建Scanner对象
Scanner sc = new Scanner(System.in)
3.提示用户输入信息
4.接收用户输入数据
package org.wdit.unit07;
import java.util.Scanner;
/**模拟用户登录
* 1.创建Scanner对象
* 2.提示用户输入用户名
* 3.声明一个变量接收用户输入的用户名
* 4.提示用户输入密码
* 5.声明另一个变量接收用户输入的密码
* 6.控制台输出欢迎信息
*/
public class ScannerTest {
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
System.out.println("请输入用户名:");
String name = sc.next();
System.out.println("请输入密码:");
String password = sc.next();
System.out.println("欢迎"+name+"登录");
}
}
2.顺序结构
执行顺序按照代码书写顺序依次执行
3.选择结构
if选择结构
格式1.if(关系表达式){
语句体
}
格式2.if(关系表达式1){
语句体1
}else{
语句体2(关系表达式2)
}
格式3…if(关系表达式1){
语句体1
}else if(关系表达式2){
语句体2
}
。。。。。
}else{
语句体n;
}
package org.wdit.unit07;
import java.util.Scanner;
public class ifDemo2 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入一个成绩:");
int num = sc.nextInt();
if (num >= 90) {
System.out.println( "优秀");
} else if (num >= 80) {
System.out.println("良好");
}else{
System.out.println("差");
}
}
}
格式4 镶嵌
package org.wdit.unit07;
public class ifDemo3 {
public static void main(String[] args) {
int num = 10;
int num2 = 20;
int num3 = 30;
if(num > num2){
//true
if(num > num3){
//true
System.out.println("最大值"+num);
}else{
System.out.println("最大值"+num3);
}
}else{
//false
if(num2 > num3){
System.out.println("最大值"+num2);
}else{
System.out.println("最大值"+num3);
}
}
}
}
package org.wdit.unit07;
//输入一个数判断它的奇偶性
import java.util.Scanner;
public class ifTest {
//主方法
public static void main(String[] args){
//创建Scanner对象
Scanner sc = new Scanner(System.in);
//提示用户输入数据
System.out.println("请输入一个正整数:");
//声明变量接收用户输入数据
int num = sc.nextInt();
//判断奇偶
if(num % 2 == 0 ) {
//偶数
System.out.println(num + "是一个偶数");
}else{
//基数
System.out.println(num+"是一个基数");
}
System.out.println("程序结束!");
}
}
package org.wdit.unit07;
//.判断季度
import java.util.Scanner;
public class ifTest1 {
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
System.out.println("输一个月份:");
int Quarter = sc.nextInt();
if( Quarter >=3 & Quarter <=5){
System.out.println("春季");
}else if(Quarter>=6 & Quarter <=8){
System.out.println("夏季");
}else if(Quarter>=10 & Quarter <=11){
System.out.println("秋季");
}else if(Quarter<=12 | Quarter <=2){
System.out.println("冬季");
}else{
System.out.println("无效");
}
System.out.println("结束");
}
}
switch选择结构
swicth(表达式){
case 常量1:
语句体1;
break;
case 常量2:
语句体2;
break;
.。。。。。
default:
语句体n
}
package org.wdit.unit07;
import java.util.Scanner;
public class SwitchDemo {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("输入您的名次");
int rank= sc.nextInt();
switch (rank){
case 1:
System.out.println("第一名");
break;
case 2:
System.out.println("第二名");
break;
case 3:
System.out.println("第三名");
break;
default:
System.out.println("第四名");
}
}
}
package org.wdit.unit07;
//判断季度
import java.util.Scanner;
public class SwitchDemo1 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("输入的月份:");
int Month = sc.nextInt();
switch (Month){
case 3:
case 4:
case 5:
System.out.println(Month +"月" + "春季");
break;
case 6:
case 7:
case 8:
System.out.println(Month +"月" +"夏季");
break;
case 9:
case 10:
case 11:
System.out.println(Month +"月" +"秋季");
break;
case 12:
case 1:
case 2:
System.out.println(Month +"月" + "冬季");
break;
default:
System.out.println("无效");
}
}
}
注意事项:
- case后的值不能重复
- break 省略会发生case穿透
- default的位置不影响执行流程,但是如果写在中间,就必须给它加上
break
4.结束条件:
a.break
b.default
c.前提:dafault省略。所有情况都不匹配
循环结构
循环语句可以在满足循环条件的情况下,反复执行某一段代码, 这段被重复执行的代码被称为循环体语句,当反复执行这个循环体时,需要在合适的时候把循环判断条件修改为false,从而结束循环,否则循环将一直执行下去,形成死循环。
循环语句的组成
初始化语句:
一条或者多条语句,这些语句完成一-些初始化操作。
判断条件语句:
这是一个boolean表达式,这个表达式能决定是否执行循环体。
循环体语句:
这个部分是循环体语句,也就是我们要多次做的事情。
控制条件语句:
这个部分在一 -次循环体结束后, 下一 -次循环判断条件执行前执行。通过用于控制循环条件中的变量,使得循环在合适的时候结束。
for循环
for循环结构
格式:
for(初始化条件;条件判断;控制条件语句){
循环体;
}
package org.wdit.unit07;
/**
* for循环结构
* 格式:
* for(初始化条件;条件判断;控制条件语句){
* 循环体;
* }
*/
public class forDemo {
public static void main(String[] args) {
for(int i= 1; i <=100;i++){
System.out.println(i+"好好学习天天向上" );
}
}
}
package org.wdit.unit07;
//算数列表
public class forDemo6 {
public static void main(String[] args){
for(int i=1;i <=9;i++){
for(int j=1;j <=i;j++){
System.out.print(j+"*" +i+"="+(i*j)+"\t");
}
System.out.println();
}
}
}
/**
* 答案
* 1*1=1
* 1*2=2 2*2=4
* 1*3=3 2*3=6 3*3=9
* 1*4=4 2*4=8 3*4=12 4*4=16
* 1*5=5 2*5=10 3*5=15 4*5=20 5*5=25
* 1*6=6 2*6=12 3*6=18 4*6=24 5*6=30 6*6=36
* 1*7=7 2*7=14 3*7=21 4*7=28 5*7=35 6*7=42 7*7=49
* 1*8=8 2*8=16 3*8=24 4*8=32 5*8=40 6*8=48 7*8=56 8*8=64
* 1*9=9 2*9=18 3*9=27 4*9=36 5*9=45 6*9=54 7*9=63 8*9=72 9*9=81
*/
package org.wdit.unit07;
public class forDemo5 {
public static void main(String[] args){
for(int i=1;i <=5;i++){
for(int j=1;j<=i;j++){
System.out.print("*");
}
System.out.println();
}
}
}
/**
* *
* **
* ***
* ****
* *****
*/
package org.wdit.unit07;
//水仙花数
public class forDemo4 {
public static void main(String[] args){
int count = 0;
for(int i=100;i <1000;i++){
int ge = i%10;
int shi = i/10%10;
int bai = i/100;
if(i == (ge*ge*ge+shi*shi*shi+bai*bai*bai)){
System.out.println("水仙花数:"+i);
count++;
}
}
System.out.println("100-999之间共有"+count+"个水仙花数");
}
}
/**
* 水仙花数:153
* 水仙花数:370
* 水仙花数:371
* 水仙花数:407
* 100-999之间共有4个水仙花数
*/
package org.wdit.unit07;
//计算0-100中所有偶数和
public class forDemo3 {
public static void main(String[] args) {
int sum = 0;
for(int i=2;i<=100;i+=2){
sum+=i;
}
System.out.println(sum);
int sum2 = 0;
for(int i1=0;i1<=100;i1++){
if(i1%2==0){
sum2=sum2+i1;
}
}
System.out.println(sum2);
}
}
/**
* 2550
* 2550
*/
while循环
语法:
-
循环初始条件语句;
-
while(判断语句){
-
循环体语句;
-
条件控制语句
-
}
package org.wdit.unit07;
/**while
* 语法:
* 循环初始条件语句;
* while(判断语句){
* 循环体语句;
* 条件控制语句
* }
*/
public class WhileDemo {
public static void main(String[] args){
int i =1;
while(i<100){
System.out.println((i)+"--好好学习天天向上");
}
}
}
do-while循环
语法:
- 初始化条件语句;
- do{
循环体语句; - 控制条件语句;
- }while(条件判断语旬)
package org.wdit.unit07;
/**
* 语法:
*
* 初始化条件语句;
* do{
*循环体语句;
* 控制条件语句;
* }while(条件判断语旬)
* while 与do-while最大的区别: .
*
* while:条件不成立,- -次都不执行
*
* do-while:条件不成立,至少执行一次
*/
public class WhileDome2 {
public static void main(String[] args){
int i = 101;
do{
System.out.println(i+"---好好学习天天向上");
i++;
} while (i<=100);
}
}
while 与do-while最大的区别: .
while:条件不成立,- -次都不执行
do-while:条件不成立,至少执行一次
流程控制语句
break:
用来跳出循环(只能终止单层循环)或switch
如果break想跳出外层循环,需要使用标签
continue:
终止当前次循环,执行下一次循环
return:
返回