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

trycatch语句中,怎么样使用户输入数据不合法自动返回继续输入!?(小程序片段测试,不要说js后台神马的)publictest(){try{Buffered类控制}c... try catch语句中,怎么样使用户输入数据不合法自动返回继续输入!?(小程序片段测试,不要说js后台神马的)public test(){try{Buffered类控制}catch(Exception e){e.print·....}}
控制台程序!!!谢谢。。。。
展开
 我来答
超人影视娱乐
高粉答主

2015-09-07 · 闲来无事的影评人-关注我把
超人影视娱乐
采纳数:7758 获赞数:60594

向TA提问 私信TA
展开全部
实现代码如下:
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);
}
}
帅气的莉大爷
2015-08-31 · TA获得超过2567个赞
知道小有建树答主
回答量:2643
采纳率:5%
帮助的人:697万
展开全部
方法一:用while循环就可以了。
第一步:先设置一个boolean的变量设成ture。
第二步:检查输入信息并进行比较 如果合格就把Boolean变量设成false 跳出循序就行了。
方法二:点击运行,重新输入。
方法三:
第一步:关闭elipse。
第二步:再打开elipse。
第三步:打开程序。
第四步:点击运行。
第五步:再次输入。
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
没有响应m2
2013-02-13 · TA获得超过8386个赞
知道大有可为答主
回答量:7579
采纳率:33%
帮助的人:3409万
展开全部
.用while循环就可以了.
先设置一个boolean的变量设成ture
检查输入信息并进行比较 如果合格就把Boolean变量设成false 跳出循序就行了
本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
Aolose
2013-02-12
知道答主
回答量:10
采纳率:0%
帮助的人:3.5万
展开全部
做成方法调用自己就行了
举个例子:

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();
}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
小一_Alone
2013-02-11
知道答主
回答量:15
采纳率:0%
帮助的人:7万
展开全部
请问是控制台程序还是web程序?
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 1条折叠回答
收起 更多回答(3)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式