JAVA

CodeTree 2일차

challnum 2024. 5. 7. 15:02
public class Main {
    public static void main (String args[]) {
        double a = 33.567268;
        System.out.printf("%.4f", a);
    }
}
출력 결과
33.5673​
public class Main {
    public static void main (String args[]) {
        int a = 5;
        System.out.println("A is " + a);

        String b = "apple";
        System.out.println("B is " + b);

        System.out.println("A is " + a + " and B is " + b);
    }
}
출력 결과
A is 5
B is apple
A is 5 and B is apple

변수 선언

public class Main {
    public static void main (String args[]) {
        int a = 5;
        System.out.printf("A is %d\n", a);

        String b = "apple";
        System.out.printf("B is %s\n", b);

        System.out.printf("A is %d and B is %s\n", a, b);
    }
}

출력 결과
A is 5
B is apple
A is 5 and B is apple

 

변수 포맷의 경우 문자열의 경우 %s , 문자의 경우 %c , 정수의 경우 %d , 실수의 경우 %f이다.

 

System.out.println과 System.out.printf를 활용한 예시

 

소수점 출력하기

 

public class Main {
    public static void main (String args[]) {
        double a = 33.567268;
        System.out.printf("%.4f", a);
    }
}
출력 결과
33.5673

 

변수 값 변경하기

public class Main {
    public static void main (String args[]) {
        int a = 5;
        System.out.println("A is " + a);

        a = 3;
        System.out.println("A is " + a);
    }
}
출력 결과
A is 5
A is 3

'JAVA' 카테고리의 다른 글

강한 결합과 약한 결합의 차이는 뭘까?  (0) 2024.06.10
CodeTree 3일차  (0) 2024.05.08
이것이 자바다 7강  (0) 2023.01.13
이것이 Java다 2강  (0) 2023.01.03
이것이 Java다 1강  (0) 2023.01.03