题解 | #计算一元二次方程#
计算一元二次方程
https://www.nowcoder.com/practice/7da524bb452441b2af7e64545c38dc26
import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); // 注意 hasNext 和 hasNextLine 的区别 double a = 0; double b = 0; double c = 0; while (in.hasNextDouble()) { // 注意 while 处理多个 case a = in.nextDouble(); b = in.nextDouble(); c = in.nextDouble(); if (a == 0) { System.out.println("Not quadratic equation"); } else { double delta = b * b - 4 * a * c; double x1 = (-1 * b / 2 / a) + Math.sqrt(delta) / (2 * a); double x2 = (-1 * b / 2 / a) - Math.sqrt(delta) / (2 * a); double shibu = ((-1 * b) / 2 / a); double xubu = Math.sqrt(-1 * delta) / (2 * a); if (delta == 0) { System.out.printf("x1=x2=%.2f", x1); } else if(delta > 0){ if (x1 <= x2) { System.out.printf("x1=%.2f;x2=%.2f", x1, x2); } else { System.out.printf("x1=%.2f;x2=%.2f", x2, x1); } } else { System.out.printf("x1=%.2f-%.2fi;x2=%.2f+%.2fi", shibu, xubu, shibu, xubu); } } System.out.println(); } } }
Java基础练习题 文章被收录于专栏
都是一些基础的语法题目,每天可以刷几道。