zoukankan      html  css  js  c++  java
  • Java练习 标准输入,输出,以及while 循环

    while语句的结构

    while(条件1){
    //代码块1
    }
    do {
    // 代码块1
    //代码块2
    
    }while (判断条件)
    for (赋初始值;判断条件;增减标志量) {
    //代码块1
    //代码块2
    }
    for(元素类型type 元素变量var : 遍历对象obj) {
    //引用了var的Java语句
    }
    /**
    此种写法适用于遍历数组,集合等的操作
    即将当前遍历到的对象(数组,集合)中的值赋给元素的变量var,之后执行引用了var 的语句
    随后接着遍历,直到遍历完对象,所有元素类型type需与遍历对象中值的类型相同
    
    */
    package ch03;
    /**
     *
     * @classname: Test_while
     * @time: 2020年4月24日
     * @author: 嘉年华
    */
    public class Test_while {
    
        public static void main(String[] args) {
            int x =1;
            while(x<=10) {
                System.out.println("这是第"+x+"次循环");
                x++;
            }
        }
    }
    package ch03;
    /**
     *
     * @classname: Test_while
     * @time: 2020年4月24日
     * @author: 嘉年华
    */
    public class Test_while {
    
        public static void main(String[] args) {
            int x =1;
            do {
                System.out.println("这是第"+x+"次循环");
                x++;
            }while(x<=10);
        }
    }
    package ch03;
    /**
     *
     * @classname: Test_while
     * @time: 2020年4月24日
     * @author: 嘉年华
    */
    public class Test_while {
    
        public static void main(String[] args) {
            int sum =0;
            for (int i=1;i<=100;i++) {
                sum += i;
            }
            System.out.println("sum:" + sum);
    
        }
    }
    //foreach循环
    package ch03;
    /**
     *
     * @classname: Test_while
     * @time: 2020年4月24日
     * @author: 嘉年华
    */
    public class Test_while {
    
        public static void main(String[] args) {
            int[] a = {1,2,3,4,5,6};
            for (int i:a) {
                System.out.print(i+" ");
            }
    
        }
    }
  • 相关阅读:
    2020-05-12 Linux基本操作
    SpringBoot项目设置能访问静态资源,resource/static目录下文件
    2020-04-25 Hadoop框架学习
    2020-05-24 vue简单语法
    2020-04-25 elasticsearch
    2020-04-25 kafka
    2020-04-11 函数式数据处理(Java8)
    2020-03-29 分布式事务解决方案(RocketMQ)
    配置文件示例
    Spring-data-redis实现消息队列的demo(附源码)
  • 原文地址:https://www.cnblogs.com/faberbeta/p/java-exercise-03.html
Copyright © 2011-2022 走看看