开发者

Getting a "1" at the end of JOptionOutput

Simple program to just find the circumference, diameter, and area of a circle. Whenever I run the program it's fine, just at the end there's always a 1 or -1 after the value for Area. For example, when using a radius of 10 I get:

Results

The circumference of the circle is: 62.832 Centimeters

The diameter of the circle is: 20.0 Centimeters

The area of the circle is: 314.159 Centimeters1

Code is as follows below:

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 + " 开发者_如何学JAVACentimeters" +
            "\n The area of the circle is: " + formatter.format(Math.PI*Math.pow(radius,2)) + " Centimeters" +
            JOptionPane.INFORMATION_MESSAGE);
    }
}


You're appending JOptionPane.INFORMATION_MESSAGE (which happens to equal 1) to your string. It should be something like:

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

The four parameters are parent, message, title, messageType. Before, you were accidentally using the two-parameter version (parent, message) and appending the messageType to your message.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜