Notice
Recent Posts
Recent Comments
Link
«   2025/02   »
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
Tags
more
Archives
Today
Total
관리 메뉴

Chihiro's Blog

516 : 입력 - 자가진단8 본문

과제/정올_JAVA

516 : 입력 - 자가진단8

ChihiroVega 2019. 5. 27. 13:19

오답 : 반올림 방법을 모름

해결 방법

Math.round(); 함수를 이용하거나 String.format(); 함수를 이용한다.

 

import java.util.Scanner;

public class Main08 {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		double a = sc.nextDouble();
		double b = sc.nextDouble();
		char c = sc.next().charAt(0); // char 및 sc.next().charAt() 사용
		
		System.out.println(String.format("%.2f", a)); 
        // String.fomat() 이용해보기 / 소숫점 둘째자리 까지 올리려면 %.2f 이런 식으로 입력
		System.out.println(String.format("%.2f", b));						  
        // %.nf -> n번째 자리까지 반올림
		System.out.println(c);
		
		sc.close();
		
	} // end main

} // end class Main08

String.format(); 함수를 이용해 보았는데,

이 함수 사용 방법은 이러하다.

System.out.println(String.format("%.nf", 함수));

여기서 n은 소숫점 자리 수를 입력하면 된다.

 

성공 결과물