JAVA

8일차//[java] 두점 간의 거리를 구하라 (메소드 사용)

aesup 2021. 1. 21. 00:39
728x90
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
public class report {
    static double getDistance(int x, int y, int x1, int y1) {
        
        //1.두점의 거리를 구해라
        //제곱 함수 Math.pow(밑, 지수)
        //루트 함수Math.sqrt()
        int a, b;
        
        a = (int) Math.pow(x1-x, 2);
        b = (int) Math.pow(y1-y, 2);
        int result = (int) Math.sqrt(a+b);
        
        return result;
    }
    
        
 
        
    
    //class 끝
    public static void main(String[] args) {
        
        System.out.println(getDistance(1,1,2,2));
    
        
        
        }
}    
 
// 출력 1.0        
cs

 

math.pow()는 double  을 받기때문에 나중에 할때는 바꿔서 쓴다.

728x90