// Array (배열)
public class Ex5_1 {
public static void main(String[] args) {
// int[] score; // 1. 배열 score을 선언(참조변수)
// score = new int[5]; // 2. 배열의 생성(int 저장공간 x 5)
int[] score = new int[5]; // 배열의 선언과 생성을 동시에
score[3] = 100;
System.out.println("score[0]=" + score[0]);
System.out.println("score[1]=" + score[1]);
System.out.println("score[2]=" + score[2]);
System.out.println("score[3]=" + score[3]);
System.out.println("score[4]=" + score[4]);
int value = 100;
System.out.println("value=" + value);
}
}
public class Ex5_2 {
public static void main(String[] args) {
// index 범위 : 0~9
int[] arr = new int[9]; // 길이가 5인 Int배열 arr을 생성
System.out.println("arr.length=" + arr.length);
for(int i =0; i < arr.length; i++) {
System.out.println("arr["+i+"]=" + arr[i]);
}
}
}
import java.util.Arrays;
public class Ex5_3 {
public static void main(String[] args) {
int[] iArr = {100, 95, 80, 70, 60};
System.out.println(iArr); // [I@1431****] 같은 형식의 문자열이 출력됌.
for(int i=0; i<iArr.length; i++) {
System.out.println(iArr[i]);
}
System.out.println(Arrays.toString(iArr));
}
}
public class Ex5_4 {
public static void main(String[] args) {
int sum = 0; // 총점을 저장하기 위환 변수
float average = 0f; // 평균을 저장하기 위한 변수
int[] score = {100, 88, 100, 100, 90};
// sum += score[0];
// sum += score[1];
// sum += score[2];
// sum += score[3];
// sum += score[4];
for(int i =0; i < score.length; i++) {
sum += score[i];
}
average = sum / (float)score.length; // 계산결과를 float
System.out.println("총점 : " + sum);
System.out.println("평균 : " + average);
}
}
import java.util.Arrays;
public class Ex5_5 {
public static void main(String[] args) {
int[] numArr = {0,1,2,3,4,5,6,7,8,9};
System.out.println(numArr);
for(int i =0; i < numArr.length; i++) {
int n = (int)(Math.random() * 10);
int tmp = numArr[i];
numArr[i] = numArr[n];
numArr[n] = tmp;
System.out.println(Arrays.toString(numArr));
}
}
}
import java.util.Arrays;
public class Ex5_6 {
public static void main(String[] args) {
// index : 0~45-1, 0~44
int[] ball = new int[45]; // 45개의 정수값을 저장하
// 배열의 각 요소에 1~45의 값을 저장한다.
for(int i =0; i < ball.length; i++) {
ball[i] = i + 1; // ball[0]에 1이 저장된다.
System.out.println(Arrays.toString(ball));
}
int tmp = 0; // 두 값을 바꾸는데 사용할 임시 변
int j = 0; // 임의의 값을 얻어서 저장할 변
// 배열의 i번째 요소와 임의의 요소에 저장된 값을 서로 바꿔서 값을 저장
// 0번째 부터 5번째 요소까지 모두 6개만 바꾼
for(int i=0; i < 6; i++) {
j = (int)(Math.random() * 45); // 0~4범위의 임의의 숫자
tmp = ball[i];
ball[i] = ball[j];
ball[j] = tmp;
System.out.println(Arrays.toString(ball));
}
// 배열 ball의 앞에서 부터 6개의 요소를 출력한다.
for(int i=0; i < 6; i++) {
System.out.printf("ball[%d]=%d%n", i, ball[i]);
}
}
}
import java.util.Arrays;
public class Ex5_7 {
public static void main(String[] args) {
String[] strArr = {"가위", "바위", "보"};
System.out.println(Arrays.toString(strArr));
for(int i=0; i< 10; i++) {
int tmp = (int)(Math.random()*3);
System.out.println(strArr[tmp]);
}
}
}
public class Ex5_8 {
public static void main(String[] args) {
int[][] score = {
{100, 100, 100}
, {20, 20, 20}
, {30, 30, 30}
, {40, 40, 40}
};
int sum = 0;
for (int i=0; i < score.length; i++) {
for(int j=0; j < 3; j++) {
System.out.printf("score[%d][%d]=%d%n", i, j, score[i][j]);
sum += score[i][j];
}
}
System.out.println("sum=" + sum);
}
}
public class Ex5_9 {
public static void main(String[] args) {
int[][] score = {
{ 100, 100, 100 }
, { 20, 20 , 20 }
, { 30, 30 , 30 }
, { 40, 40 , 40 }
};
int korTotal = 0 , engTotal = 0 , mathTotal =0;
System.out.println("번호 국어 영어 수학 총점 평균");
System.out.println("======================");
for(int i=0; i < score.length; i++) {
int sum = 0; // 개인별 총점
float avg = 0.0f; // 개인별 평균
korTotal += score[i][0];
engTotal += score[i][1];
mathTotal += score[i][2];
System.out.printf("%3d", i+1);
for(int j=0; j < score.length; j++) {
sum += score[i][j];
System.out.printf("%5d", score[i][j]);
}
avg = sum/(float) score[i].length; // 평
}
}
}
import java.util.Scanner;
public class Ex5_10 {
public static void main(String[] args) {
String[][] words = {
{"chair","의자"},
{"computer","컴퓨터"},
{"integer","정수"}
};
Scanner sc = new Scanner(System.in);
for(int i=0; i < words.length; i++) {
System.out.printf("Q%d. %s의 뜻은?", i+1, "chair");
String tmp = sc.nextLine();
if(tmp.equals(words[i][1])) {
System.out.printf("정답입니다.%n%n");
} else {
System.out.printf("틀렸습니다. 정답은 %s입니다.%n%n", words[i]);
}
}
}
}