1. for๋ฃจํ”„๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ž…๋ ฅ๋œ ์ค„ ์ˆ˜ ๊ณ„์‚ฐ

10ํ‚ค๋ณด๋“œ์—์„œ ์ค„์„ ์ฝ๊ณ  ์ˆซ์ž์ธ ์ค„์˜ ์ˆ˜๋ฅผ ํ‘œ์‹œํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•ด ๋ด…์‹œ๋‹ค . ์˜ˆ:

์•”ํ˜ธ ์„ค๋ช…
Scanner console = new Scanner(System.in);
int count = 0;
for (int i = 0; i < 10; i++)
{
   if (console.hasNextInt())
      count++;
   console.nextLine();
}
System.out.println(count);
Create a Scanner object to read data from the console.
Store the number of numbers in the count variable.
Loop from 0 to 10 (not including 10).

If a number is entered,
then increase count by one.
Read a line from the console and don't save it anywhere.

Display the calculated count on the screen.
๋ฉ”๋ชจ

์ค„์— ๊ณต๋ฐฑ์œผ๋กœ ๊ตฌ๋ถ„๋œ ์—ฌ๋Ÿฌ ํ† ํฐ์ด ํฌํ•จ๋˜์–ด ์žˆ๊ณ  ๊ทธ ์ค‘ ์ฒซ ๋ฒˆ์งธ ํ† ํฐ์ด ์ˆซ์ž์ธ ๊ฒฝ์šฐ ๋‹ค๋ฅธ ํ† ํฐ์ด ์ˆซ์ž๊ฐ€ ์•„๋‹ˆ๋”๋ผ๋„ hasNextInt()๋ฉ”์„œ๋“œ๋Š” ๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค . true์ฆ‰, ๊ฐ ์ค„์— ํ•˜๋‚˜์˜ ํ† ํฐ๋งŒ ์ž…๋ ฅ๋œ ๊ฒฝ์šฐ์—๋งŒ ํ”„๋กœ๊ทธ๋žจ์ด ์˜ฌ๋ฐ”๋ฅด๊ฒŒ ์ž‘๋™ํ•ฉ๋‹ˆ๋‹ค.


for2. ๋ฃจํ”„๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ณ„์Šน ๊ณ„์‚ฐํ•˜๊ธฐ

์•„๋ฌด๊ฒƒ๋„ ์ฝ์ง€ ์•Š๊ณ  ๋Œ€์‹  ๋ฌด์–ธ๊ฐ€๋ฅผ ๊ณ„์‚ฐํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•ด ๋ด…์‹œ๋‹ค. ์–ด๋ ค์šด ๊ฒƒ. ์˜ˆ๋ฅผ ๋“ค์–ด ์ˆซ์ž์˜ ๊ณ„์Šน์ž…๋‹ˆ๋‹ค 10.

์ˆซ์ž์˜ ๊ณ„์Šน n( ๋กœ ํ‘œ์‹œ n!)์€ ์ผ๋ จ์˜ ์ˆซ์ž์˜ ๊ณฑ์ž…๋‹ˆ๋‹ค. 1*2*3*4*5*..*n;

์•”ํ˜ธ ์„ค๋ช…
int f = 1;
for (int i = 1; i <= 10; i++)
   f = f * i;
System.out.println(f);
We store the product of numbers in the f variable.
Loop from 1 to 10 (inclusive).
Multiply f by the next number (save the result in f).
Display the calculated amount on the screen.

์ˆซ์ž๋ฅผ f = 1๊ณฑํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์‹œ์ž‘ ๊ฐ’์€ ์ž…๋‹ˆ๋‹ค . ์›๋ž˜ ๋ผ๋ฉด f๋ชจ๋“  ์ˆซ์ž๋ฅผ ๊ณฑํ•œ ๊ฐ’์€ ์ž…๋‹ˆ๋‹ค .f000


3. for๋ฃจํ”„๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ™”๋ฉด์— ๊ทธ๋ฆฌ๊ธฐ

ํ™”๋ฉด์— ์‚ผ๊ฐํ˜•์„ ๊ทธ๋ฆฌ๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•ด ๋ด…์‹œ๋‹ค. ์ฒซ ๋ฒˆ์งธ ์ค„์€ 10๋ณ„ํ‘œ๋กœ ๊ตฌ์„ฑ๋˜๊ณ  ๋‘ ๋ฒˆ์งธ 9์ค„์€ 8๋ณ„ํ‘œ ๋กœ ๊ตฌ์„ฑ๋ฉ๋‹ˆ๋‹ค.

์•”ํ˜ธ ์„ค๋ช…
for (int i = 0; i < 10; i++)
{
   int starCount = 10 - i;
   for (int j = 0; j < starCount; j++)
      System.out.print("*");
   System.out.println();
}
Loop through the lines (there should be 10 lines in total).

Calculate how many asterisks should be in the line.
Loop over the individual asterisks
(display starCount asterisks).
Move the cursor to the next line so the lines are separate.

์—ฌ๊ธฐ์—๋Š” ๋‘ ๊ฐœ์˜ ์ค‘์ฒฉ ๋ฃจํ”„๊ฐ€ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. ๋‚ด๋ถ€ ๋ฃจํ”„๋Š” ์ฃผ์–ด์ง„ ์ค„์— ์˜ฌ๋ฐ”๋ฅธ ์ˆ˜์˜ ๋ณ„ํ‘œ๋ฅผ ํ‘œ์‹œํ•˜๋Š” ์—ญํ• ์„ ํ•ฉ๋‹ˆ๋‹ค.

๊ทธ๋ฆฌ๊ณ  ๋ผ์ธ์„ ๋ฐ˜๋ณตํ•˜๋ ค๋ฉด ์™ธ๋ถ€ ๋ฃจํ”„๊ฐ€ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค.