Java基础之流程控制

流程控制

1.Scanner

作用:获取用户的输入

语法:

Scanner scanner = new Scanner(System.in);

Scanner中的方法:

next():用于接收用户输入的数据,且一定要读取到有效字符,遇到空格则结束

import java.util.Scanner;

public class Demo01 {
    public static void main(String[] args) {
        //扫描器对象,用于接收键盘数据
        Scanner scanner = new Scanner(System.in);

        System.out.println("使用next()方式接收:");

        //判断用户有没有输入字符串
        if (scanner.hasNext()){
            String str = scanner.next();
            System.out.println("输出的内容为:"+str);
        }
        //属于IO的类用完要关闭,否则会一直占用资源
        scanner.close();
    }
}

nextLine():用于接收用户输入的数据,结束条件为enter

import java.util.Scanner;

public class Demo02 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.println("使用nextLine()接收:");

        if (scanner.hasNextLine()){
            String str = scanner.nextLine();
            System.out.println("接收的数据为:"+str);
        }

        scanner.close();
    }

}

使用hasNext()方法和hasNextLine()方法判断接收的数据!

进阶使用:

import java.util.Scanner;

public class Demo05 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        double sum = 0;
        //计算输入了多少个数
        int m = 0;

        while (scanner.hasNextDouble()){
            //接收用户输入的数据
            double x = scanner.nextDouble();

            m = m + 1;  //m++
            sum = sum + x;
            System.out.println("你输入了第"+m+"个数据,当前结果为sum="+sum);
        }

        System.out.println("输入的数据的和为:"+sum);
        System.out.println("输入的数据的平均值为:"+(sum/m));


        scanner.close();
    }
}

2.顺序结构

概念:按照步骤一步步执行,它是所有算法的基础!

3.选择结构

3.1if选择结构

  1. 单选择

    import java.util.Scanner;
    
    public class IfDemo01 {
        public static void main(String[] args) {
            Scanner scanner = new Scanner(System.in);
    
            System.out.println("请输入内容:");
            String s = scanner.nextLine();
    
            //equals方法,判断字符串是否相等
            if (s.equals("Hello")){
                System.out.println(s);
            }
    
            System.out.println("End");
            scanner.close();
        }
    }
    
    
  2. 双选择

    import java.util.Scanner;
    
    public class IfDemo02 {
        public static void main(String[] args) {
            Scanner scanner = new Scanner(System.in);
    
            System.out.println("请输入成绩:");
            int score = scanner.nextInt();
            
            if (score>=60){
                System.out.println("及格");
            }else {
                System.out.println("不及格!");
            }
            
            
            scanner.close();
        }
    }
    
    
  3. 多选择

    import java.util.Scanner;
    
    public class IfDemo03 {
        public static void main(String[] args) {
            Scanner scanner = new Scanner(System.in);
            System.out.println("请输入成绩:");
    
            int score = scanner.nextInt();
    
            if (score==100){
                System.out.println("满分");
            }else if (90<=score && score<100){
                System.out.println("A级");
            }else if (80<=score && score<90){
                System.out.println("B级");
            }else if (70<=score && score<80){
                System.out.println("C级");
            }else if (60<=score && score<70){
                System.out.println("D级");
            }else if (score<60){
                System.out.println("不及格");
            }else {
                System.out.println("成绩不合法");
            }
        }
    }
    
    

3.2switch多选择结构

//字符串比较是jdk7新特性
        //字符的本质还是数字

public class SwitchDemo01 {
    public static void main(String[] args) {
        //case穿透,即没break就会把下面的所有case输出
        
        char grade = '4';

        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("未知等级");
        }
    }
}

3.3break和continue的区别

都不终止代码的运行,只是跳出循环

break:用于强行退出循环(主要用于switch和一些需要跳出代码块的循环语句中)

public class BreakDemo {
    public static void main(String[] args) {
        int i = 0;
        while (i<100){
            i++;
            System.out.println(i);
            if (i==20){
                break;
            }
        }
    }
}

continue:只跳出某一次循环(即跳过尚未执行的语句,进入下一次循环,再接着判断是否满足)

public class ContinueDemo {
    public static void main(String[] args) {
        int i = 0;
        while (i<100){
            i++;
            if (i%5==0){
                System.out.println();
                continue;
            }
            System.out.println(i);
        }
    }
}

4.循环结构

4.1while循环

使用while循环时,需注意不要出现死循环!

public class WhileDemo02 {
    public static void main(String[] args) {
        //计算1~100的和
        int i = 0;
        int sum = 0;
        while (i<=100){
            sum = sum + i;
            i++;
        }
        System.out.println(sum);
    }
}

4.2do…while循环

while循环是先判断后执行;do…while循环则是先执行后判断(保证最少执行一次)!

public class DoWhileDemo01 {
    public static void main(String[] args) {
        int i = 0;
        int sum = 0;

        do {
            sum = sum + i;
            i++;
        }while (i<=100);
        System.out.println(sum);
    }
}

//while和do...while区别
public class DoWhileDemo02 {
    public static void main(String[] args) {
        int a = 0;
        while (a<0){
            System.out.println(a);
            a++;
        }
        System.out.println("=============");
        do {
            System.out.println(a);
            a++;
        }while (a<0);
    }
}

4.3for循环

public class ForDemo01 {
    public static void main(String[] args) {
        //for循环和while循环的区别
        int i = 0;
        int a;
        int sum = 0;
        int sum1 = 0;

        while (i<100){
            i++;
            sum += i;
        }
        System.out.println(sum);
        System.out.println("========");
        for (a=0;a<=100;a++){
            sum1 = sum1 + a;
        }
        System.out.println(sum1);
    }
}

public class ForDemo02 {
    public static void main(String[] args) {
        //计算0~100之间奇数和偶数的和

        int oddSum = 0;
        int evenSUm = 0;

        for (int i = 0; i < 101; i++) {
            if (i%2!=0){
                oddSum += i;
            }else {
                evenSUm += i;
            }
        }
        System.out.println("奇数的和为:"+oddSum);
        System.out.println("偶数的和为:"+evenSUm);
    }
}

public class ForDemo03 {
    public static void main(String[] args) {
        //输出1~1000之间能被5整除的数,每行输出3个
        for (int i = 0; i < 1001; i++) {
            if (i%5==0){
                System.out.println(i+"\t");
            }
            if (i%(5*3)==0){//每行
                System.out.println();
                //System.out.print("\n");
            }
            //print不换行输出
            //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	
 */
public class ForDemo05 {
    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 + "=" + (j * i) + "\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	
 */
public class ForDemo04 {
    public static void main(String[] args) {
        for (int i=1;i<=9;i++) {
            //将输出的结果居中
            for (int a=i;a<9;a++){
                System.out.print("\t");
            }
            for (int j = 1; j <= i; j++) {
                System.out.print(j + "*" + i + "=" + (j * i) + "\t");
            }
            System.out.println();
        }
    }
}

增强for循环

public class ForDemo06 {
    public static void main(String[] args) {
        //增强for循环
        int[] numbers = {1,2,3,4,5};

        //普通方法
        for (int i = 0;i<5;i++){
            System.out.println(numbers[i]);
        }
        System.out.println("========");

        //用于遍历数组
        for (int x:numbers){
            System.out.println(x);
        }
    }
}

4.4练习

public class Test01 {
    //打印1~100的所有质数
    public static void main(String[] args) {
        for (int i = 2; i <= 100; i++) {//1既不是质数也不是和数,所以从2开始
            boolean k = true;
            for (int n = 2; n < i; n++) {
                if (i % n == 0) {
                    k = false;
                    break;
                }
            }
            if (k) {
                System.out.print(i + " ");
            }
        }
    }
}

public class Test02 {
    public static void main(String[] args) {
        //打印5行三角形
        for (int i = 1; i <= 5; i++){
            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();
        }
    }
}

4.5Debug

作用:用于调试代码,可以检查代码中数据的走向!

流程控制的语句都可以嵌套!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值