有关JAVA字符输入的问题
import javax.swing.JOptionPane;
public class Eg26
{
public static void main(String[] args)
{
int he = 0;
int Bai = 0;
int Shi = 0;
int Ge = 0;
int Zheng = 0;
while(Zheng <= 0 || Zheng >= 1000)
{
String zheng = JOptionPane.showInputDialog(null,"Enter a int value",
"Example 2.1 Input (double)",JOptionPane.QUESTION_MESSAGE);
Zheng = Integer.parseInt(zheng);
}
Ge = Zheng % 10;
Shi = (Zheng / 10) % 10;
Bai = Zheng / 100;
he = Ge + Shi + Bai;
JOptionPane.showMessageDialog(null,he,
"Example 2.1 Output(double)",JOptionPane.INFORMATION_MESSAGE);
}
}
如何对输入的字符或符号进行限制
如果输入值不为1到1000之间的数将重新输入
引用 报告 评分 回复
[广告] 网监王WJW110: 智能双向高速过滤网站内容,禁止浏览不良页面,禁止发布不良信息,全面降低您网站的运营风险!
FreeAccess
VIP
UID 5815079
精华 0
积分 286
帖子 217
威望 227
金钱 0
阅读权限 80
注册 2007-5-9
情侣ID
小语→
来自 黑客基地
状态 离线 #2发表于 2007-10-16 02:29 资料 个人空间 短消息 加为好友
add one while loop construction, check "Zheng" value, if it doesn't below 0 -1000 ,then going into the loop, repeat askinig input value.
import javax.swing.JOptionPane;
class Eg26
{
public static void main(String[] args)
{
int he = 0;
int Bai = 0;
int Shi = 0;
int Ge = 0;
int Zheng = 0;
while(Zheng <= 0 || Zheng >= 1000)
{
String zheng = JOptionPane.showInputDialog(null,"Enter a int value",
"Example 2.1 Input (double)",JOptionPane.QUESTION_MESSAGE);
Zheng = Integer.parseInt(zheng);
while (Zheng > 1000 || Zheng < 0)
{
zheng = JOptionPane.showInputDialog(null,"You Must entry (0 - 1000),try again!",
"Example 2.1 Input (double)",JOptionPane.QUESTION_MESSAGE);
Zheng = Integer.parseInt(zheng);
}
}
Ge = Zheng % 10;
Shi = (Zheng / 10) % 10;
Bai = Zheng / 100;
he = Ge + Shi + Bai;
JOptionPane.showMessageDialog(null,he,
"Example 2.1 Output(double)",JOptionPane.INFORMATION_MESSAGE);
}
}