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+" ");
            }
    
        }
    }
  • 相关阅读:
    面试笔记之手写单例模式
    Java学习笔记六:Java最基础
    Mac下终端工具iTerm2的快捷键
    Typescript引用类型--字符串
    Typescript引用类型--数组
    Typescript函数作用域
    Typescript的函数定义方式
    初识Typescript以及Typescript的安装
    VS Code前端开发常用插件和常用快捷键
    Vue CLI3项目构建
  • 原文地址:https://www.cnblogs.com/faberbeta/p/java-exercise-03.html
Copyright © 2011-2022 走看看