Java流程控制

Java流程控制

Scanner类

next()方法和nextLine()方法

请添加图片描述
请添加图片描述


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();

    }
}


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();
    }
}

总结就是next方法不可以接收带空格的字符,而nextline方法以enter输入作为结束,可以接受空格。
需要注意的还有一点就是用了scanner不要忘了close

Scanner进阶

  • 简单介绍nextInt()和nextFloat()方法
package com.kuang.scanner;

import java.util.Scanner;

public class Demo03 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int i = 0;
        float f =0.0f;

        System.out.println("请输入整数:");
        if(scanner.hasNextLine()){
            i = scanner.nextInt();
            System.out.println(i);
        }else{
            System.out.println("您没有输入整数");
        }

        System.out.println("请输入小数:");
        if(scanner.hasNextLine()) {

            f = scanner.nextFloat();
            System.out.println(f);
        }else{
            System.out.println("您没有输入小数");
        }
        scanner.close();

    }
}

  • 小思考题目
    输入多个数字,求和和平均数,每输入一个按回车确认,通过输入非数字来结束执行并且计算结果
package com.kuang.scanner;

import java.util.Scanner;

public class Demo04 {
    public static void main(String[] args) {
//和
        double sum = 0;
//个数
        int m = 0;
        Scanner scanner = new Scanner(System.in);
//由此来确保输入的都是数字,如果输入别的字符会跳出循环
        while(scanner.hasNextDouble()){
            double x = scanner.nextDouble();
            sum = sum + x;
            m++;
        }
        System.out.println(m+"个数字的总和是:"+sum);
        System.out.println(m+"个数字的总和是:"+sum/m);

        scanner.close();
    }
}

顺序结构

程序自上而下逐条运行,就是java语言中的顺序结构


public class helloworld {
    public static void main(String[] args) {
        System.out.println("hello world!");
        System.out.println("i can do it well ");
    }
}

选择结构

单选择结构

public class Demo01 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入内容:");
        String s = scanner.next();
        //单选择结构
        //equals()方法 用来判断是否字符串是否相等
        if(s.equals("hello")){
            System.out.println(s);
        }
        System.out.println("end");

        scanner.close();
    }
}

双选择结构

public class Demo02 {
    public static void main(String[] args) {
        //双选择结构
        //考试分数大于60就是及格,小于60就是不及格
        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();
    }
}

多选择结构

if-else if结构

public class Demp03 {
    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(score<100&&score>=90){
            System.out.println("A级");
        }else if(score<90&&score>=80){
            System.out.println("B级");
        }else if(score<80&&score>=70){
            System.out.println("C级");
        }else if(score<70&&score>=60){
            System.out.println("D级");
        }else if(score<60&&score>=0){
            System.out.println("不及格");
        }else{
            System.out.println("不合法的数据");
        }

        scanner.close();
    }
}

switch多选择结构

请添加图片描述

public class Demo04 {
    public static void main(String[] args) {
        //switch多选择结构
        char c = 'c';
        switch(c){
            case 'a':
                System.out.println("a等级");
                break;
            case 'b':
                System.out.println("b等级");
                break;
            case 'c':
                System.out.println("c等级");
                break;
            default:
                System.out.println("请重新输入");
        }
    }
}
  • 需要特指出来的是字符串也可以作为表达式中的值
public class Demo05 {
    public static void main(String[] args) {
        //JDK7后允许字符串作为case后的变量表达式
        //case是具有”穿透性“
        String str = "sello";
        switch (str){
            case"hello":
                System.out.println("hello world");
                break;
            case"byebye":
                System.out.println("bye bye world");
                break;
            case"hey girl":
                System.out.println("hey girl");
                break;
            default:
                System.out.println("重新检测");
        }
    }
}

循环结构

while循环

  • 少部分情况需要循环一直执行,比如服务器的请求响应监听等
public class Demo01 {
    public static void main(String[] args) {
        //简单从1输出到100
        int i = 1;
        while (i <= 100) {
            System.out.println(i);
            i++;
        }
        //从1加到100
        int j = 0;
        int sum = 0;
        while (j <= 100) {
            sum = sum + j;
            j++;
        }
        System.out.println(sum);

    }
}

do…while循环

do…while循环和while循环的区别就是,do…while循环至少会执行一次

public class Demo02 {
    public static void main(String[] args) {
        int i = 0;
        int sum = 0;
        do{
            sum = sum + i;
            i++;
        }while(i<=100);
        System.out.println(sum);
    }
}

for循环

public class Demo03 {
    public static void main(String[] args) {
        int sum = 0;
        int i = 0;
        //总的来说,for循环就是//初始化//条件判断//迭代
        for(i=0;i<=100;i++){
            sum += i;
        }
        System.out.println(sum);
        System.out.println("循环结束!");
    }
}
  • 题目1
public class Demo04 {
    public static void main(String[] args) {
        //计算从0到100之间的奇数和偶数的和
        int i = 0;
        int sum1 = 0;
        int sum2 = 0;
        for(i=0;i<=100;i++){
            if(i%2!=0){
                sum1 = sum1+i;
            }else{
                sum2 = sum2+i;
            }
        }
        System.out.println("1—100的奇数和为:"+sum1);
        System.out.println("1—100的偶数和为:"+sum2);

    }
}
  • 题目二
public class Demo05 {
    public static void main(String[] args) {
        //用while或for循环输出1-1000之间能被5整除的数,并且每行输出3个
        int k = 0;
        for(int i=0;i<=1000;i++){
            if(i%5==0){
                System.out.print(i+"\t");
                k++;
                if(k%3==0){
                    System.out.println();
                    //print()打印完不换行
                    //println()打印完换行
                }
            }

        }
    }
}
  • 题目三
    打印九九乘法表
    for嵌套循环,并且需要具体问题具体分析
public class Demo06 {
    public static void main(String[] args) {
        /*
        1.我们先打印第一列
        2.我们把固定的1用循环包起来
        3.去掉重复的项 i<=j
        4.调整样式
         */
        int i = 0;
        int j = 0;
        for(j = 1; j <= 9; j++){
            for(i = 1; i <= j; i++){
                System.out.print(j+"*"+i+"="+(j*i)+"\t");
            }
            System.out.println();
        }
    }
}
//九九乘法表 需要好好理解思路
  • 增强for循环
public class Demo07 {
    public static void main(String[] args) {
        /*
        for(声明语句:表达式)
        {
            //代码句子
        }
        1.声明语句:数组元素
        2.表达式是要访问的数组名,或者返回值为数组的方法
         */
        int[] numbers = {10,20,30,40,50};
        for(int x:numbers){
            //和普通for循环不同的是用的是:而不是;号
            System.out.println(x);
        }
    }
}

break与continue

  • break强制跳出循环体,也可以用于switch语句块
  • continue 用于终止某次循环

练习题

  • 打印三角形
    1.三角形分为三部分去解决
    2.一部分一部分的解决问题
public class Demo08 {
    public static void main(String[] args) {
        //打印三角形
        //把三角形分成了三个部分去解决

        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();
        }

    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值