java
국비 코딩 풀스택 수업 36일차 자바 첫날 셋팅 + 알고리즘 문제
비루블
2022. 8. 12. 17:49
https://adoptopenjdk.net/?variant=openjdk11&jvmVariant=hotspot
jdk 11(lts), hotspot
windows msi
https://www.eclipse.org/downloads/
사진참고
vscode extension 설치
extension Pack for Java 설치
// nodejs=>node_modules=>npm
// java =>maven
vscode에서 프로젝트 생성
view -> pallete -> java create -> maven -> maven->archetype -> 1.4 ->
com.example -> project이름 -> 파일 위치 선정
엔터 => Y
syso (println)
파일명 == 클래스명 똑같아야함 사진참고

이제 변수 선언은 데이터 형태로 해야함(int, float, String 등등등)
자바의 정석 기초편1 99페이지 조건식
app에서 함수 불러와서 실행
클래스명이 같을때는 임포트를 지우고, ctrl + space로 원하는 클래스
끌어오면 됨
알고리즘 문제 푸는 연습함.
문제를 아주아주 많이 내주심.
재밌었음
exam_if > Exam01.java
package com.example.exam_if;
import java.util.Scanner;
// 숫자2개 받아서 합을 구한다음 3, 5의 배수 판단하기
public class Exam01 {
// 변수
// 가시성 리턴값 메소드명()
public void myPrint(){
// 1. 클래스명 변수명 = new 클래스명();
Scanner scanner = new Scanner(System.in);
// 2. 기능을 사용한다. 함수를 사용한다.
System.out.print("국어 점수 입력하세요.");
int numScoreKor = scanner.nextInt();
System.out.print("수학 점수 입력하세요.");
int numScoreMath = scanner.nextInt();
scanner.close();
int sumScore = numScoreKor + numScoreMath;
if( sumScore % 3 == 0 && sumScore % 5 == 0 ) {
System.out.println("3, 5");
}
else if( sumScore % 5 == 0 ) {
System.out.println("5");
}
else if( sumScore % 3 == 0 ) {
System.out.println("3");
}
}
}
exam_for > Exam01.java
package com.example.exam_for;
import java.util.Scanner;
// 숫자 1개를 받아서 그 숫자까지 출력
public class Exam01 {
//메소드
public void myPrint(){
Scanner scanner = new Scanner(System.in);
System.out.print("숫자를 입력하세요.");
int num = scanner.nextInt();
scanner.close();
for(int i =1; i<=num; i++){
System.out.print(i);
if(i != num){
System.out.print(", ");
}
}
System.out.println();
System.out.println("------------------");
for( int i =num; i>0 ;i--){
System.out.print(i);
if(i != 1){
System.out.print(", ");
}
}
System.out.println();
System.out.println("------------------");
// 문제1) 5를 입력하면 홀수만 1, 3, 5
int num2 = 5;
for(int i = 1; i<=num2 ; i++){
if(i % 2 == 1){
System.out.print(i);
if(i != num2){
System.out.print(", ");
}
}
}
System.out.println();
System.out.println("------------------");
// 문제2) 15를 입력하면 5의 배수만 5, 10, 15
int num3 = 15;
for(int i = 1; i<=num3; i++){
if(i % 5 == 0){
System.out.print(i);
if(i != num3){
System.out.print(", ");
}
}
}
}
}
exam_for > Exam02.java
package com.example.exam_for;
public class Exam02 {
public void myPrint(){
int a = 7;
int b = 4;
for(int i = 1; i<a; i++){ // 12345
for(int j=1; j<b; j++){ // 1234
System.out.print(i+":"+j+", ");
}
System.out.println();
}
System.out.println("----------------");
// 문제1) 숫자 a=5일때
// 1
// 12
// 123
// 1234
// 12345
// for(int i = 1; i<=a; i++){
// for(int j = 0; j<i; j++){
// System.out.print(i);
// }
// System.out.println();
// }
for(int i = 1; i<=a; i++){
for(int j = 1; j<=i; j++){
System.out.print(j);
}
System.out.println();
}
System.out.println("----------------");
// 문제2) 숫자 a=5일때, a=7일때
// 1 1
// 123 123
// 12345 12345
// 123 1234567
// 1 12345
// 123
// 1
// int abc = 0;
// for(int i = 1; i<=a; i++){
// // for(int j = 1; j<=i; j++){
// // System.out.print(j);
// // }
// // System.out.println();
// abc = abc * 10 + i;
// int length = (int)(Math.log10(abc)+1);
// if(length % 2 == 1){
// System.out.println(abc);
// }
// }
// for(int i = a; i>0; i--){
// abc = abc / 100 ;
// if( abc != 0){
// System.out.println(abc);
// }
// }
int a2 = 20;
for(int i=1; i<= a2/2+1; i++) { // 1 2 3 4
for(int j=1; j<=i*2-1; j++){ // 1 3 5 7
System.out.print(j);
}
System.out.println();
}
for(int i=1; i<=a2/2; i++) {
for(int j=1; j<=(a2-1)-(i*2-1); j++){ // 6-1 6-3 6-5
System.out.print(j);
}
System.out.println();
}
}
}
exam_for > Exam02.java
Arr01.java
package com.example.exam_array;
// 1차원 배열실습
public class Arr01 {
public void myPrint(){
int[] arr = new int[]{11,22,13,44};
//arr[0]=11, arr[1]=22, arr[2]=13, arr[3]=44
for(int i=0; i<arr.length;i++) {// 0 1 2 3
System.out.print( arr[i] + 2 + ", ");
}
System.out.println();
for(int tmp : arr) { //forEach문
System.out.print(tmp + 2 + ", ");
}
// 문제1) int[] arr = new int[]{ 14, 22, 13, 44, 35};
// 3의 배수의 합 구하기
int[] arr1 = { 15, 22, 13, 44, 36};
int sum = 0;
for(int i=0;i<arr1.length;i++){
if( arr1[i] % 3 == 0 ) {
//3의 배수
sum = sum + arr1[i]; // sum += arr1[i]
}
}
System.out.println(sum);
// 문제2) int[] arr = new int[]{ 44, 22, 13, 44, 35 };
// b = 44 입력받아서 그값이 존재하는지 확인(순차검색)
int i = 0;
for(i=0; i<3; i++){
//0 1 2
}
//여기서 i의 값은?? 3
int b = 44;
for(i=0; i<arr1.length; i++) { // 0 1 2 3 4
if(arr1[i] == b){
break; //반복문 탈출
}
}
if( i < arr1.length) {
System.out.println("44는 배열에 있는 수");
}
// 문제3) int[] arr = new int[]{ 14, 22, 13, 44, 35};
// 가장 큰 숫자 구하기(최대값, 최소값)
int max = arr1[0]; //14
for(i =1;i<arr1.length;i++) {
if(arr1[i] > max) {
max = arr1[i];
}
}
System.out.println(max);
}
}
Arr02.java
package com.example.exam_array;
public class Arr02 {
public void myPrint(){
int[][] arr2 = {
{1, 3, 5, 5, 6},
{2, 4, 5, 6, 7},
{4, 5, 7, 8, 9},
};
System.out.println(arr2.length); // 3
System.out.println(arr2[0].length); // 5
for(int i = 0; i<arr2.length; i++){
for(int j = 0; j<arr2[0].length; j++){
System.out.print(arr2[i][j] + " ");
}
System.out.println();
}
System.out.println("----------------------");
// 문제1) 뒤쪽부터 출력하기
/* 6 5 5 3 1
7 6 5 4 2
9 8 7 5 4 */
int[][] arr3 = {
{6, 5, 5, 3, 1},
{7, 6, 5, 4, 2},
{9, 8, 7, 5, 4},
};
// System.out.println(arr3);
// System.out.println(arr3.length); // 3
// System.out.println(arr3[0].length); // 5
for(int i = arr3.length-1; 0<=i; i--){
for(int j = arr3[0].length-1; 0<=j; j--){
System.out.print(arr3[i][j] + " ");
}
System.out.println();
}
System.out.println("----------------------");
/*
문제2)
0 1 1 0 0 0 1 1 0 0 0 1 1 0 0
0 1 1 0 1 0 1 1 0 1 0 1 1 0 1
0 1 1 0 1 0 1 1 0 0 0 1 1 0 1
0 0 1 0 1 0 1 1 0 0 0 1 1 0 1
0 1 1 0 1 0 1 1 0 0 0 1 1 0 1
*/
int[][] arr5 = {
{0, 1, 1, 0, 0},
{0, 1, 1, 0, 1},
{0, 1, 1, 0, 1},
{0, 0, 1, 0, 1},
{0, 1, 1, 0, 1},
};
int[][] arr6 = {
{0, 1, 1, 0, 0},
{0, 1, 1, 0, 1},
{0, 1, 1, 0, 0},
{0, 1, 1, 0, 0},
{0, 1, 1, 0, 0},
};
int[][] arr7 = new int[5][5];
// System.out.println(arr5.length); // 5 0~4
// System.out.println(arr5[0].length); // 5 0~4
for(int i=0; i<arr5.length; i++){
for(int j=0; j<arr5[0].length; j++){
arr7[i][j] = arr5[i][j] + arr6[i][j];
if(arr7[i][j] > 1){
arr7[i][j] = 1;
}
}
}
for(int i = 0; i<arr7.length; i++){
for(int j = 0; j<arr7[0].length; j++){
System.out.print(arr7[i][j] + " ");
}
System.out.println();
}
}
}