Java用户输入错误返回继续输入怎么实现?

try catch语句中,怎么样使用户输入数据不合法自动返回继续输入!?(小程序片段测试,不要说js后台神马的)public test(){try{Buffered类控制}catch(Exception e){e.print·....}}
控制台程序!!!谢谢。。。。

实现代码如下:
package com.zuxia.javabasic.practice;
import java.awt.Button;
import java.awt.Color;
import java.awt.Label;
import java.awt.Panel;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class LoginTest extends JFrame implements ActionListener{
public static void main(String[] args) {
new LoginTest();
}
//窗体控件
Panel p = new Panel();
//实例化标签
Label null1=new Label();
Label lblName=new Label();
TextField txtName=new TextField();

Label lblPassword=new Label();
TextField txtPassword=new TextField();

Label lblError=new Label();

Button btnlogin=new Button();
Button btnCancel =new Button();

//初始化控件
public void initialize(){
//改变p面板的布局为空布局
p.setLayout(null);
lblName.setText("用户名:");
lblName.setBounds(30,10,40,20);
txtName.setBounds(90,10,160,20);
//加载标签
p.add(lblName);
//加载输入文本框
p.add(txtName);

lblPassword.setText("密 码:");
txtPassword.setColumns(15);
txtPassword.setEchoChar('*');
lblPassword.setBounds(30,40,40,20);
txtPassword.setBounds(90,40,160,20);
p.add(lblPassword);
p.add(txtPassword);

btnlogin.setLabel("登录");

btnCancel.setLabel("取消");
btnlogin.setBounds(100,80, 40, 30);
btnCancel.setBounds(180,80, 40, 30);

btnlogin.addActionListener(this);
btnCancel.addActionListener(this);
p.add(btnlogin);
p.add(btnCancel);

lblError.setBounds(60, 130, 180, 20);
lblError.setBackground(Color.red);
p.add(lblError);
lblError.setVisible(false);
}

private void checkUser() throws ExceptionClass1 {
String name = txtName.getText().trim();
String password = txtPassword.getText().trim();

if("jack".equals(name)&& "jack123".equals(password)) {
lblError.setVisible(true);
lblError.setText("欢迎登陆!");
} else {
throw new ExceptionClass1("用户名或密码错误!");
}
}

//构造方法
public LoginTest(){
//设置窗体的大小
this.setSize(300, 200);
//设置关闭
this.setDefaultCloseOperation(3);
//设置窗体是否能够放大
this.setResizable(false);
//设置窗体居中
this.setLocationRelativeTo(null);
//设置窗体显示
this.setVisible(true);
//设置标题
this.setTitle("欢迎您使用俺的登录界面");
//加载面板
this.add(p);
//初始化控件
this.initialize();

}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource()==btnlogin){
try {
checkUser();
} catch (ExceptionClass1 e1) {
lblError.setVisible(true);
lblError.setText(e1.getMessage());
}
}
if(e.getSource()==btnCancel){
txtName.setText("");
txtPassword.setText("");
}

}

}
class ExceptionClass1 extends Exception{
public ExceptionClass1() {
super();
}
public ExceptionClass1(String message){
super(message);
}
}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2015-08-31
方法一:用while循环就可以了。
第一步:先设置一个boolean的变量设成ture。
第二步:检查输入信息并进行比较 如果合格就把Boolean变量设成false 跳出循序就行了。
方法二:点击运行,重新输入。
方法三:
第一步:关闭elipse。
第二步:再打开elipse。
第三步:打开程序。
第四步:点击运行。
第五步:再次输入。
第2个回答  2013-02-13
.用while循环就可以了.
先设置一个boolean的变量设成ture
检查输入信息并进行比较 如果合格就把Boolean变量设成false 跳出循序就行了本回答被提问者采纳
第3个回答  2013-02-12
做成方法调用自己就行了
举个例子:

static Scanner sc = new Scanner();

static String input(){
String str=sc.nextLine();
if(str.match("//d{2}.+"))return str;
system.out.print("Error!Retype:")
return input();
}
第4个回答  2013-02-11
请问是控制台程序还是web程序?

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网