[예제 ①] 총합과 평균 구하기
public class exCode {
public static void main(String[] args) {
int sum = 0;
float average = 0f;
int[] score = {88, 100, 76, 84, 94};
for (int i = 0; i < score.length; i++) {
sum += score[i];
}
average = sum/(float)score.length;
System.out.println("총점 : " + sum);
System.out.println("평균 : " + average);
}
}
[실행 결과]
[예제 ②] 최대값, 최소값 찾기
public class exCode {
public static void main(String[] args) {
int[] score = {80, 90,95, 82, 100, 76};
int max = 0;
int min = score[0];
for(int i=0; i< score.length; i++){
if(score[i]>max){
max = score[i];
} else if (score[i]<min){
min = score[i];
}
}
System.out.println("최대값 : " + max);
System.out.println("최소값 : " + min);
}
}
[실행 결과]
[예제 ③] shuffle_카드섞기
public class exCode {
public static void main(String[] args) {
int[] nums = new int[10];
for (int i = 0; i < nums.length; i++) {
nums[i] = i;
System.out.print(nums[i]);
}
System.out.println();
//shuffle
for (int i = 0; i < 100; i++) {
int n = (int) (Math.random() * 10);
int temp = nums[0];
nums[0] = nums[n];
nums[n] = temp;
}
for (int numsPr : nums) {
System.out.print(numsPr);
}
}
}
[실행 결과]
[예제 ④] shuffle2_로또번호 생성하기
public class exCode {
public static void main(String[] args) {
int[] lotto = new int[45];
for(int i=0; i<lotto.length; i++){
lotto[i] = i+1;
}
int temp = 0;
int j = 0;
for(int i=0; i<6; i++){
j = (int)(Math.random()*45);
temp = lotto[i];
lotto[i] = lotto[j];
lotto[j] = temp;
}
for(int i=0; i<6; i++){
System.out.printf("ball[%d] = %d%n", i, lotto[i]);
}
}
}
[실행 결과]
[예제 ⑤] 임의의 값으로 배열 채우기
import java.util.Arrays;
public class exCode {
public static void main(String[] args) {
int[] code = {-4, -1, 3, 6, 11};
int[] arr = new int[10];
for(int i=0; i<arr.length; i++){
int temp = (int)(Math.random()* code.length);
arr[i] = code[temp];
}
System.out.println(Arrays.toString(arr));
}
}
[실행 결과]
[예제 ⑥] 배열 요소 정렬하기
public class exCode {
public static void main(String[] args) {
int[] nums = new int[10];
for(int i=0; i<nums.length; i++){
System.out.print(nums[i] = (int)(Math.random()*10));
}
System.out.println();
//버블 정렬 알고리즘
for(int i=0; i<nums.length-1; i++){
boolean changed = false;
for(int j=0; j<nums.length-1-i; j++){
if(nums[j] > nums[j+1]){
int temp = nums[j];
nums[j] = nums[j+1];
nums[j+1] = temp;
changed = true;
}
}
if(!changed) break;
for(int k=0; k<nums.length; k++){
System.out.print(nums[k]);
}
System.out.println();
}
}
}
[실행 결과]
[예제 ⑦] 각 배열 요소의 빈도 수 구하기
public class exCode {
public static void main(String[] args) {
int[] nums = new int[10];
int[] counter = new int[10];
for(int i=0; i<nums.length; i++){
nums[i] = (int)(Math.random()*10);
System.out.print(nums[i]);
}
System.out.println();
for(int i=0; i<nums.length; i++){
counter[nums[i]]++;
}
for(int i=0; i<nums.length; i++){
System.out.printf("%d의 개수 : %d%n", i, counter[i]);
}
}
}
[실행 결과]
[예제 ⑧] 2차원 좌표에 위치 표시하기
import java.util.Scanner;
public class exCode {
public static void main(String[] args) {
final int SIZE = 10;
int x = 0;
int y = 0;
char[][] board = new char[SIZE][SIZE];
byte[][] shipBoard = {
{0, 0, 0, 0, 0, 0, 1, 0, 0},
{1, 1, 1, 1, 0, 0, 1, 0, 0},
{0, 0, 0, 0, 0, 0, 1, 0, 0},
{0, 0, 0, 0, 0, 0, 1, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0},
{1, 1, 0, 1, 0, 0, 0, 0, 0},
{0, 0, 0, 1, 0, 0, 0, 0, 0},
{0, 0, 0, 1, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 1, 1, 1, 0},
};
for (int i = 0; i < SIZE; i++) {
board[0][i] = board[i][0] = (char) (i + '0');
}
Scanner scan = new Scanner(System.in);
while (true) {
System.out.printf("좌표를 입력하세요. (종료는 00) : ");
String input = scan.nextLine();
if (input.length() == 2) {
x = input.charAt(0) - '0';
y = input.charAt(1) - '0';
if (x == 0 && y == 0) break;
}
if (input.length() != 2 || x <= 0 || x >= SIZE || y <= 0 || y >= SIZE) {
System.out.println("잘못된 입력입니다. 다시 입력해주세요.");
continue;
}
board[x][y] = shipBoard[x - 1][y - 1] == 1 ? 'O' : 'X';
for (int i = 0; i < SIZE; i++) {
System.out.println(board[i]);
}
System.out.println();
}
}
}
[실행 결과]
[예제 ⑨] 빙고판에서 입력받은 숫자 지우기
import java.util.Scanner;
public class exCode {
public static void main(String[] args) {
final int SIZE = 5;
int x = 0, y = 0, num = 0;
int[][] bingo = new int[SIZE][SIZE];
Scanner scan = new Scanner(System.in);
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++) {
bingo[i][j] = i * SIZE + j + 1;
}
}
//shuffle
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++) {
x = (int) (Math.random() * SIZE);
y = (int) (Math.random() * SIZE);
int temp = bingo[i][j];
bingo[i][j] = bingo[x][y];
bingo[x][y] = temp;
}
}
do {
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++) {
System.out.printf("%2d ", bingo[i][j]);
}
System.out.println();
}
System.out.println();
System.out.printf("1~%d의 숫자를 입력하세요. (종료는 0) : ", SIZE * SIZE);
String temp = scan.nextLine();
num = Integer.parseInt(temp);
outer:
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++) {
if (bingo[i][j] == num) {
bingo[i][j] = 0;
break outer;
}
}
}
} while (num != 0);
}
}
[실행 결과]
[예제 ⑩] 영단어의 뜻 맞추기
import java.util.Scanner;
public class exCode {
public static void main(String[] args) {
String[][] words = {
{"apple", "사과"},
{"banana", "바나나"},
{"grape", "포도"}
};
Scanner scan = new Scanner(System.in);
for(int i=0; i< words.length; i++){
System.out.printf("Q%d. %s의 뜻은? : ", i+1, words[i][0]);
String temp = scan.nextLine();
if(temp.equals(words[i][1])){
System.out.printf("정답입니다.%n%n");
} else {
System.out.printf("틀렸습니다. 정답은 %s입니다. %n%n", words[i][1]);
}
}
}
}
[실행 결과]
'▶ JAVA > 개념정리' 카테고리의 다른 글
[Java] 11.객체지향언어(2)_클래스 (0) | 2022.03.07 |
---|---|
[Java] 10. 객체지향언어(1)_클래스 (0) | 2022.02.10 |
[Java] 8. 배열 (0) | 2022.02.04 |
[Java] 7. 반복문 (0) | 2022.02.03 |
[Java] 6. 조건문 (0) | 2022.02.02 |