티스토리 뷰
오늘은 자바 프로그래밍 기초 마지막 날이다.
공부 챕터는 조건문, 반복문, 배열 부분이다.
오늘도 과제 부분에서는 큰 어려움 없이 제출했고
문제를 제대로 읽지 않고 과제를 해서 틀린 부분을 수정했다.
알아도 문제좀 잘 읽고 문제 풀자...
배열 출력하기(toString, deepToString)
1차원 배열을 출력 할때 toString을 쓰고
다차원 배열일 때는 deepToString을 쓰면 모든 배열을 출력할 수 있어서
알고리즘 풀때 for문으로 노가다 안해도 된다.
package report4;
import java.util.Arrays;
//5-4. 2차원 배열 arr에 담긴 모든 총합과 평균을 구하는 프로그램을 완성하세요.
public class Report4_4 {
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[i].length; j++) {
total += arr[i][j];
}
}
// average = (float) total / count;
average = (float) total / (arr.length*arr[0].length);
System.out.println(Arrays.deepToString(arr));
System.out.println("total=" + total);
System.out.println("average=" + average);
} // end of main
}
// deepToString 출력 결과
[[5, 5, 5, 5, 5], [10, 10, 10, 10, 10], [20, 20, 20, 20, 20], [30, 30, 30, 30, 30]]
package report3;
public class Report3_1 {
public static void main(String[] args) {
//int형 변수 x가 10보다 크고 20보다 작을 때 true인 조건식
int x = 15;
if (x > 10 && x < 20) {
System.out.println("true1");
}
//char형 변수 ch가 공백이나 탭이 아닐 때 true인 조건식
char ch = 'a';
if (ch != ' ' && ch != '\t') {
System.out.println("true2");
}
//char형 변수 ch가 'x' 또는 'X'일 때 true인 조건식
char ch2 = 'x';
if (ch2 == 'x' || ch2 == 'X') {
System.out.println("true3");
}
//char형 변수 ch가 숫자('0'~'9')일 때 true인 조건식
char ch3 = '1';
if (ch3 >= 48 && ch3 <= 57) {
System.out.println("true4");
}
//char형 변수 ch가 영문자(대문자 또는 소문자)일 때 true인 조건식
char ch4 = 'A';
if (ch4 >= 65 && ch4 <= 90 || ch4 >= 97 && ch4 <= 122) {
System.out.println("true5");
}
//int형 변수 year가 400으로 나눠떨어지거나 또는 4로 나눠떨어지고 100으로 나눠떨어지지 않을때 true인 조건식
int year = 4;
if (year % 100 != 0 && (year % 400 == 0 || year % 4 == 0)) {
System.out.println("true6");
}
//boolean형 변수 powerOn이 false일 때 true인 조건식
boolean powerOn = false;
if (powerOn == false) {
System.out.println("true7");
}
//문자열 참조변수 str이 "yes"일 때 true인 조건식
String str = "yes";
if (str.equals("yes")) {
System.out.println("true8");
}
}
}
package report4;
import java.util.Arrays;
//5-5. 다음은 1과 9 사이의 중복되지 않은 숫자로 이루어진 3자리 숫자를 만들어내는 프로그램이다.
//코드를 완성하세요. 다만 Math.random()을 사용했기 때문에 실행 결과 예시와 다를 수 있습니다.
public class Report4_5 {
public static void main(String[] args) {
int[] ballArr = {1, 2, 3, 4, 5, 6, 7, 8, 9};
int[] ball3 = new int[3];
// 배열 ballArr의 임의의 요소를 골라서 위치를 바꾼다
for (int q = 0; q < ballArr.length; q++) {
int j = (int) (Math.random() * ballArr.length);
int tmp = ballArr[j];
int k = (int) (Math.random() * ballArr.length);
ballArr[j] = ballArr[k];
ballArr[k] = tmp;
}
// 배열 ballArr의 앞에서 3개의 수를 배열 ball3로 복사한다
// ball3 = Arrays.copyOf(ballArr, 3);
System.arraycopy(ballArr, 0, ball3, 0, 3);
for (int i = 0; i < ball3.length; i++) {
System.out.print(ball3[i]);
}
}//end of main
}
'Java' 카테고리의 다른 글
JAVA - 문자열자르기(split, substring) (0) | 2023.02.16 |
---|---|
JAVA - HashMap (0) | 2023.02.16 |
프로그래밍 기초 #1 (0) | 2023.02.13 |
JAVA - Math 클래스와 문자열의 비교 (0) | 2023.02.13 |
JAVA - 산술변환 (0) | 2023.02.13 |
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- aws
- CodeDeploy
- 오토 스케일링
- 인스턴스
- JWT
- githubactions
- EC2
- 위치의 중요성
- 시작 템플릿
- HTML
- Auto Scaling
- CICD
- Load Balancer
- 로드 밸런서
- 로드밸런서
- script
- flask
- java
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
글 보관함