第八期 Java 基础课-20190127

课堂练习

  • 九九乘法表
    @Test
    public void fun9(){
          int i,j=0;
            for (i=1; i <10 ; i++) {
                for ( j = 1; j <=i ; j++) {
                    System.out.print(i+"*"+j+"="+i*j+" ");


                }
                System.out.println();
            }
        }
  • 直角三角形&倒直角三角形&等腰三角形
 /**
     * 正三角
     */
    @Test
    public void fun1(){
        for (int i = 0; i <10 ; i++) {
            for (int j = 0; j <i ; j++) {
                System.out.print("*");
            }
            System.out.println();
        }
    }


    @Test
    public void fun3(){
        for (int i = 0; i <10 ; i++) {
            for (int j = i; j <10; j++) {
                System.out.print("*");
            }
            System.out.println();
        }
    }

    @Test

        public void fun7(){
            //循环输出等腰三角形
            int max=10;//行数
            //控制行
            for(int i=1;i<=max;i++)
            {
                //控制空格
                for(int k=1;k<=max-i;k++)
                {
                    System.out.print(" ");//print输出不换行
                }
                //控制“*”
                for(int j=1;j<=2*i-1;j++)
                {
                    System.out.print("*");//print输出不换行
                }
                System.out.println();//利用println输出换行
            }
        }

数组的三种创建方式

  • int[] 名字 = new int[5];
  • int[] 名字 = new int[]{1,2,3,4,5,6,7};
  • int[] 名字 = {1,2,3,4,5,6,7,8};

map遍历三种方式

  /**
         * map遍历方式1
         */
Iterator<String> iterator = hashMap.keySet().iterator();
        while (iterator.hasNext()){
            String key = iterator.next();
            System.out.println("key值是"+key);

            System.out.println("value值是"+hashMap.get(key));
        }

        /**
         * map遍历方式2
         */
        Set<Map.Entry<String, Object>> entries = hashMap.entrySet();
        for (Map.Entry en:entries) {
            System.out.println("key值是"+en.getKey());
            System.out.println("value值是"+en.getValue());
        }

        
        
        for (Map.Entry<String,Object> en:hashMap.entrySet()) {
            System.out.println("key值是"+en.getKey());
            System.out.println("value值是"+en.getValue());
        }


        /**
         * map 第三种遍历方式
         */
        Set<String> strings = hashMap.keySet();

        for (String key:strings) {
            System.out.println("key值是"+key);
        }
        Collection<Object> values = hashMap.values();
        for (Object value:values) {
            System.out.println("value值是"+value);
        }