GiantStepDEV
article thumbnail

for๋ฌธ

  • for๋ฌธ๋‚ด์— ์ดˆ๊ธฐ์‹, ์กฐ๊ฑด์‹, ์ฆ๊ฐ์‹์— ๋Œ€ํ•œ ์‹์„ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ์œ ํ•œ์˜ ๊ฐฏ์ˆ˜๋ฅผ ๋ฐ˜๋ณต ์ˆ˜ํ–‰ํ•  ๋•Œ ์œ ์šฉํ•˜๊ฒŒ ์‚ฌ์šฉ
  • for๋ฌธ๋‚ด์˜ ์ดˆ๊ธฐ์‹, ์กฐ๊ฑด์‹, ์ฆ๊ฐ์‹์€ ๊ฐ๊ฐ ์ƒ๋žต ๊ฐ€๋Šฅ
  • for๋ฌธ์—์„œ ์‹คํ–‰๋  ๋ช…๋ น๋ฌธ์ด ํ•œ ์ค„์ธ ๊ฒฝ์šฐ ์ค‘๊ด„ํ˜ธ ์ƒ๋žต ๊ฐ€๋Šฅ (์กฐ๊ฑด๋ฌธ๋„ ๋™์ผ)

๊ธฐ๋ณธ for๋ฌธ

public class ForEx1 {
    public static void main(String[] args) {
        for(int i = 1; i <= 100; i++) {
            System.out.printf("%4d", i);
            if(i % 10 == 0) System.out.println();
        }

๊ตฌ๊ตฌ๋‹จ ์ฐ๊ธฐ

public class ForEx1 {
    public static void main(String[] args) {
        for (int i = 2; i < 10; i++) {
            System.out.println("๊ตฌ๊ตฌ๋‹จ " + i + "๋‹จ");
            for (int j = 1; j < 10; j++) {
                System.out.printf("%d x %d = %d",i,j,(i*j));
                System.out.println();
            }System.out.println();
        }
    }
}

๋ณ„ ์ฐ๊ธฐ

public class ForEx1 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("๋ณ„ ๊ฐฏ์ˆ˜ : ");
        int star = sc.nextInt();
        for(int i=0; i<star; i++) {
            for(int j=0; j<star; j++) {
                System.out.print("*  ");
            }System.out.println();
        }
    }
}

 

๋ณ„ ์ฐ๊ธฐ(2)

public static void main(String[]args) {
    Scanner sc = new Scanner(System.in);
    int n = sc.nextInt();
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < i+1;j++) {
            System.out.print("* ");
        }
        System.out.println();
    }
}

* ๋ณ„ ์ฐ๊ธฐ ์—ฐ์Šต์€ ์ฝ”๋”ฉ ์—ฐ์Šต ๊ฒŒ์‹œํŒ์—์„œ ์ข€ ๋” ๋‹ค์–‘ํ•˜๊ฒŒ ๋‹ค๋ค„ ๋ณผ ์˜ˆ์ •์ด๋‹ค.

์‚ฌ๊ฐํ˜• ์ฐ๊ธฐ

- ์–‘์˜ ์ •์ˆ˜ n์„ ๋ฐ›์•„์„œ n * n ํฌ๊ธฐ์˜ ํ–‰๋ ฌ์„ ์ถœ๋ ฅ
- ๊ฐ’์€ 1๋ถ€ํ„ฐ ์‹œ์ž‘
- ์ถœ๋ ฅ ์˜ˆ์‹œ) ์ •์ˆ˜ ๊ฐ’ : 3
1 2 3
4 5 6
7 8 9
package For๋ฐ˜๋ณต๋ฌธ์—ฐ์Šต;
import java.util.Scanner;

public class ForEx1 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("์ •์ˆ˜๋ฅผ ์ž…๋ ฅ ํ•˜์„ธ์š”. : ");
        int num = sc.nextInt();

        for(int i=1; i<=(num * num); i++) {
            System.out.printf("%3d",i);
            if(i % num == 0) System.out.println();
        }
    }
}

์ฃผ์‚ฌ์œ„ ๊ฒŒ์ž„

์ฃผ์‚ฌ์œ„ 2๊ฐœ ํ•ฉ์ด 12๊ฐ€ ๋˜๋ฉด ํƒˆ์ถœํ•˜๋Š” ๊ฒŒ์ž„
package ์ฃผ์‚ฌ์œ„๊ฒŒ์ž„;
public class DiceGameEx1 {
    public static void main(String[] args) {
        int dice1, dice2;
        int count = 0;

        while(true) {
            count++;
            dice1 = (int)(Math.random() * 6) + 1;
            dice2 = (int)(Math.random() * 6) + 1;
            System.out.println("์ฃผ์‚ฌ์œ„์˜ ํ•ฉ์€ " + (dice1+dice2));
            if((dice1 + dice2) == 12) break;
        } System.out.println("ํƒˆ์ถœ!");
        System.out.printf("%d๋ฒˆ ์‹œ๋„ ๋์— ํƒˆ์ถœ ํ•˜์˜€์Šต๋‹ˆ๋‹ค.",count);
    }
}

* 1 ๋ถ€ํ„ฐ n ๊นŒ์ง€ ๋žœ๋ค ์ˆซ์ž ์ถœ๋ ฅ : (int)(Math.random() * n) + 1;

profile

GiantStepDEV

@kongmi

ํฌ์ŠคํŒ…์ด ์ข‹์•˜๋‹ค๋ฉด "์ข‹์•„์š”โค๏ธ" ๋˜๋Š” "๊ตฌ๋…๐Ÿ‘๐Ÿป" ํ•ด์ฃผ์„ธ์š”!