2010-07-08 34 views
0

簡單的程序可以找到圓的周長,直徑和麪積。每當我運行該程序時都沒問題,只是最後在Area的值後總會有1或-1。例如,使用10的半徑,當我得到:在JOptionOutput的末尾獲取「1」

結果
圓的周長爲:62.832釐米
的圓的直徑是:20.0釐米
圓的面積是: 314.159 Centimeters1

守則如下所示:

import javax.swing.JOptionPane; 
import java.text.DecimalFormat; 

public class Circle { 

    public static void main(String[] args) 
    { 
    //Declarations 
    double radius; 
    String getRadius; 

    //Formatting 
    DecimalFormat formatter = new 
    DecimalFormat(".000"); 

    //Calculations 
    getRadius = JOptionPane.showInputDialog("Enter Circle Radius In Centimeters:"); 
    radius = Double.parseDouble(getRadius); 

    //Output 
    JOptionPane.showMessageDialog(null, "Results" + 
      "\n The circumference of the circle is: " + formatter.format(2*Math.PI*radius) + " Centimeters" + 
      "\n The diameter of the circle is: " + 2*radius + " Centimeters" + 
      "\n The area of the circle is: " + formatter.format(Math.PI*Math.pow(radius,2)) + " Centimeters" + 
      JOptionPane.INFORMATION_MESSAGE); 
    } 
} 

回答

1

要追加JOptionPane.INFORMATION_MESSAGE(恰好等於1)到你的字符串。它應該是這樣的:

JOptionPane.showMessageDialog(null, 
     "Results" + 
     "\n The circumference of the circle is: " + formatter.format(2*Math.PI*radius) + " Centimeters" + 
     "\n The diameter of the circle is: " + 2*radius + " Centimeters" + 
     "\n The area of the circle is: " + formatter.format(Math.PI*Math.pow(radius,2)) + " Centimeters", 
     "Results", 
     JOptionPane.INFORMATION_MESSAGE); 

這四個參數是父,消息,標題,messageType。之前,您意外地使用了雙參數版本(parent,message)並將messageType附加到消息中。

+0

就是這樣。我花了一秒鐘的時間才弄清楚你的四個參數是什麼意思,但後來我意識到這個逗號的作用。你幾乎讓我看到這完全不同,所以謝謝。 – Nidhoggur 2010-07-08 04:23:25