Получение «1» в конце JOptionOutput - PullRequest
0 голосов
/ 08 июля 2010

Простая программа, чтобы просто найти окружность, диаметр и площадь круга. Всякий раз, когда я запускаю программу, все в порядке, просто в конце всегда есть 1 или -1 после значения для Area. Например, при использовании радиуса 10 я получаю:

Результаты
Окружность круга: 62,832 см
Диаметр круга составляет: 20,0 сантиметров
Площадь круга составляет: 314,159 сантиметров1

Код выглядит следующим образом:

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 Ответ

1 голос
/ 08 июля 2010

Вы добавляете 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);

Четырьмя параметрами являются parent, message, title, messageType. Раньше вы случайно использовали версию с двумя параметрами (parent, message) и добавляли messageType к вашему сообщению.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...