#26869: java 有人幫我抓錯嗎?一直出現程式無法正常結束


Que030 (Que)


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);

        }

 

    }

}

 

#26873: Re:java 有人幫我抓錯嗎?一直出現程式無法正常結束


cges30901 (cges30901)


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)