250x250
Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
Tags
- 자바개발자
- 스프링부트
- 자바
- SpringBoot
- 자바의정석
- 데이터베이스
- 운영체제
- 자바스크립트
- 코딩
- 리액트
- 프로그래머
- Codeup
- 개발자
- 국비지원
- 프로그래밍
- 미라클모닝
- React
- 프로세스
- 알고리즘
- 혼공컴운
- 프로그래머스
- 백엔드
- 코드업
- 국비지원코딩
- 소셜로그인구현
- 자바알고리즘
- 백엔드개발자
- 개발자일기
- Java
- db
Archives
- Today
- Total
초코딩(chocoding)
[Java] 자바의 정석 / chap5.배열_연습문제 본문
728x90
5-3)
package j04_Array;
public class Ex_example {
public static void main(String[] args) {
int[] arr = {10, 20, 30, 40, 50};
int sum = 0;
for (int i = 0; i < arr.length; i++) {
sum += arr[i];
}
System.out.println(sum);
}
}
5-4)
package j04_Array;
public class Ex_example {
public static void main(String[] args) {
int[] [] arr = {
{5, 5, 5, 5, 5},
{10, 10, 10, 10, 10},
{20, 20, 20, 20, 20},
{30, 30, 30, 30, 30}
};
int total = 0;
float average = 0;
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[0].length; j++) {
total += arr[i][j];
}
}
average = (float)total / (arr.length * arr[0].length);
System.out.println("total => " + total);
System.out.println("average => " + average);
}
}
5-5)
package j04_Array;
public class Ex_example {
public static void main(String[] args) {
int[] ballArr = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
int[] ball3 = new int[3];
for (int i = 0; i < ballArr.length; i++) {
int j = (int) (Math.random() * ballArr.length);
int tmp = 0;
tmp = ballArr[i];
ballArr[i] = ballArr[j];
ballArr[j] = tmp;
}
System.arraycopy(ballArr, 0, ball3, 0, 3);
for (int i = 0; i < ball3.length; i++) {
System.out.print(ball3[i]);
}
}
}
5-6)
package j04_Array;
import java.util.Scanner;
public class Ex_example {
public static void main(String[] args) {
String[] words = {"television", "computer", "mouse", "phone"};
Scanner scanner = new Scanner(System.in);
for (int i = 0; i <words.length; i++) {
char[] question = words[i].toCharArray();
char randomNum = (char)((int)(Math.random() * question.length));
char temp = ' ';
temp = question[i];
question[i] = question[randomNum];
question[randomNum] = temp;
System.out.printf("Q%d. %s의 정답을 입력하세요. > ", i + 1, new String(question));
String answer = scanner.nextLine();
if (words[i].equals(answer.trim())) {
System.out.printf("맞았습니다. %n%n");
} else {
System.out.printf("틀렸습니다. %n%n");
}
}
}
}
..................섞여서 나오긴 하는데 다시 생각해봐야 할 것 같다.....
+ 추가)
아니 나 바보야???????????????????? 왜 randomNum을 char로 받음?????????
어이 아리마셍
package j04_Array;
import java.util.Scanner;
public class Ex_example {
public static void main(String[] args) {
String[] words = {"television", "computer", "mouse", "phone"};
Scanner scanner = new Scanner(System.in);
for (int i = 0; i < words.length; i++) {
char[] question = words[i].toCharArray();
int num = (int)(Math.random() * question.length);
char temp;
for (int j = 0; j < question.length; j++) {
temp = question[j];
question[j] = question[num];
question[num] = temp;
}
System.out.printf("Q%d. %s의 정답을 입력하세요. > ", i + 1, new String(question));
String answer = scanner.nextLine();
if (words[i].equals(answer.trim())) {
System.out.printf("맞았습니다. %n%n");
} else {
System.out.printf("틀렸습니다. %n%n");
}
}
}
}
728x90
'[Java] 자바의 정석' 카테고리의 다른 글
[Java] 자바의 정석 / chap8) 예외처리 - 연습문제 (0) | 2023.09.15 |
---|---|
[Java] 자바의 정석 / 다형성 (0) | 2023.09.11 |
[Java] 자바의 정석 / JVM 생성 순서, 상속 관계에서의 생성자 (0) | 2023.09.08 |
[Java] 자바의 정석 / 상속에 대하여 (개념정리) (0) | 2023.09.07 |
[Java] 자바의 정석 / chap6.객체지향 프로그래밍_연습문제 (0) | 2023.09.05 |