上次多了两行
回复 3楼 实际应用
我的意思是说goto 跳到回头后为什么没停下要求要输入数据,而直接的循环上次输入的错识的提示.
public class RegisterDemo { public RegisterDemo() { Register1 frame=new Register1(); frame.setTitle("登录系统"); frame.setVisible(true); } public static void main(String[] args) { new RegisterDemo(); } } class Register extends JFrame { /** * serialVersionUID */ private static final long serialVersionUID = 1L; JLabel lblName,lblPassword; JTextField txtName; JPasswordField txtPass; JButton btnOk,btnExit; public Register1() { lblName=new JLabel("用户名"); lblPassword=new JLabel("密码"); txtName=new JTextField(20); txtPass=new JPasswordField(20); btnOk=new JButton("登录"); btnExit=new JButton("退出"); Box bv1=Box.createVerticalBox(); bv1.add(lblName); bv1.add(Box.createRigidArea(new Dimension(8,8))); bv1.add(lblPassword); bv1.add(Box.createRigidArea(new Dimension(8,8))); Box bv2=Box.createVerticalBox(); bv2.add(txtName); bv2.add(Box.createRigidArea(new Dimension(8,8))); bv2.add(txtPass); bv2.add(Box.createRigidArea(new Dimension(8,8))); Box bh=Box.createHorizontalBox(); bh.add(bv1); bh.add(bv2); Box box=Box.createVerticalBox(); box.add(bh); JPanel panel=new JPanel(); panel.add(btnOk); panel.add(btnExit); box.add(panel); this.setLayout(new FlowLayout()); this.add(box); this.setSize(300,140); this.setLocation(400,400); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); btnOk.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { String name=txtName.getText().trim(); String pass=new String(txtPass.getPassword()); if(name.equals("林月儿")) { if(!pass.trim().equals("123")) { JOptionPane.showMessageDialog(null,"密码不正确!","警告!",JOptionPane.WARNING_MESSAGE); } else if(pass.trim().equals("123")) { String data="用户名:"+name+"\n"; data+="密码:"+pass.trim()+"\n"; JOptionPane.showMessageDialog(null, "登录成功!请核对:\n"+data,"提示",JOptionPane.INFORMATION_MESSAGE); } } else { JOptionPane.showMessageDialog(null,"该用户不存在!","警告!",JOptionPane.WARNING_MESSAGE); } } }); btnExit.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { System.exit(0); } }); } }
#include <cstdio> #include <cstring> #include <conio.h> const short K_ENTER = 0x0d; const char* Correct_Password = "1a2b3c"; const size_t Max_Password_Length = 10; const char* Get_Password(char* str) { size_t index = 0; printf_s("請輸入密碼: "); while (index < Max_Password_Length) { int ch = _getch(); if (ch == K_ENTER) { break; } str[index++] = ch; putchar('*'); } str[index] = '\0'; putchar('\n'); return str; } int main(void) { char password[Max_Password_Length + 1]; size_t count = 0; while (count++ < 2) { printf_s("您輸入的密碼是: %s\n", Get_Password(password)); if (strcmp(password, Correct_Password) == 0) { printf_s("密碼正確!\n"); } } printf_s("按任意鍵結束程序..."); _getch(); return 0; }