栏目分类:
子分类:
返回
文库吧用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
文库吧 > IT > 软件开发 > 后端开发 > Java

零基础Java流程控制代码笔记

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

零基础Java流程控制代码笔记

Java流程控制01Scanner next()与nextLine()的区别

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

}

运行结果:( next()方法不能输出带空格的字符串!)

public class Demo02 {
    public static void main(String[] args) {
        //从键盘接收数据
        Scanner scanner = new Scanner(System.in);

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

        //判断是否还有输入
        if (scanner.hasNextLine()){
            String str = scanner.nextLine();
            System.out.println("输出的内容为:"+str);
        }

        scanner.close();
    }

}

运行结果:( nextLine()方法可以输出带空格的字符串!)

2. if语句(判断是否为整数还是小数)
public class Demo04 {

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);

        //从键盘接受数据
        int i = 0;
        float f= 0.0f;

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

        //如果...那么
        if (scanner.hasNextInt()){
            i = scanner.nextInt();
            System.out.println("整数数据:" + i);
        }else {
            System.out.println("输入的不是整数数据!");
        }



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

        //如果...那么
        if (scanner.hasNextFloat()){
            f = scanner.nextFloat();
            System.out.println("小数数据:" + f);
        }else {
            System.out.println("输入的不是小数数据!");
        }

        scanner.close();
    }
}

运行结果:

3. 输入数字求总和与平均值
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(m + "个数的和为" + sum);
        System.out.println(m + "个数的平均值是" + (sum / m));

        scanner.close();
    }
}

运行结果:

Java流程控制02顺序结构
public class ShunXuDemo {

    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");
    }
}
Java流程控制03选择结构 While
public class WhileDemo01 {

    public static void main(String[] args) {

        //输出1~100

        int i=0;
        while (i<100){
            i++;
            System.out.println();
        }

    }
}


public class WhileDemo03 {

    public static void main(String[] args) {
        //计算1+2+3+...+100=?

        int i=0;
        int sum=0;

        while (i<=100){
            sum = sum+i;
            i++;
        }
        System.out.println(sum);
    }
}
多分支选择Switch
public class SwitchDemo01 {
    public static void main(String[] args) {
        //多分支选择结构
        char grade = 'C';

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

}


public class SwitchDemo02 {

    public static void main(String[] args) {
        String name= "王同学";
        //

        switch (name){
            case "王同学":
                System.out.println("王同学");
                break;
            case "小王":
                System.out.println("小王");
                break;
            default:
                System.out.println("错误!!!");
        }
    }
}

DoWhile

从1加到100的总和:

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 {

    //while先判断后执行,dowhile是先执行后判断!
    //do...while总是保证循环体会被至少执行一次!
    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);

    }
}
if选择结构
public class IfDemo01 {
    //if选择结构
    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();


    }
}


public class IfDemo02 {
    //if单选择结构
    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();
    }
}


public class IfDemo03 {
    //if多选择结构
    public static void main(String[] args) {
        //考试分数大于等于60就是及格,小于60分就不及格
        Scanner scanner = new Scanner(System.in);

        System.out.println("请输入成绩:");

        int score = scanner.nextInt();
        //double score = scanner.nextDouble();    //可以输入小数进行查询

        if (score==100){
            System.out.println("恭喜,满分!");
        }else if (score<100 && score>=90){
            System.out.println("A级");
        }else if (score<100 && score>=80){
            System.out.println("B级");
        }else if (score<100 && score>=70){
            System.out.println("C级");
        }else if (score<100 && score>=60){
            System.out.println("D级");
        }else if (score<60 && score>=0){
            System.out.println("不及格");
        } else {
            System.out.println("成绩不合法!");
        }

        scanner.close();
    }
}
Java流程控制04循环结构
public class ForDemo01 {

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

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

        System.out.println("while循环结束!");

        //初始化;条件判断;迭代
        for (int i=1;i<=10;i++){
            System.out.println(i);
        }

        //快捷键100.for
        

        System.out.println("for循环结束!");

    }
}


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;
            }else {     //偶数
                evenSum+=i;
            }
        }

        System.out.println("奇数的和:"+oddSum);
        System.out.println("偶数的和:"+evenSum);


    }
}


public class ForDemo03 {

    public static void main(String[] args) {
        //练习2:用while或for循环输出1~1000之间能被5整除的数,并且每行输出3个

        for (int i = 0; i <= 1000; i++) {
            if (i%5==0){
                System.out.print(i+"t");
            }
            if (i%(5*3)==0){//换行
                System.out.println();
            }
        }

        //println   输出会换行
        //print 输出不会换行

    }
}


//输出九九乘法表

public class ForDemo04 {

    public static void main(String[] args) {
        //练习3:九九乘法表
        //1. 先打印第一列
        //2. 我们把固定的1再用一个循环包起来
        //3. 去掉重复项,i<=j
        //4. 调整样式
        for (int j = 1; j <= 9; j++) {
            for (int i = 1 ; i <= j; i++) {
                System.out.print(i+"*"+j+"="+(j*i) + "t");
            }
            System.out.println();
        }

    }
}


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

}
Java流程控制05break&continue break和continue区别
  • break用于强行退出循环
    continue用于终止某次循环,跳过当前的循环,接着进行下一次循环的判定
break
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
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);
        }

    }
}
Java流程控制06练习 打印101~150之间的所有的质数
public class LabelDemo {
    public static void main(String[] args) {
        //质数:大于1的自然数中,除了1和它本身以外不再有其他因数的自然数

        int count=0;
        outer:for (int i=101;i<150;i++){
            for (int j =2;j   //j
                    continue outer;
                }
            }
            System.out.print(i+" ");
        }

    }
}

运行结果:

打印三角形
public class TestDemo01 {

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


    }
}

运行结果:

转载请注明:文章转载自 www.wk8.com.cn
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 wk8.com.cn

ICP备案号:晋ICP备2021003244-6号