- 5개의 숫자를 랜덤 으로 받아 배열로 저장
- 5개의 숫자 중 가장 큰 값을 출력
package day_2024_07_26;
import java.util.Arrays;
public class _5_Random {
public static void main(String[] args) {
int[] randomArr = new int[5];
for (int i = 0; i < randomArr.length; i++) {
randomArr[i] = (int) (Math.random() * 100 + 1);
}
int max = randomArr[0];
for (int i = 0; i < randomArr.length; i++) {
if (max < randomArr[i])
max = randomArr[i];
}
System.out.println(Arrays.toString(randomArr));
System.out.println(max);
}
}
package _2024_07_26;
public class Array_Abc {
public static void main(String[] args) {
char[] arrAbc = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S',
'T', 'U', 'V', 'W', 'X', 'Y', 'Z' };
for (int i = 0; i < arrAbc.length; i++) {
System.out.print(arrAbc[i]+" ");
}
}
}
출력)
랜덤한 정수들 : 3 6 3 6 1 3 8 9 6 9
평균은 5.4
public class Array_Random {
public static void main(String[] args) {
int[] arrRandom = new int[10];
double sum = 0;
double avg;
for (int i = 0; i < arrRandom.length; i++) {
arrRandom[i] = (int) (Math.random() * 10 + 1);
System.out.print(arrRandom[i] + " ");
sum += i;
}
avg = sum / arrRandom.length;
System.out.println();
System.out.println("평균: " + avg);
}
}
해당 객체에 인덱스 순서대로 가로 세로 설정 -
이번에는 반드시 scanner 로 입력 받을것
해당 배열에 있는 Rectangle 의 총넓이의 합을 구하시오.
또한 아래의 함수도 만들것(static 으로 만들것)
- 파라미터를 Rectangle 배열로 받아서 해당 배열에 들어 잇는
Rectangle 들에 총 넓이를 리턴 하는 함수를 만
드시오.
package _2024_07_26;
import java.util.Scanner;
class Rectangle {
private double width, height;
public Rectangle(double w, double h) {
width = w;
height = h;
}
public double getArea() {
return width * height;
}
}
public class Array_Rect {
public static double RectAreaSum(Rectangle[] r) {
double sum = 0;
for (int i = 0; i < r.length; i++) {
sum += r[i].getArea();
}
return sum;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Rectangle[] arrRect = new Rectangle[3];
double areaSum = 0;
for (int i = 0; i < arrRect.length; i++) {
System.out.print("가로: ");
int width = sc.nextInt();
System.out.print("세로: ");
int height = sc.nextInt();
arrRect[i] = new Rectangle(width, height);
areaSum += arrRect[i].getArea();
}
System.out.println("모든 사각형의 넓이의 합: " + areaSum);
System.out.println("모든 사각형의 넓이의 합: " + RectAreaSum(arrRect));
}
}
String str = "age" + 17;
자료형이 다른 더하기 연산시 자료형의 범위가 넓은 쪽으로 자동 형변환이 됩니다.
따라서 정수형인 17을 문자열로 자동 형변환을 해서 연산을 하기 때문에 오류가 발생하지 않습니다.
int[] ar1 = new int[5];

Box[] arr = new Box[5];

화폐매수 구하기
- 1원 부터 5000 원 까지 랜덤으로 생성.
- 500원 100 원 50 원 10원은 배열로 선언 하여 저장
해당 랜덤생성된 화폐 매수를 아래와 같이 출력
출력
2860원
500원 : 5개
100원 : 3개
50원 : 1개
10원 : 1개
public class ArrayMoney {
public static void main(String[] args) {
int money = (int) (Math.random() * 5000 + 1);
int[] count = new int[4];
int moneySave = money;
count[0] = moneySave / 500;
moneySave = moneySave % 500;
count[1] = moneySave / 100;
moneySave = moneySave % 100;
count[2] = moneySave / 50;
moneySave = moneySave % 50;
count[3] = moneySave / 10;
moneySave = moneySave % 10;
System.out.println("랜덤액수: " + money + "원");
System.out.println("500원: " + count[0] + "개");
System.out.println("100원: " + count[1] + "개");
System.out.println("50원: " + count[2] + "개");
System.out.println("10원: " + count[3] + "개");
System.out.println("남은금액: " + moneySave + "원");
}
}
package _2024_07_26;
public class Array_Three {
public static void main(String[] args) {
int[] arrThree = new int[100];
for (int i = 0; i < arrThree.length; i++) {
arrThree[i] = (int) (Math.random() * 100 + 1);
if (arrThree[i] % 3 != 0) {
continue;
} else {
System.out.print(arrThree[i] + " ");
}
}
}
}