2015-06-05 57 views
0

我只是在学习一点Java,并弄清楚事情是如何工作的。
我已经设法让第一个窗口“弹出”,
但我无法得到“弹出”的答案,并且帮助会很好。从Java GUI获取输入时遇到的问题

package classwork; 
import java.util.Scanner; 

import javax.swing.JFrame; 
import javax.swing.JOptionPane; 

public class computeArea { 
    public static void main(String[] args) { 
     Scanner input = new Scanner(System.in); 

     JOptionPane.showInputDialog("What is your radius?"); 
     double radius = input.nextDouble(); 

     double area = radius * radius * 3.14159; 
     JOptionPane.showMessageDialog(null," the area for the circle of radius is " + radius + " is " + area); 
    } 
} 
+0

你为什么不得到从输入对话框中的价值? – MadProgrammer

+0

我相信扫描仪是用于命令行输入(我可能是错误的)。 –

回答

3

让我们开始的事实,你是穿越的目的,GUI的让用户输入数据的一种方法,游戏机做另一个,你应该避免同时使用,而是简单地使用GUI功能,这是提供给您...

因此,核心问题是double radius =input.nextDouble();等待输入......虽然你只是提示输入用户...

你可能有另一个问题,是双打可以使用格式化科学记数法,我敢肯定它很棒,但对某些用户来说并不总是特别有用,所以你可能会考虑使用NumberFormat格式化双值成什么样我们其余的人可以理解;)

String value = JOptionPane.showInputDialog("What is your radius?"); 
// User cancelled or closed the dialog 
if (value != null) { 
    try { 
     double radius = Double.parseDouble(value); 
     double area = radius * radius * Math.PI; 
     JOptionPane.showMessageDialog(null, "The area of the circle of radius " + NumberFormat.getNumberInstance().format(radius) + " is " + NumberFormat.getNumberInstance().format(area)); 
    } catch (NumberFormatException exp) { 
     JOptionPane.showMessageDialog(null, value + " is not a valid double, try again"); 
    } 
} 

看看How to Make Dialogs的一些细节

+0

这非常有帮助谢谢! :) –

+0

@IsaacAlderton很高兴它可以帮助 – MadProgrammer

相关问题