Can';不能在Java上验证数据
我在Java中遇到了一个问题。我试图检查提供的输入是否只包含十进制数(数字和a“)。这段代码是我的教授提供的,它不是真正的计算。我真的不知道是怎么回事Can';不能在Java上验证数据,java,validation,Java,Validation,我在Java中遇到了一个问题。我试图检查提供的输入是否只包含十进制数(数字和a“)。这段代码是我的教授提供的,它不是真正的计算。我真的不知道是怎么回事 import javax.swing.JOptionPane; public class MoneyCount { public static void check(String s) { boolean decimalPoint = false; for (int i = 0; i < s.le
import javax.swing.JOptionPane;
public class MoneyCount {
public static void check(String s) {
boolean decimalPoint = false;
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) == '.') {
if (!decimalPoint) {
decimalPoint = true;
}
} else {
JOptionPane.showMessageDialog(null,
"You must enter an integer value");
System.exit(0);
}
}
}
public static void main(String[] args) {
//retrieve amount due
String moneyd = JOptionPane.showInputDialog("Enter the amount due");
check(moneyd);
double amountd = Double.parseDouble(moneyd) * 100;
String moneyr = JOptionPane.showInputDialog("Enter amount you would like to pay");
check(moneyr);
double amountr = Double.parseDouble(moneyr) * 100;
}
}
import javax.swing.JOptionPane;
公共类货币计数{
公共静态无效检查(字符串s){
布尔小数点=假;
对于(int i=0;i
尝试这样更改代码
public static void check(String s) {
boolean decimalPoint = false;
for (int i = 0; i < s.length();i++) {
if (s.charAt(i) == '.') {
if (!decimalPoint) {
decimalPoint = true;
}else{
JOptionPane.showMessageDialog(null,
"Not a valid number (contains more than one .)");
System.exit(0);
}
}
}
if (decimalPoint) {
{
JOptionPane.showMessageDialog(null,
"You must enter an integer value");// you need only integer as input
System.exit(0);
}
}
}
公共静态无效检查(字符串s){
布尔小数点=假;
对于(int i=0;i
只有在检查整个字符串后才能显示消息。
那是在完成循环之后从桌面检查开始(哦,亲爱的上帝,我从没想过我会再这么说)
基本上,这个方法所做的就是检查s
是否包含多个
字符
从s开始
等于123.123
=decimalPoint
false
- 当
=i
时,0
将为s.charAt(i)
1
- 当
=i
时,1
将是s.charAt(i)
2
- 当
=i
时,2
将是s.charAt(i)
3
- 当
=i
时,3
将s.charAt(i)
为decimalPoint
,将false
更改为decimalPoint
true
- 当
=i
时,4
将是s.charAt(i)
1
- 当
=i
时,5
将是s.charAt(i)
2
- 当
=i
时,6
将是s.charAt(i)
3
s开始
等于1.2.3
=decimalPoint
false
- 当
=i
时,0
将为s.charAt(i)
1
- 当
=i
时,1
将s.charAt(i)
为decimalPoint
,将false
更改为decimalPoint
true
- 当
=i
时,2
将s.charAt(i)
2
- 当
=i
时,3
将s.charAt(i)
为decimalPoint
,显示错误消息并退出true
s开始
等于123
=decimalPoint
false
- 当
=i
时,0
将为s.charAt(i)
1
- 当
=i
时,1
将是s.charAt(i)
2
- 当
=i
时,2
将是s.charAt(i)
3
为decimalPoint
false
因此,唯一中断的情况是第二种情况…注意,此方法没有考虑如果输入字母字符可能发生的情况…但这不是它的设计目的…您可以使用try-and-catch验证字符串是否为十进制或与上述代码不同:
public static void check(String s) {
try {
Integer.parseInt(s);
} catch (Exception e) {
JOptionPane.showMessageDialog(null,
"You must enter an integer value");
System.exit(0);
}
}
问题是要找出教授的代码有什么问题,还是要编写自己的方法 如果是前者,我还要指出错误消息的意图应该是“您必须输入一个十进制值”。检查这个 公开课考试{
public static void main(String[] args) throws ParseException {
String moneyd = JOptionPane.showInputDialog("Enter the amount due");
if (check(moneyd)) {
double amountd = Double.parseDouble(moneyd) * 100;
System.out.println("ok");
} else {
System.out.println("Not a valid number");
System.exit(0);
}
}
public static boolean check(String s) {
boolean decimalPoint = false;
for (int i = 0; i < s.length(); i = i + 1) {
if (s.charAt(i) == '.') {
if (!decimalPoint) {
decimalPoint = true;
} else {
return (false);
}
}
}
return true;
}
publicstaticvoidmain(字符串[]args)引发异常{
String moneyd=JOptionPane.showInputDialog(“输入到期金额”);
如果(支票(已付款)){
双倍金额=双倍。双倍(货币)*100;
System.out.println(“ok”);
}否则{
System.out.println(“不是有效数字”);
系统出口(0);
}
}
公共静态布尔检查(字符串s){
布尔小数点=假;
对于(int i=0;i
}你所说的“它不是真正的评估”是什么意思 我理解代码的方式:
- 如果您输入了正确的数字(例如:“1”、“24.2”),则可以正常工作
- 如果输入了错误的数字(例如:“1.5.6”),则显示错误对话框
- 对任何其他对象抛出异常(例如:“blabla”、“1,2”)
Double吗。解析(字符串)
这是作弊吗