package que;
import java.util.Scanner;
public class a006 {
public static void main(String[] age) {
Scanner sc = new Scanner(System.in);
int a, b, c, d;
a = sc.nextInt();
b = sc.nextInt();
c = sc.nextInt();
d = b * b - 4 * a * c;
if (d < 0) {
System.out.println("No real root");
} else if (d == 0) {
System.out.println("Two same roots x=" + - b / (2 * a));
} else {
double xa = (Math.sqrt(d) - b) / 2 / a, xb = -(Math.sqrt(d) - b) / 2 / a;
System.out.printf("Two diffent roots x1=%d x2=%d", xa, xb);
}
}
}
package que;
import java.util.Scanner;
public class a006 {
public static void main(String[] age) {
Scanner sc = new Scanner(System.in);
int a, b, c, d;
a = sc.nextInt();
b = sc.nextInt();
c = sc.nextInt();
d = b * b - 4 * a * c;
if (d < 0) {
System.out.println("No real root");
} else if (d == 0) {
System.out.println("Two same roots x=" + - b / (2 * a));
} else {
double xa = (Math.sqrt(d) - b) / 2 / a, xb = -(Math.sqrt(d) - b) / 2 / a;
System.out.printf("Two diffent roots x1=%d x2=%d", xa, xb);
}
}
}
你的xa, xb是double,printf裡面用的是%f。
(不過輸出說明說答案均為整數,所以你xa, xb要用int)