java流程控制 #学习日记6

用户交互Scanner

public class Demo {
    public static void main(String[] args) {
        //创建扫描对象用于接收键盘数据
        Scanner scanner = new Scanner(System.in);
        System.out.println("使用next方式接收");
        //判断用户是否输入数据
        if (scanner.hasNext()){
            //使用next方式接收
            String str = scanner.next();
            System.out.println("输出:"+str);
        }
        //凡是属于IO流的类不关闭会一直占用资源
        scanner.close();
    }
}
public class Demo1 {
    public static void main(String[] args) {
        //创建扫描对象用于接收键盘数据
        Scanner scanner = new Scanner(System.in);
        System.out.println("使用nextLine方式接收");
        //判断用户是否输入数据
        if (scanner.hasNextLine()){
            //使用nextLine方式接收
            String str = scanner.nextLine();
            System.out.println("输出:"+str);
        }
        //凡是属于IO流的类不关闭会一直占用资源
        scanner.close();
    }
}
public class Demo2 {
    //不使用if
    public static void main(String[] args) {
        //创建扫描对象用于接收键盘数据
        Scanner scanner = new Scanner(System.in);
        System.out.println("使用nextLine方式接收");
            //使用nextLine方式接收
            String str = scanner.nextLine();
            System.out.println("输出:"+str);
        //凡是属于IO流的类不关闭会一直占用资源
        scanner.close();
    }
}
public class Demo3 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入整数");
        if (scanner.hasNextInt()){
        int numInt = scanner.nextInt();//nextInt()整数的输入
        System.out.println("整数为:"+numInt);
        }else {
        System.out.println("输入不是整数!");
        }
        System.out.println("请输入小数:");
        if (scanner.hasNextFloat()){
        float numFloat = scanner.nextFloat();//同理nextFloat为浮点数的输入
        System.out.println("输入小数:"+numFloat);
        }else {
        System.out.println("输入小数!");
        }
        scanner.close();
    }
}

next():

  1. 一定要读取到有效字符后才能结束输入

  2. 对输出有效字符之前的空白,会自动去掉

  3. 有效字符后的空白作为分隔符或结束符

  4. 不能得到带有空格的字符串

    nextLine():

    1. 以Enter为结束符
    2. 可以获得空白

If选择结构

public class If {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("输入");
        String s = scanner.nextLine();

        if (s.equals("hello")){
            System.out.println(s);
        }
        System.out.println("end");
        scanner.close();
    }
}
public class If1 {
    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();
    }
}
public class If2 {
    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>=80){
            System.out.println("优秀");
        }else if (score<80&&score>=70){
            System.out.println("良好");
        }else if (score<70&&score>=60){
            System.out.println("及格");
        }else if (score<60){
            System.out.println("不及格");
        }else{
            System.out.println("非法成绩");
        }
        scanner.close();
        /*if语句至多有一个else,且在所有else if语句之后
        if语句有若干个else if语句,必须在else前
        一旦else if语句检验为ture,其他的else和else if都跳过执行
         */
    }
}

Switch选择结构

public class Switch {
    public static void main(String[] args) {
        char grade = 'a';
        //switch匹配一个具体值
        switch (grade){
            //case 穿透
            case 'a':
                System.out.println("666");
                break;
            case 'b':
                System.out.println("可以");
                break;
            case 'c':
                System.out.println("通过");
                break;
            case 'd':
                System.out.println("爬");
                break;
            default://都不是
                System.out.println("???");
        }
    }
}
public class Switch1 {
    public static void main(String[] args) {
        String s = "我";
        switch (s){//也可以匹配字符串
            case "我":
                System.out.println("不一样的烟火");
                break;
            case "喵喵喵":
                System.out.println("喵喵喵");
                break;
            default:
                System.out.println("???");
        }
    }
}

While循环

public class While {
    public static void main(String[] args) {
        int i = 0;
        while (i<100){//while (布尔表达式)
            i++;//循环内容
            System.out.println(i);
        }
    }
}
public class While1 {
    public static void main(String[] args) {
        while (true){//死循环
        }
    }
}
public class While2 {
    public static void main(String[] args) {
        //问题:1+2+3+......+99+100=?
        int i = 0;
        int sum = 0;
        while (i<=100){
            sum = sum + i;
            i++;
        }
        System.out.println(sum);
    }
}

Do While循环

public class DoWhile {
    public static void main(String[] args) {
        int i = 0;
        int sum = 0;
        do {
            sum = sum + i;
            i++;
        }while (i<=100);
        System.out.println(sum);
        //Do...While总是保证循环体会被至少执行一次
        //while先判断后执行,dowhile先执行后判断
    }
}

For循环

public class For {
    public static void main(String[] args) {
        int i = 1;//初始化条件
        while (i<=100){//判断条件
            System.out.println(i);//循环体
            i+=2;//迭代
        }
        System.out.println("while循环结束");
        //   初始化/     条件判断  /迭代
        for (int i1 = 1;i1<=100;i1++){
            System.out.println(i1);
        }
        System.out.println("for循环结束");
        //for循环结构是支持迭代的一种通用结构,是最有效,最灵活的循环结构。
        //快捷键 数字+.+for
        /*最先执行初始化步骤,可以说明一种类型,但可初始化一个或多个循环体控制变量,也可以是空语句
        然后检测布尔表达式的值,true执行循环体,false终止循环,开始执行循环体后的语句
        执行一次循环后,更新循环控制变量(迭代因子控制循环变量的增减
        再检测布尔表达式,继续循环
         */
        for (;;){
            //死循环
        }
    }
}
public class For1 {
    public static void main(String[] args) {
        //问题:计算0到100之间的奇数偶数之和
        int oddSum = 0;
        int evenSum = 0;
        for (int i = 0; i <= 100; i++) {
            if (i%2!=0) {
                oddSum += i;
            }else{
                evenSum +=i;
            }
        }
        System.out.println("奇数:"+oddSum);
        System.out.println("偶数:"+evenSum);
    }
}
public class For2 {
    public static void main(String[] args) {
        //问题:输出1到1000之间被5整除的数,每行输出3个
        for ( int i = 0;i<= 1000; i++){
            if (i % 5 == 0){
                System.out.print(i+"\t");
            }
            if (i%15==0){
                System.out.println();
                //System.out.print("\n");
                //print输出不会换行
                //println输出会换行
            }
        }
    }
}
public class For3 {
    public static void main(String[] args) {
        //问题:打印九九乘法表
        for (int z = 1; z <= 9; z++) {
            for (int i = 1; i <= z; i++) {
            System.out.print(z+"*"+i+"="+z*i+"\t");
        }
            System.out.println();
        }
    }
}
public class For4 {
    public static void main(String[] args) {
        int[] numbers = {10,20,30,40,50};//定义数组
        //遍历数组的元素
        for (int x:numbers){
            System.out.println(x);
        }
    }
}

Break&Continue

public class Break {
    public static void main(String[] args) {
        int i = 0;
        while (i<100){
            i++;
            System.out.println(i);
            if (i==30){
                break;//由于强行退出循环,不执行循环中剩余的语句
            }
        }
    }
}
public class Continue {
    public static void main(String[] args) {
        int i = 0;
        while (i<100){
            System.out.println();
            i++;
            if (i%10==0){
                System.out.println();
                continue;//用于跳过某次循环过程,跳过循环体尚未执行的语句,接着执行下一次是否循环的判定。
            }
            System.out.print(i);
        }
    }
}

练习:输出三角形

public class Text {
    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();
        }
    }
}

顺序结构也是一种结构

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值