๋ฌด์์ ์ ์ (0~5)๋ฅผ ํ๊ฐ ์ถ์ถํ์ฌ ๊ทธ ์๊ฐ 0,1์ด ์๋ ๊ฒฝ์ฐ์๋ง '์ ํจํ ์'๋ผ๊ณ ํ์ํ๊ณ ๊ทธ ์ธ์๋ ๋ชจ๋ '๋ฌดํจํ ์'๋ผ๊ณ ํ์ํ๋ ๊ธฐ๋ฅ์ ์์ฑํ์์ค.
์ ์ผ ๋จผ์ Random ํด๋์ค์ ๊ฐ์ฒด(์ธ์คํด์ค, instance)๋ฅผ ์์ฑํด์ผ ํ๋ค. (heap์ด๋ผ๋ ์์ญ์ ํ ๋น๋๋ค.)
Random num2 = new Random();
int num4 = num2.nextInt(6);
String str2 = (num4!=0 && num4!=1) ? "์ ํจํ ์" : "๋ฌดํจํ ์";
System.out.println(str2 + num4);
* ์ฆ๊ฐ์ฐ์ฐ์ (์ฆ๊ฐ, ๊ฐ์ ์ฐ์ฐ์)
์ฆ๊ฐ ์ฐ์ฐ์ (Increment Operator) : ++
System.out.println(++num); // ์ ์ฆ๊ฐ ์ฐ์ฐ์(Pre increment Operator) ์ฆ๊ฐํ ํ์ ๊ฐ์ด ์ถ๋ ฅ๋๊ธฐ ๋๋ฌธ์ 3์ด ๋์จ๋ค. ์์ num์ +1
์ฝ์๊ฐ :
1
2
3
Scanner scan = new Scanner(System.in);
System.out.print("์ซ์์
๋ ฅ:");
String input = scan.nextLine();
int x = Integer.parseInt(input);
int c = 1;
int v = (x * c);
System.out.println(v);
System.out.println(v * (++c));
System.out.println(v * (++c));
System.out.println(v * (++c));
System.out.println(v * (++c));
System.out.println(v * (++c));
System.out.println(v * (++c));
System.out.println(v * (++c));
System.out.println(v * (++c));
๋ฌธ์์ด์ ์ฌ์ฉํ์ฌ ํ์ด ๋ณธ๋ค๋ฉด
Scanner scan = new Scanner(System.in);
System.out.print("2~9 ์ฌ์ด ์ซ์ ์
๋ ฅ:");
String input = scan.nextLine();
int x = Integer.parseInt(input);
int c = 1;
String format = "%d * %d = %d \n";
System.out.printf(format, x, c, x*(c++));
System.out.printf(format, x, c, x*(c++));
System.out.printf(format, x, c, x*(c++));
System.out.printf(format, x, c, x*(c++));
System.out.printf(format, x, c, x*(c++));
System.out.printf(format, x, c, x*(c++));
System.out.printf(format, x, c, x*(c++));
System.out.printf(format, x, c, x*(c++));
System.out.printf(format, x, c, x*(c++));
}
ํค๋ณด๋์์ ์์ด๋, ์ํธ๋ฅผ ์ ๋ ฅ๋ฐ๊ณ ์์ด๋๊ฐ smith, ์ํธ๊ฐ 1111์ด๋ผ๋ฉด '๋ก๊ทธ์ธ ์ฑ๊ณต'ํ์ ์๋๋ฉด '๋ก๊ทธ์ธ ์คํจ' ํ์
๋ค๋ฅธ ๋ฐฉ๋ฒ ์ฐ๊ธฐ
๋ฉ์๋ ๋ง๋๋ ๋ฒ
public static void main(String[] args)
{
elseif(); //๋ฉ์ธ ๋ฉ์๋ ์์ elseif ๋ฉ์๋ ํธ์ถ(์ฝ)
}
public static void elseif() //๋ฉ์ธ ๋ฉ์๋์ ํธ์ถ์ ํด์ผ ๊ฒฐ๊ณผ๊ฐ์ ๋ณผ ์ ์๋ค.
{
}
Calendar c = Calendar.getInstance();
int year = c.get(c.YEAR);
int month = c.get(c.MONTH);
int day = c.get(c.DAY_OF_MONTH)+1;
int dow = c.get(c.DAY_OF_WEEK);
String sDow = null;
if(dow==1) sDow = ("์ผ");
else if(dow==2)sDow = ("์");
else if(dow==3)sDow = ("ํ");
else if(dow==4)sDow = ("์");
else if(dow==5)sDow = ("๋ชฉ");
else if(dow==6)sDow = ("๊ธ");
else if(dow==7)sDow = ("ํ ");
else sDow = "์์ผ ์ค๋ฅ";
System.out.println(year+"๋
");
System.out.println(month+"์");
System.out.println(day+"์ผ");
System.out.println(sDow +"์์ผ");
Calendar c = Calendar.getInstance();
int dow = c.get(c.DAY_OF_WEEK);
if(dow==1 || dow==7)
{
Scanner age = new Scanner(System.in);
System.out.println("20๋๋ฉด 2, 30๋๋ฉด 3์ 40๋๋ฉด 4 50๋๋ฉด 5๋ฅผ ์
๋ ฅํ์ธ์");
String agag = age.nextLine();
if(agag.equals("2") || agag.equals("3"))
{
System.out.println("์๋น์ค ์ ๊ณต");
}
else
{
System.out.println("์๋น์ค ์ ๊ณต์ด ์ด๋ ต์ต๋๋ค.");
}
}
else
{
Scanner gender = new Scanner(System.in);
System.out.println("๋จ์๋ฉด m์ ์ฌ์๋ฉดf๋ฅผ ์ ์ด์ฃผ์ธ์.");
String gdgd = gender.nextLine();
if(gdgd.equals("m"))
{
System.out.println("์๋น์ค ์ ๊ณต");
}
else
{
System.out.println("์๋น์ค ์ ๊ณต์ด ์ด๋ ต์ต๋๋ค.");
}
}
switch๋ฌธ์ ์จ์ ์์ผ ์ถ์ถํ๊ธฐ
switch(dow)๋ case์ ์์์ ์ ์๋ ค์ค๋ค. break๋ฌธ์ ์์ฐ๋ฉด ๊ฒฐ๊ตญ ๋๊น์ง ๊ฐ๋ค.
์๋ฅผ๋ค์ด ์ค๋์ด ํ์์ผ์ด๊ธฐ๋๋ฌธ์ dow๋ 3์ด๊ณ switch๋ฌธ์ case3๋ถํฐ ์์๋๋ค. ๊ทผ๋ฐ break๊ฐ ์๊ธฐ๋๋ฌธ์ case3๋ถํฐ ๊ฒฐ๊ตญ์ default๊น์ง ๊ฐ์ฌ ์ฝ์๊ฐ์ ์ ๋ ๊ฒ ๋ํ๋๊ฒ ๋๋ค ๊ทธ๋์ break๋ฌธ์ ์จ์ผํ๋ค.
๋ง์ง๋งdefault๋ ๋ง์ง๋ง์ด๊ธฐ ๋๋ฌธ์ ๋ฐ๋ก dreak๊ฐ ํ์ ์๋ค.
break๋ฌธ์ ๋ฃ์ด ์์ ํ๋ ์ ํํ๊ฒ ๋์จ๋ค.
Scanner num = new Scanner(System.in);
System.out.println("์ซ์๋ฅผ 1 ~ 10 ์ค ์๋ฌด๊ฑฐ๋ ์ ์ผ์์ค");
int x = num.nextInt();
int sum = 0;
switch(x)
{
case 1: sum= sum + 1;
case 2: sum= sum + 2;
case 3: sum= sum + 3;
case 4: sum= sum + 4;
case 5: sum= sum + 5;
case 6: sum= sum + 6;
case 7: sum= sum + 7;
case 8: sum= sum + 8;
case 9: sum= sum + 9;
case 10: sum= sum + 10;
System.out.printf( "%d , %d ~ 10๊น์ง ํฉ์ฐ์ %d์
๋๋ค. \n", x, x, sum);
}
์ฝ์ ๊ฐ:
์ซ์๋ฅผ 1 ~ 10 ์ค ์๋ฌด๊ฑฐ๋ ์ ์ผ์์ค
6
6 , 6 ~ 10๊น์ง ํฉ์ฐ์ 40์
๋๋ค.
while ๋ฐ๋ณต๋ฌธ
while() // ( ) ์์๋ true์ false ๊ฐ์ ๋ํ๋ด๋ boolean ํํ์๋ง์ด ์ฌ ์ ์๋ค.
{
(์คํ๋ฌธ)
}
//( ) ๊ฐ์ด true ํ ๊ณ์ ์คํ๋ฌธ์ ๋ฐ๋ณตํ๋ค.
int n = 0;
while(n<10)
{
System.out.println(n++);
}
System.out.println("์์คํ
์ข
๋ฃ");
}
์ฝ์ ๊ฐ:
1
2
3
4
5
6
7
8
9
์์คํ
์ข
๋ฃ
while ๋ฌธ์ ์ด์ฉํ์ฌ 1๋ถํฐ 10๊น์ง ๋ํด์ ๊ทธ ๊ฒฐ๊ณผ๋ฅผ ํ์ํด ๋ณด์ธ์.
int n = 1;
int sum = 0;
while(n <= 10)
{
//sum = sum + n++;
sum += n++;
}
System.out.println(sum);
์ฝ์ ๊ฐ: 55
while ๋ฌธ์ ์ด์ฉํ์ฌ 1~20 ์ฌ์ด์ ์ ์ ์ค์์ ์ง์๋ง ์ ํ์ ์ผ๋ก ํ์ํด๋ณด์ธ์.
int n = 2;
int x = 1;
while(x <=10)
{
System.out.println((x++)*n);
}
๋ค๋ฅธ ๋ฐฉ๋ฒ
int n = 1;
while(n<=20)
{
if( n%2==0 )
{System.out.println(n+" ");
}
n++;
}
์ฝ์ ๊ฐ:
2
4
6
8
10
12
14
26
18
20
while ๋ฌธ์ ์ด์ฉํ์ฌ 1~5 ์ฌ์ด์ ์ ์๋ฅผ ์ ๋ ฅ ๋ฐ์์ ํฉํ ๋ฆฌ์ผ ๊ฐ์ ๊ตฌํ์ธ์.
Scanner num = new Scanner(System.in);
System.out.println("์ซ์๋ฅผ 1 ~ 5 ์ค ์๋ฌด๊ฑฐ๋ ์ ์ผ์์ค");
int x = num.nextInt();
int origin = x;
int res = 1;
while(x>0)
{
res*=x;
x--; //x -= 1;(๊ฐ์ ํํ)
}
System.out.printf("%d!์ %d์ด๋ค \n", origin, res);
์ฝ์ ๊ฐ:
์ซ์๋ฅผ 1 ~ 5 ์ค ์๋ฌด๊ฑฐ๋ ์ ์ผ์์ค
5
5!์ 120์ด๋ค
ํค๋ณด๋์์ ๊ตฌ๊ตฌ๋จ ๋จ์๋ฅผ ์ ๋ ฅ ๋ฐ๊ณ ํด๋น ์์ ๊ตฌ๊ตฌ๋จ์ด ํ์ ๋๋๋ก ํ๋ค.
Scanner num = new Scanner(System.in);
System.out.println("๊ตฌ๊ตฌ๋จ ๋จ์๋ฅผ ์ ์ผ์์ค");
int x = num.nextInt();
int origin = x;
int y = 1;
while (y<10)
{
System.out.printf("%d * %d = %d \n", origin, y, x*y);
y++;
}
์ฝ์ ๊ฐ:
๊ตฌ๊ตฌ๋จ ๋จ์๋ฅผ ์ ์ผ์์ค
5
5 * 1 = 5
5 * 2 = 10
5 * 3 = 15
5 * 4 = 20
5 * 5 = 25
5 * 6 = 30
5 * 7 = 35
5 * 8 = 40
5 * 9 = 45
์ค์ ์ฝ๋ฉ์์ ๋ ๋ง์ด ์ฐ๋ ๋ฐฉ๋ฒ
Scanner num = new Scanner(System.in);
System.out.println("๊ตฌ๊ตฌ๋จ ๋จ์๋ฅผ ์ ์ผ์์ค");
int x = num.nextInt();
int origin = x;
int y = 1;
while (y<10)
{
String gugu = String.format("%d * %d = %d \n", origin, y, x*y);
System.out.printf(gugu);
y++;
}
๋๊ธ