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+" ");
            }
    
        }
    }
  • 相关阅读:
    图的存储结构(精编)
    二叉树的输入
    哈夫曼树及编码
    C. Bits (Codeforces Round #276 (Div. 2) )
    C++ Map 容器
    POJ 1080 Human Gene Functions(dp)
    数和二叉树——二叉树的建立及应用(遍历等)(基础篇)
    数独问题的介绍及POJ 2676-Sudoku(dfs+剪枝)
    【数据结构】——稀疏矩阵转置
    POJ 3083 Children of the Candy Corn
  • 原文地址:https://www.cnblogs.com/faberbeta/p/java-exercise-03.html
Copyright © 2011-2022 走看看