简体   繁体   English

带有双输入的 JOptionpane 验证

[英]JOptionpane validation with double input

I am learning java.我正在学习java。 I try to declare a double array.我尝试声明一个双数组。 For the input method, I would like to use JOptionPane.对于输入法,我想使用 JOptionPane。 My question is how can I create the validation for this array.(For example: I want to valid that the salary input will be from 2000 to 10000).我的问题是如何为这个数组创建验证。(例如:我想验证输入的工资是从 2000 到 10000)。 I'm sorry for my bad writing.我很抱歉我写得不好。 Thank you guys!感谢大伙们!

Here is my code!这是我的代码!

import javax.swing.JOptionPane;

public class Testing {

    /**
     * @param args
     */
    public static void main(String[] args) {

        double[] salary = new double[10];

        for(int i = 0; i < salary.length; i++)
        {
            salary[i] = Double.parseDouble(JOptionPane.showInputDialog(null," Enter Salary: "));
        }

    }

}

I think this will help you a bit to resolve your issue.我认为这会对您解决问题有所帮助。

int i = 0;
double temp;
    while(i < salary.length) {
       // parseDouble throws NumberFormatException, handle it
      temp = Double.parseDouble(JOptionPane.showInputDialog(null," Enter Salary: "));
       if (temp >= 2000.0 && temp <= 10000.0){
           salary[i] = temp;
           i++;     // if in range change counter to next count
          // do something
       } else {
          // do something for out of range
       }
}

Read the section from the Swing tutorial on Stopping Automatic Dialog Closing .阅读 Swing 教程中有关停止自动关闭对话框的部分 Customize the code to do you particular edit.自定义代码以进行特定编辑。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM