์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | 7 |
8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 | 16 | 17 | 18 | 19 | 20 | 21 |
22 | 23 | 24 | 25 | 26 | 27 | 28 |
29 | 30 |
- SQL
- ํจ์
- post๋ฐฉ์
- ๋์
- ๋ช ๋ น์ด
- Update
- MySQL
- ํ ์ด๋ธ
- ๋์ ํ ์ด๋ธ
- ์ปจํธ๋กค๋ฌ
- ๋์ปค
- ๋ฐฑํฑ
- JS
- DATE_FORMAT
- Spring
- oracle
- ํ๋ก๊ทธ๋๋จธ์ค
- ๋ณ์
- ๋ฆฌ๋ ์ค
- ๋ฐฐ์ด
- ์๋ฐ์คํฌ๋ฆฝํธ
- Ajax
- like
- select
- JavaScript
- ์ฝํ
- order by
- ์ธ๋ผ์ธ๋ทฐ
- optionํ๊ทธ
- JSP
- Today
- Total
bom's happy life
[JAVA] ํ์ง ๊ตฌ๋ถํ๊ธฐ ๋ณธ๋ฌธ
๋ฌธ์ ์ค๋ช :
์์ฐ์ n์ด ์ ๋ ฅ์ผ๋ก ์ฃผ์ด์ก์ ๋ ๋ง์ฝ n์ด ์ง์์ด๋ฉด "n is even"์,
ํ์์ด๋ฉด "n is odd"๋ฅผ ์ถ๋ ฅํ๋ ์ฝ๋๋ฅผ ์์ฑํด ๋ณด์ธ์.
์ ํ์ฌํญ :
๐ 1 ≤ n ≤ 1,000
์ ์ถ๋ ฅ ์ :
์ ๋ ฅ #1
100
์ถ๋ ฅ #1
100 is even
์ ๋ ฅ #2
1
์ถ๋ ฅ #2
1 is odd
๋ฌธ์
import java.util.Scanner;
public class Solution {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
}
}
๋ฐฉ๋ฒ 1
import java.util.Scanner;
public class Solution {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
if(n % 2 == 0) {
System.out.println("100 is even");
} else {
System.out.println("1 is odd");
}
}
}
์ด๋ ๊ฒ ์์ฑํ๋๋ ํ ์คํธ ์ผ์ด์ค๋ง ํต๊ณผํ๊ณ ์ ํ์ฑ ํ ์คํธ์์ ์คํจํ๋ค.
๋ฐฉ๋ฒ 2
import java.util.Scanner;
public class Solution {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
while(true) {
if(n == 0) {
System.out.println("0์ ์
๋ ฅํ์ต๋๋ค. ๋ค์ ์
๋ ฅํ์ธ์.");
n = sc.nextInt();
} else if(n % 2 == 1) {
System.out.println("1 is odd");
break;
} else if(n % 2 == 0 ) {
System.out.println("100 is even");
break;
}
}
}
}
๋ค์ while๋ฌธ์ผ๋ก ์์ฑํ๋๋ฐ ๋ 50%์ผํธ๋ง ๋ง์๋ค๊ณ ๋ค์ ํ๋ผ๊ณ ํ๋ค. ์ด๋ป๊ฒ ๋ฐ๊ฟ์ผ ํ๋๊ฑธ๊น?..?
๋ฐฉ๋ฒ 3
import java.util.Scanner;
public class Solution {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a;
String result;
String msg1= "1 is odd";
String msg2 = "100 is even";
int n = sc.nextInt();
result = (n % 2 == 1) ? msg1 : msg2;
System.out.println(result);
}
}
์ด๋ ๊ฒ 3ํญ์ฐ์ฐ์ผ๋ก ํด๋ 100% ์ ๋ต์ ์๋๋ค...
.
.
.
๋ฐฉ๋ฒ 4
import java.util.Scanner;
public class Solution {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
if(n%2==0){
System.out.println(n + " is even");
}
else{
System.out.println(n + " is odd");
}
}
}
์.. ์ด๋ ๊ฒ ์ผ์ด์ผ ํ๋ค. 100๊ณผ 1์ ๋ฐ๋ณตํด์ ์์ฑํ๋ ๊ฒ์ด ์๋๋ผ ์ ๋ ฅ๋ฐ์ ๋ณ์ n์ผ๋ก ํ๊ธฐํ๊ธฐ.
๋ฐฉ๋ฒ 5
import java.util.Scanner;
public class Solution {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
System.out.print(n + " is ");
System.out.println(n%2 == 0 ? "even" : "odd");
}
}
๋ค๋ฅธ ํ์ด ์ค์ ์ด๋ ๊ฒ ์ถ๋ ฅ์ ์ผํญ์ฐ์ฐ์ ์ฌ์ฉํ ๊ฒ์ด ์ข์๋ค.
.
.
๋ณ์๋ฅผ ์ ์ธํ๋ค๋ฉด ๋ณ์๋ก ์ฌ์ฉํด์ค์ผ ํ๋ค!
'Algorithm๐ > ํ๋ก๊ทธ๋๋จธ์ค' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[JAVA] ๋ฌธ์์ด ์๊ธฐ (0) | 2023.04.28 |
---|---|
[JAVA] ๋ฌธ์์ด ๊ฒน์ณ์ฐ๊ธฐ (0) | 2023.04.25 |
[JAVA] ๋ฌธ์์ด ๋๋ฆฌ๊ธฐ (0) | 2023.04.23 |
[JAVA] ๋ฌธ์์ด ๋ถ์ฌ์ ์ถ๋ ฅํ๊ธฐ (0) | 2023.04.22 |
[JAVA] ๋ง์ ์ ์ถ๋ ฅํ๊ธฐ (0) | 2023.04.22 |