์ํ ์์ ๋ฌธ์
2023. 2. 2. 17:23ใjava
1. ๋ก๋ ๋ฒํธ ์์ฑ๊ธฐ
public class Lotto {
public static void main(String[] args) {
int[] lotto = new int[6];
for(int i = 0; i < lotto.length ; i++){
lotto[i] = (int)(Math.random() * 45 + 1);
}
for(int e : lotto){
System.out.print(e + " ");
}
}
}
1.1 ๋ก๋ ์ค๋ณต ์ ๊ฑฐ
public class Lotto {
public static void main(String[] args) {
int[] lotto = new int[6];
for(int i = 0; i < lotto.length ; i++){
lotto[i] = (int)(Math.random() * 45 + 1);
//์ค๋ณต ์ ๊ฑฐ
for(int j = 0; j < i ; j++){
if(lotto[i] == lotto[j]){
i--;
break;
}
}
}
for(int e : lotto){
System.out.print(e + " ");
}
}
}
2. ์ธ ์ ์ค์บ๋๋ก ์ ๋ ฅ๋ฐ์ ํฐ ์์๋๋ก ๋์ด (if๋ฌธ ๋น๊ต ์กฐ๊ฑด 3๊ฐ์ง ๋ค์ด๊ฐ์ผ ํจ)
import java.util.Scanner;
public class ๋ฌธ์ 09 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a, b, c;
System.out.println("-์ถ๋ ฅ์์-");
System.out.print("์ฒซ๋ฒ์งธ ์ : ");
a = sc.nextInt();
System.out.print("๋๋ฒ์งธ ์ : ");
b = sc.nextInt();
System.out.print("์ธ๋ฒ์งธ ์ : ");
c = sc.nextInt();
if(a > b && a > c && b > c) {
System.out.println(a + " > " + b + " > " + c);
} else if(a > b && a > c && b < c) {
System.out.println(a + " > " + c + " > " + b);
}if(b > a && b > c && a > c) {
System.out.println(b + " > " + a + " > " + c);
} else if(b > a && b > c && c > a) {
System.out.println(b + " > " + c + " > " + a);
}
if(c > a && c > b && a > b) {
System.out.println(c + " > " + a + " > " + b);
} else if(c > a && c > b && b > a) {
System.out.println(c + " > " + b + " > " + a);
}
}
}
3. ๋ฐฐ์ด์์ ์ต๋๊ฐ ์ต์๊ฐ ๊ตฌํ๊ธฐ
public class MaxMin {
public static void main(String[] args) {
int[] array = new int[10];
for(int i = 0; i < array.length ; i++){
array[i] = (int)(Math.random() * 100 + 1);
}
//๋ฐฐ์ด์ ์ ์ฅ๋ ๋ชจ๋ ์ ์ ์ถ๋ ฅ
for (int e : array){
System.out.print(e + " ");
}
System.out.println();
//๋ฐฐ์ด์ ์ ์ฅ๋ ์ ์ค ์ต์๊ฐ
int min = array[0];
for(int i = 0; i < array.length ; i++){
if(array[i] < min) {
min = array[i];
}
}
System.out.println("์ต์๊ฐ : " + min);
//๋ฐฐ์ด์ ์ ์ฅ๋ ์ ์ค ์ต๋๊ฐ
int max = array[0];
for(int i = 0; i < array.length ; i++){
if(array[i] > max){
max = array[i];
}
}
System.out.println("์ต๋๊ฐ : " + max);
}
}
3.1
์ต๋๊ฐ ์ต์๊ฐ ๋ฉ์๋ ๋ง๋ค์ด ๋ฆฌํด
public class Max {
public static void main(String[] args) {
int[] arr = {10, 150, 50, 250, 70};
System.out.println(getMaxFromArr(arr));
System.out.println(getMinFromArr(arr));
}
public static int getMaxFromArr(int[] arr){
int max = arr[0];
for(int i = 0; i < arr.length ; i++){
if(arr[i] > max){
max = arr[i];
}
}
return max;
}
public static int getMinFromArr(int[] arr){
int min = arr[0];
for(int i = 0; i < arr.length; i++){
if(arr[i] < min){
min = arr[i];
}
}
return min;
}
}
4.ํด๋์ค๋ก ๋ฉ์๋ ๋ง๋ค๊ณ ์คํ.
๋ฉ์๋ ๋ง๋ค ๋ static ๋ถ์ด ์๋ ๊ฒฝ์ฐ๋ main ๋ฉ์๋ ๋ฃ์ ํด๋์ค์์ ๋ฉ์๋ ๋ง๋๋ ๊ฒฝ์ฐ๋ง static ์ ์ด์ผ ํจ.
๋ฉ์ธ ๋ฉ์๋์ static์ด ์ ํ ์์ด์ ๋์ผํ๊ฒ ๋ง์ถ๋ ๊ฒ!
์ฐ ์ค๊ณ๋ ํด๋์ค ๋ง๋ค ๋๋ public void ๋ฉ์๋๋ช ๋ง ์ ์ด๋ ๋จ.
** ์ค๋ณต ๊ฐ ์์ ๋ ์ฝ๋ ์๊ณ ์์ผ๋ฉด ๋ง์ง๋ง ๋ฌธ์ ํ๊ธฐ ์กฐ๊ธ ๊ด์ฐฎ์.
'java' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
wrapper class (0) | 2023.02.03 |
---|---|
โ ์์ธ์ฒ๋ฆฌ (0) | 2023.02.03 |
์ธํฐํ์ด์ค ๋ฌธ์ ํ๊ธฐ (0) | 2023.02.02 |
Interface(์ธํฐํ์ด์ค) (0) | 2023.02.02 |
String ํด๋์ค (0) | 2023.02.02 |