# 리뷰: 두 점 사이의 거리 #### CODE <a class='btn btn-default' href='/codes/31845'>Link</a> ``` public class PointTest { public static void main(String[] args) { // 객체 생성 Point p1 = new Point(0, 0); Point p2 = new Point(3, 4); // 거리 계산 double dist = Point.distance(p1, p2); // 결과 출력 System.out.printf("두 점 A%s, B%s 사이의 거리: %.2f", p1.toStr(), p2.toStr(), dist); } } class Point { int x; int y; Point(int cx, int cy){ x = cx; y = cy; } String toStr(){ System.out.printf("(%d,%d)",x,y); } static double distance(Point p, Point t){ double result = 0.0; result = (p.x - t.x)*(p.x - t.x)+(p.y - t.y)*(p.y - t.y); result = Math.sqrt(result); return result; } } ``` #### INPUT ``` ``` #### OUPUT ``` /root/var/tmp/2020_09_11_04_24_39_b18e8ae0/PointTest.java:27: error: missing return statement } ^ 1 error ```
## 메서드 리턴 String toStr() { System.out.printf("(%d,%d)",x,y); } toStr 메서드가 String 타입을 리턴하는데 위 코드에는 값을 반환해주겠다는 return 문이 들어가 있지 않습니다. 또한 printf 메소드의 경우 값을 반환하지 않기 때문에 return 뒤에 사용할수 없습니다. 따라서 String.format 메소드를 통해 문자열을 만들어 반환해줘야 합니다. String toStr() { return String.format("(%d, %d)", x, y); }