- 5개의 숫자를 랜덤 으로 받아 배열로 저장
- 5개의 숫자 중 가장 큰 값을 출력
✅ 도전
import java.util.Arrays;
public class ArrayNumMax {
public static void main(String[] args) {
int[] ranNum = new int[5];
int max = ranNum[0];
for(int i=1; i<ranNum.length; i++) {
ranNum[i] = (int)((Math.random()*45)+1);
if(max<ranNum[i]) {
max = ranNum[i];
}
}
System.out.println(Arrays.toString(ranNum));
System.out.println("최대값 :"+ max);
}
}
알파벳은 26개.
✅ 도전
import java.util.Arrays;
public class CharAZ {
public static void main(String[] args) {
char[] alphabet = new char[26];
int j = 65;
for(int i=0; i<alphabet.length;i++) {
alphabet[i] = (char)(j+i);
}
System.out.println(Arrays.toString(alphabet));
}
}
정수를 10개 저장하는 배열을 만들고 1에서 10까지 범위의 정수를 랜덤하게 생성하여 배열에 저장하라. 그리고 배열에 든 숫자들과 평균을 출력하라.
▼출력▼
랜덤한 정수들 : 3 6 3 6 1 3 8 9 6 9
평균은 5.4
✅ 도전
import java.util.Arrays;
public class ArrayNumSumAvg {
public static void main(String[] args) {
int[] ranNum = new int[10];
int sum = 0;
for(int i=0; i<ranNum.length; i++) {
ranNum[i] = (int)((Math.random()*10)+1);
}
for(int i=0; i<ranNum.length; i++) {
sum += ranNum[i];
}
double avg = (double)sum/ranNum.length;
System.out.println("랜덤 정수 : "+Arrays.toString(ranNum));
System.out.println("랜덤 정수 합계 확인용 : "+sum);
System.out.println("평균 :"+ avg);
}
}
Rectangle 을 배열로 3개 선언 해당 객체에 인덱스 순서대로 가로 세로 설정 - 이번에는 반드시 scanner 로 입력 받을것 해당 배열에 있는 Rectangle 의 총넓이의 합을 구하시오. 또한 아래의 함수도 만들것(static 으로 만들것)- 파라미터를 Rectangle 배열로 받아서 해당 배열에 들어 잇는 Rectangle 들에 총 넓이를 리턴 하는 함수를 만드시오.
✅ 도전
package day_2024_07_26;
import java.util.Scanner;
class Rectangle1{
private double width,height ;
public Rectangle1(double width,double height) {
this.width=width;
this.height=height;
}
public double getArea() {
return width*height;
}
}
public class ArrayRectangleScanner {
public static void main(String[] args) {
Rectangle1[] arrRectangle = new Rectangle1[3];
Scanner sc = new Scanner(System.in);
for ( int i = 0; i < arrRectangle.length; i++) {
System.out.print(i+1+"번 째 사격형의 가로를 입력하시오>>");
double width = sc.nextDouble();
System.out.print(i+1+"번 째 사격형의 세로를 입력하시오>>");
double height = sc.nextDouble();
arrRectangle[i] = new Rectangle1(width,height);
}
double area = 0;
for ( int i = 0; i < arrRectangle.length; i++) {
System.out.println((i+1)+"번째 사각형의 넓이 : " + arrRectangle[i].getArea());
area += arrRectangle[i].getArea();
}
System.out.println("3개의 사각형 넓이의 합은 : " + area);
}
}
String str = "age" + 17;
String str = "age: ".concat(String.valueOf(17));
- 객체 값의 변경 여부
- String 변경 불가
- StringBuilder 변경 가능
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개