Java流程控制

1.用户交互Scanner

Scanner对象

之前我们学的基本语法中我们并没有实现程序和人的交互,但是Java给我们提供了这样一个工具类,我们可以获取用户的输入。java.util.Scanner 是Java5的新特性,我们可以通过Scanner类来获取用户的输入。

基本语法:

Scanner s = new Scanner(System.in);

 next()和nextLine()方法

通过Scanner类next()和nextLine()方法获取输入的字符串,在读取前我们一般需要使用hasNext()与hasNextLine()判断是否还有输入的数据。

package org.psmaxwell.scanner;

import java.util.Scanner;

public class Demo03 {
    public static void main(String[] args) {
        // receive the data from keyboard
        Scanner scanner = new Scanner(System.in);

        System.out.println("Please input the data: ");

        String str = scanner.nextLine();
        System.out.println("output's content: "+str);

        scanner.close();
    }
}
package org.psmaxwell.scanner;

import java.util.Scanner;

public class Demo02 {
    public static void main(String[] args) {
        // receive the data from keyboard
        Scanner scanner = new Scanner(System.in);

        System.out.println("use the nextLine way to receive: ");

        // IF there is existing input value
        if(scanner.hasNextLine()){
            String str = scanner.nextLine();   // wait user input value
            System.out.println("Output's content: " +str);
        }

        scanner.close();


    }
}

2.Scanner进阶使用

package org.psmaxwell.scanner;

import java.util.Scanner;

public class Demo05 {
    public static void main(String[] args) {
        //我们可以输入多个数字,并求其总和与平均数,每输入一个数字用回车确认,通过输入非数字来结束输入并输出执行结果。
        Scanner scanner = new Scanner(System.in);   // ctrl+ alt +v 补全代码

        // summary
        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(m + "个数的和为" + sum);
        System.out.println(m + "个数的平均值是" + (sum / m));

        scanner.close();
    }
}
package org.psmaxwell.scanner;

import java.util.Scanner;

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


        //input the data from keyboard
        int i = 0;
        float f = 0.0f;

        System.out.println("请输入整数: ");

        if(scanner.hasNextInt()){
            i = scanner.nextInt();      //补全代码 CTRL + alt + V
            System.out.println("整数数据 :" + i);
        }else{
            System.out.println("输入的不是整数数据!");
        }


        System.out.println("请输入小数: ");

        if(scanner.hasNextFloat()){
            f = scanner.nextFloat();      //补全代码 CTRL + alt + V
            System.out.println("小数数据 :" + f);
        }else{
            System.out.println("输入的不是小数数据!");
        }


        scanner.close();
    }

}
package org.psmaxwell.scanner;

import java.util.Scanner;

public class Demo03 {
    public static void main(String[] args) {
        // receive the data from keyboard
        Scanner scanner = new Scanner(System.in);

        System.out.println("Please input the data: ");

        String str = scanner.nextLine();
        System.out.println("output's content: "+str);

        scanner.close();
    }
}

3.顺序结构

  • JAVA的基本结构就是顺序结构,除非特别指明,否则就按照顺序一句一句执行。
  • 顺序结构最简单的算法结构。
  • 语句与语句之间,框与框之间是按从上到下的顺序进行的,它是由若干个依次执行的处理步骤组成的,它是任何一个算法都离不开的一种基本算法结构。
package org.psmaxwell.structure;

public class SequenceDemo01 {
    public static void main(String[] args) {
        System.out.println("hello1");
        System.out.println("hello2");
        System.out.println("hello3");
        System.out.println("hello4");
        System.out.println("hello5");
    }
}

4.if选择结构

if单项选择结构

if双项选择结构

if多选择结构

 

嵌套的if结构

5.switch多选择结构

package org.psmaxwell.structure;

public class SwitchDemo01 {
    public static void main(String[] args) {
        // case 穿透  // switch 匹配一个具体的值
        char grade = 'C';

        switch (grade){
            case 'A':
                System.out.println("Excellent");
                break;   //可选
            case 'B':
                System.out.println("Good");
                break;    //可选
            case 'C':
                System.out.println("Not good");
                break;    //可选
            case 'D':
                System.out.println("Just so so");
                break;     //可选
            case 'E':
                System.out.println("Failed");
                break;      //可选
            default:
                System.out.println("Not sure");
        }
    }
}
package org.psmaxwell.structure;

public class SwitchDemo02 {
    public static void main(String[] args) {
        String name = "Maxwell";

        // JDK7 的新特性,表达式结果可以是字符串!!!
        // 字符的本质还是数字

        // 反编译 java----class(字节码文件)----反编译(IDEA)

        switch (name){
            case "Mountain":
                System.out.println("Mountain");
                break;
            case "Maxwell":
                System.out.println("Maxwell");
                break;
            default:
                System.out.println("What happen?");
        }

    }
}

6.While循环详解

 

package org.psmaxwell.structure;

public class WhileDemo03 {
    public static void main(String[] args) {
        // calculate 1+2+3+...+100=?
        int i = 0;
        int sum = 0;
        while (i<=100){
            sum=sum+i;
            i++;
        }
        System.out.println(sum);
    }
}

 7.DoWhile循环

package org.psmaxwell.structure;

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

8. while 和do...while的区别:

package org.psmaxwell.structure;

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

 9.FOR 循环

package org.psmaxwell.structure;

public class ForDemo01 {
    public static void main(String[] args) {
        int a = 1;    //初始化条件


        while(a<=100){//条件判断
            System.out.println(a); //循环体
            a+=2; //迭代
        }

        System.out.println("while循环结束!");
        //初始化//条件判断//迭代
        for (int i = 1; i < 100; i++) {
            System.out.println(i);
        }
        System.out.println("for循环结束!");
    }
}

 

在Java5中引入了一种主要用于数组的增强型for循环

package org.psmaxwell.structure;

public class ForDemo02 {
    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; // oddSum = oddSum + i;
            }else{ //偶数
                evenSum+=i;
            }
        }
        System.out.println("奇数的和: " +oddSum);
        System.out.println("偶数的和: " +evenSum);
    }
}
package org.psmaxwell.structure;

public class ForDemo03 {
    public static void main(String[] args) {
        for (int i = 0; i <= 1000; i++) {
            if(i%5==0){
                System.out.println(i+"\t");
            }
            if(i%(5*3)==0){ //每行
                System.out.println();
                //System.out.print("\n");
            }
        }
        //println 输出完会换行
        //print 输出完不会换行
    }
}

10.打印九九乘法表

package org.psmaxwell.structure;

public class ForDemo4 {
    public static void main(String[] args) {
        //1.打印第一列,
        //2.将固定的1再用一个循环抱起来
        //3.去掉重复项目i <=j
        //4.调整样式
        for (int j = 1; j <= 9; j++) {
            for (int i = 1; i <= j; i++) {
                System.out.print(j+"*"+i+"="+(j*i) + "\t");
            }
            System.out.println();
        }
    }
}

11. 增强for循环

package org.psmaxwell.structure;

public class ForDemo05 {
    public static void main(String[] args) {
        int[] numbers = {10,20,30,40,50}; //定义一个数组

        for (int i = 0; i < 5; i++) {
            System.out.println(numbers[i]);
        }
        System.out.println("=================================");
        //遍历数组的元素
        for(int x:numbers){
            System.out.println(x);
        }
    }
}

12.break、continue、goto

 

//Break

package org.psmaxwell.structure;

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

        System.out.println("123");
    }
}
//continue

package org.psmaxwell.structure;

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

        //break 在任何循环语句的主体部分,均可用break控制循环的流程。
        //break 用于强行退出循环,不执行循环中剩余的语句。(break语句也在switch语句中使用)
        //
        // continue 语句用在循环体语句中,用于终止某次循环过程,即跳过循环体中尚未执行的语句,接着进行下一次是否执行循环的判定。
    }
}
goto

package org.psmaxwell.structure;

public class LabelDemo {
    public static void main(String[] args) {
        //打印101-150之间的所有质数
        int count = 0;
        //不建议使用
        outer:for (int i = 101; i < 150; i++) {
            for (int j = 2; j < i/2; j++) {
                if (i % j ==0){
                    continue outer;
                }
            }
            System.out.print(i+" ");
        }
    }
}

13.打印三角形及Debug

package org.psmaxwell.structure;

public class TestDemo {
    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、付费专栏及课程。

余额充值