틀린점을 알고싶습니다

chaeheum3 Reply 9 years 9 weeks ago
import java.util.Scanner; /** * Created by parkch on 9/13/15 18:52. */ public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); double a = scanner.nextDouble(); double b = scanner.nextDouble(); double c = scanner.nextDouble(); double r1, r2, dis; dis = Math.pow(b, 2) - (4 * a * c); if (dis < 0) { System.out.print("complex"); System.exit(0); } dis = Math.sqrt(dis); r1 = (-b + dis) / 2 * a; r2 = (-b - dis) / 2 * a; if(r1>r2) { System.out.printf("%.1f\n", r1); } else { System.out.printf("%.1f\n", r2); } } }
withcs Reply 9 years 9 weeks ago
버림을 하라고 했는데 %.1f를 사용하면 반올림을합니다.
chaeheum3 Reply 9 years 9 weeks ago
그런데 그전에 제출했던 버림도 같은 결과가 나와서 질문드립니다 import java.util.Scanner; /** * Created by parkch on 9/13/15 18:52. */ public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); double a = scanner.nextDouble(); double b = scanner.nextDouble(); double c = scanner.nextDouble(); double r1, r2, dis; dis = Math.pow(b, 2) - (4 * a * c); if (dis < 0) { System.out.print("complex"); System.exit(0); } dis = Math.sqrt(dis); r1 = (-b + dis) / 2 * a; r2 = (-b - dis) / 2 * a; r1 = (int) (r1 * 10) / 10f; r2 = (int) (r2 * 10) / 10f; if(r1>r2) { System.out.printf("%.1f\n", r1); } else { System.out.printf("%.1f\n", r2); } } } 위와 같이 제출하였습니다
withcs1 Reply 9 years 9 weeks ago
/2×a != /2/a