java 带有双输入的 JOptionpane 验证
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16202888/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JOptionpane validation with double input
提问by tkay87
I am learning java. I try to declare a double array. For the input method, I would like to use 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). I'm sorry for my bad writing. Thank you guys!
我正在学习java。我尝试声明一个双数组。对于输入法,我想使用 JOptionPane。我的问题是如何为这个数组创建验证。(例如:我想验证输入的工资是从 2000 到 10000)。我很抱歉我写得不好。感谢你们!
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: "));
}
}
}
回答by Smit
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
}
}
回答by camickr
Read the section from the Swing tutorial on Stopping Automatic Dialog Closing. Customize the code to do you particular edit.
阅读 Swing 教程中有关停止自动关闭对话框的部分。自定义代码以进行特定编辑。