You are using an outdated browser. Please upgrade your browser to improve your experience.

~61p까지

다중루프

루프가 중첩되는 수준에 따라 이중루프, 삼중루프라고 한다.

곱셉표

package com.practiceExample.Doit;

public class Multiple99_01 {
  public static void main(String[] args) {
    System.out.print("   |");
    for(int i = 1; i <= 9; i++) {
      System.out.printf("%3d", i);
    }
    System.out.println("\n---+---------------------------");

    for(int i = 1; i <= 9; i++) {
      System.out.printf("%2d |", i);
      for(int j = 1; j <= 9; j++) {
        System.out.printf("%3d", j*i);
      }
      System.out.println();
    }

  }
}

public class Multiple99_03 {
  public static void main(String[] args) {
    for(int i = 0; i < 5; i++) {
      System.out.print("*");
      for(int j = 0; j < 5; j++) {
        System.out.print("*");
      }
      System.out.println();
    }
  }
}

package com.practiceExample.Doit;

public class IntArray {
  public static void main(String[] args) {
    int[] a = new int[5];
    a[1] = 37;
    a[2] = 51;
    a[4] = a[1] * 2;

    for(int i = 0; i < a.length; i++) {
      System.out.println("a[" + i + "] = " + a[i]);
    }
  }
}

배열 복제(클론)

package com.practiceExample.Doit.chap02;

public class CloneArray {
  public static void main(String[] args) {
    int[] a = {1, 2, 3, 4, 5};
    int[] b = a.clone();

    b[3] = 0;

    System.out.print("a =");
    for(int i = 0; i < a.length; i++) {
      System.out.print(" " + a[i]);
    }
    System.out.print("\nb =");
    for(int i = 0; i < b.length; i++) {
      System.out.print(" " + b[i]);
    }
  }
}

배열의 최대값

package com.practiceExample.Doit.chap02;

import java.util.Scanner;

public class MaxOfArray {

  static int maxOf(int[] a) {
    int max = a[0];
    for(int i = 1; i <= a.length; i++) {
      if(a[i] > max) {
        max = a[i];
      }
    }
    return max;
  }
  public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);

    System.out.println("키의 최대값");
    System.out.println("사람수 : ");
    int x = scanner.nextInt();

    int[] height = new int [x];

    for(int i = 0; i < x; i++) {
      System.out.print("height[" + i + "] : ");
      height[i] = scanner.nextInt();
    }
    System.out.println("최대값 : " + maxOf(height));
  }
}

배열요소 역순으로 정렬하기

package com.practiceExample.Doit.chap02;

import java.util.Scanner;

public class ReverseArray {

  static void swap(int[] a, int idx1, int idx2) {
    int t = a[idx1];
    a[idx1] = a[idx2];
    a[idx2] = t;
  }

  static void reverse(int[] a) {
    for(int i = 0; i < a.length/2; i++) {
      swap(a, i, a.length - i - 1);
    }
  }
  public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);

    System.out.println("요솟수 : ");
    int num = scanner.nextInt();

    int[] x = new int[num];

    for(int i = 0; i < num; i++) {
      System.out.print("x[" + i + "] : ");
      x[i] = scanner.nextInt();
    }
    reverse(x);

    System.out.println("요소를 역순으로 정렬했습니다.");
    for(int i = 0; i < num; i++) {
      System.out.println("x[" + i + "] = " + x[i]);
    }
  }
}