자바 기초

미니멀리즘·2022년 10월 19일

Class 만들기

-클래스명 규칙 = 영단어(소문자) + 영단어(첫글자 대문자) + ... + 영단어(첫글자 대문자)
-단축어 sout = System.out.println();

변수(variable)

-변수의 선언 (변수형) (변수명) = 초기값;

문자 형 = boolean
논리 형 = char(acter)
정수 형 = byte, short, int, long
실수 형 = float, double

int A * int B = int C
int C => 계산결과 int 값의 범위를 오버하는 것에 주의하자

int firstNumber = 5;
System.out.println(firstNumber);
System.out.println("firstNumber");

int secondNumber = 12;
int thirdNumber = 35;
System.out.println(firstNumber + secondNumber + thirdNumber);
int total = firstNumber + secondNumber + thirdNumber;
System.out.println(total);

int firstValue = 10000;
int firstMinIntValue = Integer.MIN_VALUE;
int firstMaxIntValue = Integer.MAX_VALUE;
System.out.println("MinIntValue =" + firstMinIntValue);
System.out.println("MaxIntValue =" + firstMaxIntValue);

byte firstMinByteValue = Byte.MIN_VALUE;
byte firstMaxByteValue = Byte.MAX_VALUE;
System.out.println("MinByteValue =" + firstMinByteValue);
System.out.println("MaxByteValue =" + firstMaxByteValue);

short firstMinShortValue = Short.MIN_VALUE;
short firstMaxShortValue = Short.MAX_VALUE;
System.out.println("MinShortValue=" + firstMinShortValue);
System.out.println("MaxShortValue=" + firstMaxShortValue);

long firstMinLongValue = Long.MIN_VALUE;
long firstMaxLongValue = Long.MAX_VALUE;
System.out.println("MinLongValue=" + firstMinLongValue);
System.out.println("MaxLongValue=" + firstMaxLongValue);

float firstMinFloatValue = Float.MIN_VALUE;
float firstMaxFloatValue = Float.MAX_VALUE;
System.out.println("MinFloatValue=" + firstMinFloatValue);
System.out.println("MaxFloatValue=" + firstMaxFloatValue);

double firstMinDoubleValue = Double.MIN_VALUE;
double firstMaxDoubleValue = Double.MAX_VALUE;
System.out.println("MinFloatValue=" + firstMinDoubleValue);
System.out.println("MaxFloatValue=" + firstMaxDoubleValue);

int x =10;
int y= 8;
System.out.println("x > y = ? " + (x > y));
System.out.println("x < y = ? " + (x < y));

boolean isJaveFun = true;
boolean isFishTasty = false;
int x1 = 10;
int y1 = 8;
System.out.println(x1 > y1);
if (x1 > y1 == isJaveFun){
    System.out.println("That's great!!");
}else if (x1 > y1 == isFishTasty){
    System.out.println("ummmm....");
}

축약형(복합형) 대입 연산자

a = a + 1;
a++; 같은 결과

b = b + c;
b += c; 같은 결과

x = x / y;
x /= y; 같은 결과

int a = 1;
int b = a+1;
System.out.println("a = " + a + " " +"b =" + b);
int c = a + b;
int d = a + b;
System.out.println("a+b 결과 = " + c +" "+ d);
c = c -1;
d--;
System.out.println("-1 결과 = " + c +" "+ d);
c = c * 10;
d *= 10;
System.out.println("*10 결과 = " + c +" "+ d);
c = c / 5;
d /= 5;
System.out.println("/5 결과 = " + c +" "+ d);
c = c % 3 ;
d %= 3;
System.out.println("%3 결과 = " + c +" "+ d);

int e = 0;
System.out.println(e);
e++;
System.out.println(e);

논리 연산자

&& and
|| or

내장함수

Math.random()
-외부 함수는 호출 해서 사용

int a = 5, b = 10;
int thirdNumber = 65;
double fourNumber = -4.7;

System.out.println(Math.max(a, b));
System.out.println(Math.min(a, b));

long resultMath1 = Math.max(a, b);
long resultMath2 = Math.min(a, b);
System.out.println(resultMath1);
System.out.println(resultMath2);
System.out.println(Math.sqrt(thirdNumber));
System.out.println(Math.abs(fourNumber));
System.out.println(Math.random());

double randomFirst = Math.random();
System.out.println("randomFirst " + randomFirst);
int randomSecond = (int) randomFirst;
System.out.println("randomSecond " + randomSecond);

double randomGift = Math.random() * 10;
int gift = (int) randomGift;

int giveaway = (int) (Math.random() * 10);
System.out.println("giveaway=" + giveaway);

System.out.println(randomGift);
System.out.println(gift);

//     if (gift >= 1 && gift <= 3) {
if (gift == 1 || gift==2 || gift== 3){
    System.out.println("10점입니다.");
} else if (gift >= 4 && gift <= 6) {
    System.out.println("50점입니다.");
} else if (gift >= 7 && gift <= 9){
    System.out.println("80점입니다.");
} else { // if( gift == 0){
    System.out.println("100점입니다!!!!!");
}

조건문

if (조건){
	(실행문);
}else if(조건){
	(실행문);
}else
	(실행문);
    
switch (today) {
	case 6: case 7:
		System.out.println("주말입니다");
		break;
	default : //case 1: case 2: case 3: case 4: case 5:
		System.out.println("주말이 아닙니다");
		break;
	}
    

if - else if - else
switch - case: - break - default

if문

 int a = 60;
 if(a>90){
     System.out.println("A학점입니다.");
 }else if(a>80){
    System.out.println("B학점입니다.");
}else if(a>70){
    System.out.println("C학점입니다");
}else{
    System.out.println("D학점입니다.");
}

int topScore = 98;
if (topScore > 90){
    System.out.println("excellent");
}

int secondTopScore = 81;
if (topScore > secondTopScore && topScore < 100){
    System.out.println("100점이 아닙니다");
}
//or은 ||이고, and는 &&이다.

int myScore = 71;
if (myScore > 90) {
    System.out.println("A입니다");
} else if (myScore > 80) {
    System.out.println("B입니다.");
} else if (myScore > 70) {
    System.out.println("C입니다.");
} else
System.out.println("D입니다");

//=, == 차이점
boolean isAlien = true;
//boolean isAlien = false;
if (isAlien == false)   {
    System.out.println("It is not an alien");
}
else{
    System.out.println("And I am an aliens");
}

int a = 81;
if(100>=a && a>90){
    System.out.println("A학점입니다");
}else if(90>=a && a>80){
    System.out.println("B학점입니다");
}else if(80>=a && a>70){
    System.out.println("C학점입니다");
}else{
    System.out.println("D학점입니다");
}

int t = 18;

if(t > 0 && t <= 12){
    System.out.println("Good morning");
}else if(t > 12 && t <= 18){
    System.out.println("Good afternoon");
}else if(t > 18 && t <= 24){
    System.out.println("Good evening");
}

int today = 5;
switch (today) {
    case 1:
        System.out.println("Monday");
        break;
    case 2:
        System.out.println("Tuesday");
        break;
    case 3:
        System.out.println("Wednesday");
        break;
    case 4:
        System.out.println("Thursday");
        break;
    case 5:
        System.out.println("Friday");
        break;
    case 6:
        System.out.println("Saturday");
        break;
    case 7:
        System.out.println("sunday");
        break;
}

if (today == 6 || today ==7){
    System.out.println("주말이다~~~");
}else{
    System.out.println("주말을 기다리십쇼");
}

switch (today) {
    case 6: case 7:
        System.out.println("주말입니다");
        break;
//            case 1: case 2: case 3: case 4: case 5:
    default :
        System.out.println("주말이 아닙니다");
        break;
}

반복문

단순 작업의 자동화

int t = 0;
while(t < 6){
	System.out.println("종이접기 " + t + "번");
	t++;
}

for (int i = 0; i <6; i++){
	if( i == 4){
		break; //continue;
	}
	System.out.println("종이접기 " + i + "번째");
}
int t = 0;
while(t < 6){
//            if( t == 4){
//                break;
//            }
    System.out.println("while 종이접기 " + t + "번");
    t++;
}

for (int i = 0; i <6; i++){
//            if( i == 4){
//                break; //continue;
//            }
    System.out.println("for 종이접기 " + i + "번째");
}
}

public static class arrayEx {
public static void main(String[] args) {

    String name = "민수";
    System.out.println(name);
    String[] myFamily = {"1", "2", "3", "4"};
//        System.out.println(myFamily); //[Ljava.lang.String;@58ceff1
    System.out.println(myFamily[2]);

    for (int i = 0; i < myFamily.length; i++) {
        System.out.println(myFamily[i]);
    }

    int t = 0;
    while (t < myFamily.length) {
        System.out.println(myFamily[t]);
        t++;
    }
}

문자열

char = 기본 자료형
String = 문자열

String[] myN = {"1", "2", "3", "4"};
for (int i = 0; i < myN.length; i++) {
	System.out.println(myN[i]);
}

형변환

String firstS = String.valueOf(firstC);

int myInt = 9;
double myDouble = myInt;

작은 크기에서 큰크기로 변경가능

double secondDouble = 97891234567890123456.7d;
	System.out.println(secondDouble + " double");
    
// int secondInt = secondDouble; //error
int secondInt = (int) secondDouble;
	System.out.println(secondInt + " int");

큰크기에서 작은 크기로는 형변환을 해주어야함

기본 자료형 ↔ 기본 자료형 - 변환 할떄 앞에 수식어 붙이기

char → string (Java 버전 마다 다름)

char firstC = 'E';
String firstS = String.valueOf(firstC);

string → char (Java 버전 마다 다름)

char myChar = 'D';
char myUnicodeChar = '\u0044';
char yourUnicodeChar = '\u0050';
System.out.println(myChar);
System.out.println(myUnicodeChar);
System.out.println(yourUnicodeChar);
char myCopyrightChar = '\u00A9';
System.out.println(myCopyrightChar);
char myUn = '\u0DF4';
System.out.println(myUn);



boolean myTureBooleanVaule = true;
boolean mtFalseBooleanVaule = false;

boolean inCustomerOverTwentyOne = true;
System.out.println(inCustomerOverTwentyOne);

char firstC = 'E';
char secondC = 'T';
System.out.println(firstC);
System.out.println(secondC);
System.out.println(firstC + 0);
System.out.println(firstC);
System.out.println(firstC + secondC);

String firstS = String.valueOf(firstC);
String secondS = String.valueOf(secondC);
System.out.println(firstS);
System.out.println(secondS);
System.out.println(firstS + secondS);
System.out.println("");

int myInt = 9;
double myDouble = myInt;

System.out.println(myInt);
System.out.println(myDouble);

double secondDouble = 9.77d;
System.out.println(secondDouble + " double");
// int secondInt = secondDouble; //error
int secondInt = (int) secondDouble;
System.out.println(secondInt + " int");

다차원 배열

int[][] myNumbers = {{1, 2, 3, 4}, {5, 6, 7}};

for(int a = 0; a < myNumbers.length; a++){
	for(int b = 0; b<myNumbers[a].length; b++){
		System.out.print(myNumbers[a][b] + " ");
	}
}
String name = "ms";
System.out.println(name);
String[] myFamily = {"1", "2", "3", "4"};
//        System.out.println(myFamily); //[Ljava.lang.String;@58ceff1
System.out.println(myFamily[2]);

for (int i = 0; i < myFamily.length; i++) {
    System.out.println(myFamily[i]);
}

int t = 0;
while (t < myFamily.length) {
    System.out.println(myFamily[t]);
    t++;
}

/// 4가지 과일 종류를 변수에 넣고 모두를 출력해 보세요.
// while과 for를 활용하세요

String[] fruit = {"사과", "귤", "배", "포도"};
for (int i = 0; i < fruit.length; i++) {
//            if (i == 3) {
//                break;
//            }
    System.out.println(fruit[i]);
}

int j = 0;
while (j < fruit.length) {
//            if (j == 3) {
//                break;
//            }
    System.out.println(fruit[j]);
    j++;
}

//if와 switch를 활용하세요. 날짜를 변수에 넣고,
//그 날짜가 짝수이면 "짝수날", 홀수이면 "홀수 날"이라고
//출력되도록 하세요

int today = 22;
int number = 2;

//System.out.println(today % number);
if (today % number == 0) {
    System.out.println("짝수날");
} else {
    System.out.println("홀수날");
}

switch (today % number) {
    case 0:
        System.out.println("짝수날");
        break;
    case 1:
        System.out.println("홀수날");
        break;
}

//다차원 배열
int[][] myNumbers = {{1, 2, 3, 4}, {5, 6, 7}};
int x = myNumbers[1][2];
int y = myNumbers[0][2];
System.out.println(x);  //outputs 7
System.out.println(y); //outputs 3


//위 다차원 배열의 모든 숫자가 하나씩 출력 되도록 하세요.
System.out.println("다차원배열");
//        for(int a = 0; a < 2; a++){
//            for(int b = 0; b < 4; b++){
//                System.out.println(myNumbers[a][b]);
//            }
//        }
//            1 2 3 4
//            5 6 7

for (int a = 0; a < myNumbers.length; a++) {
    for (int b = 0; b < myNumbers[a].length; b++) {
        System.out.println(a + "" + b + "_" + myNumbers[a][b] + " ");
    }
    System.out.print("\n");
}
//아래 배얄에 있는 숫자 1~10까지 출력 되도록 하세요.
int[][] threeArrays = {{1,2,3,4},{5,6,7},{8,9,10}};
for(int ii=0; ii< threeArrays.length ; ii++){
    for(int jj=0; jj< threeArrays[ii].length; jj++){
        System.out.print(threeArrays[ii][jj]+" ");
    }
}
profile
웹 개발자

0개의 댓글