gpt4 book ai didi

java - 在 JOptionOutput 末尾获取 "1"

转载 作者:行者123 更新时间:2023-12-01 16:03:32 25 4
gpt4 key购买 nike

简单的程序,只需计算圆的周长、直径和面积。每当我运行该程序时,都很好,只是在最后,Area 值后面总是有一个 1 或 -1。例如,当使用半径 10 时,我得到:

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

代码如下:

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

最佳答案

您正在将 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。之前,您不小心使用了双参数版本(父级、消息)并将 messageType 附加到消息中。

关于java - 在 JOptionOutput 末尾获取 "1",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3200561/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com