我的计算需要和JOption输入而不是system.output

时间:2013-12-06 00:53:12

标签: java input

我想要发生的是“FOR”为该计算而不是系统输入的joption输入

  String me = JOptionPane.showInputDialog("Please input a username!");
  JOptionPane.showMessageDialog(null,"Your name is: " + me);
  String user = JOptionPane.showInputDialog("Please enter your choice!");
  JOptionPane.showMessageDialog(null, "Your choice is: " + user);
  String numbers = JOptionPane.showInputDialog("Please enter a set of numbers!");

  int n, c, fact = 1;

  if(user.equals("1"))
  JOptionPane.showMessageDialog(null, "" + me + "" + numbers);

  else if(user.equals("2"))                
  JOptionPane.showInputDialog("Please enter a number to calculate a Factoral Username");
  Scanner in = new Scanner(System.in);

  n = in.nextInt();

  if ( n < 0 )
  JOptionPane.showMessageDialog(null, ("Number should be non-negative."));

  else
  {
  for ( c = 1 ; c <= n ; c++ )
  fact = fact*c;

  JOptionPane.showMessageDialog(null, ("New username is: " + me + fact));

1 个答案:

答案 0 :(得分:0)

将对话框调用的结果赋给变量,然后将其转换为整数:

String input = JOptionPane.showInputDialog("Please enter a number");
try {
    int n = Integer.parseInt(input);
    // ....
} catch (NumberFormatException e) {
    // Exception when input is not in integer format
    // or is null when user press cancel button
}