我正在尝试学习如何在对话框中输入多个输入
对于EG以下程序要求用户在单独的框中输入x 1,Y 1和radius的值,如果用户可以在同一个框中输入值X,Y和半径,那么看起来不错的是什么。请让我知道
[code]
import javax.swing.JOptionPane;
public class C3E29GeometryTwoCircles
{
public static void main(String[] args)
{
// Ask the user to enter the x1 coordinate.
String strNumber = JOptionPane.showInputDialog("Enter the value of x1 coordinate " );
double x1 = Double.parseDouble(strNumber);
strNumber = JOptionPane.showInputDialog("Enter the value of y1 coordinate " );
double y1 = Double.parseDouble(strNumber);
strNumber = JOptionPane.showInputDialog("Enter the value of radius " );
double r1 = Double.parseDouble(strNumber);
strNumber = JOptionPane.showInputDialog("Enter the value of x2 coordinate " );
double x2 = Double.parseDouble(strNumber);
strNumber = JOptionPane.showInputDialog("Enter the value of y2 coordinate " );
double y2 = Double.parseDouble(strNumber);
strNumber = JOptionPane.showInputDialog("Enter the value of radius " );
double r2 = Double.parseDouble(strNumber);
double distance = Math.sqrt((Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2)));
String strOutput;
if ((distance <= Math.abs(r1 - r2)))
{
strOutput = "The circle2 is inside circle1 ";
}
else if (distance <= (r1 + r2))
{
strOutput = "circle2 overlaps circle1";
}
else
{
strOutput = "circle2 does not overlap circle1";
}
JOptionPane.showMessageDialog(null, strOutput);
}
}
[/code]
答案 0 :(得分:0)
由于您使用对话框执行所有操作,我认为您还没有进入swing库,在这种情况下,可能最简单的解决方案是从对话框中分割输入。 告诉用户按特定顺序输入值,并用逗号,空格等分隔它们,然后在该字符上拆分字符串。
String strNumber = JOptionPane.showInputDialog("Enter the value of x,y and radius, separated by space." );
String[] input = strNumber.split(" ");
double x1 = Double.parseDouble(input[0]);
double y1 = Double.parseDouble(input[1]);
double r1 = Double.parseDouble(input[2]);
编辑:您需要检查/验证输入,以避免NumberFormatException和ArrayIndexOutOfBounds。