java注冊
『壹』 java編寫登陸注冊頁面(簡單一點的,連接資料庫)
這是我自己做的一個管理系統的登錄界面,代碼雖然有點繁瑣,不過簡單易懂,你根據自己的需要進行修改吧。
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.JDialog;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
/**
*
* @author mzhe
*
*/
class Loginfrm extends JDialog implements ActionListener
{
JLabel l_name,l_pass,l_error;
JTextField t_name;
JButton b_enter,b_cancle,b_clear;
JPanel pan1,pan2,pan3,pan4;
JPasswordField t_pass;
String sname,spass;
int sunit;
NetConn sql;
Statement sqll;
ResultSet rs;
Librarybox lbox;
int until=0;
Loginfrm(JFrame f,String s)
{
//界面布局
super(f,s);
l_name=new JLabel("名字:");
l_pass=new JLabel("密碼:");
l_error=new JLabel("請輸入用戶名和密碼登錄");
t_name=new JTextField("",10);
t_pass=new JPasswordField("",10);
t_pass.setEchoChar('*');
b_enter=new JButton("確定");
b_enter.addActionListener(this);
b_cancle=new JButton("取消");
b_cancle.addActionListener(this);
b_clear=new JButton("清除");
b_clear.addActionListener(this);
pan1=new JPanel();
pan2=new JPanel();
pan3=new JPanel();
pan4=new JPanel();
pan1.add(l_name);pan1.add(t_name);
pan2.add(l_pass);pan2.add(t_pass);
pan3.add(l_error);
pan4.add(b_enter);pan4.add(b_cancle);pan4.add(b_clear);
setLayout(new GridLayout(4,1));
add(pan1);add(pan2);add(pan3);add(pan4);
//建立資料庫連接
sql=new NetConn();
//設置窗口大小
setSize(300,300);
setVisible(false);
//得到屏幕信息以便使框架居中顯示
Dimension screeSize = Toolkit.getDefaultToolkit().getScreenSize();
int screeWidth=screeSize.width;
int screeHeight=screeSize.height;
//得到框架的大小信息
Dimension frameSize=this.getSize();
int x=(screeWidth-frameSize.width)/2;
int y=(screeHeight-frameSize.height)/2;
this.setLocation(x, y);
}
public void actionPerformed(ActionEvent e)
{
//單擊確定按鈕的事件處理程序
if(e.getSource()==b_enter)
{
//如果連續登錄次數小於4
if(until<=4)
{
//如果用戶名或者密碼為空,將顯示提示信息
if(t_name.getText().equals("")||t_pass.getText().equals(""))
{
l_error.setText("用戶名和密碼不能為空");
}
else
{
try
{
sqll=sql.connect();
//根據用戶名查詢
rs=sqll.executeQuery("SELECT * FROM users where username="+"'"+t_name.getText()+"'");
//遍歷查詢得到的結果集
while(rs.next())
{
sname=rs.getString(2);
spass=rs.getString(3);
//得到登錄用戶的級別
sunit=Integer.parseInt(rs.getString(4));
//如果密碼正確
if(t_pass.getText().equals(spass))
{
//判斷用戶的級別,根據不同的級別,顯示不同的菜單
switch(sunit)
{
case 1:
{
l_error.setText("登錄成功");
t_name.setText("");
t_pass.setText("");
lbox=new Librarybox();
lbox.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
lbox.setResizable(false);
lbox.bookfi.setEnabled(true);
lbox.bookse.setEnabled(true);
lbox.bookth.setEnabled(true);
lbox.bookfo.setEnabled(true);
lbox.mi_system_manger.setEnabled(true);
lbox.mi_system_login.setEnabled(false);
lbox.setVisible(true);
this.dispose();
break;
}
case 2:
{
l_error.setText("登錄成功");
t_name.setText("");
t_pass.setText("");
lbox=new Librarybox();
lbox.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
lbox.setResizable(false);
lbox.bookfi.setEnabled(false);
lbox.bookse.setEnabled(false);
lbox.bookth.setEnabled(true);
lbox.bookfo.setEnabled(false);
lbox.mi_system_manger.setEnabled(false);
lbox.mi_system_login.setEnabled(false);
lbox.setVisible(true);
this.dispose();
break;
}
case 3:
{
l_error.setText("登錄成功");
t_name.setText("");
t_pass.setText("");
lbox=new Librarybox();
lbox.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
lbox.setResizable(false);
lbox.bookfi.setEnabled(true);
lbox.bookse.setEnabled(false);
lbox.bookth.setEnabled(false);
lbox.bookfo.setEnabled(true);
lbox.mi_system_manger.setEnabled(false);
lbox.mi_system_login.setEnabled(false);
lbox.fi_msglabel_user.setEnabled(false);
lbox.setVisible(true);
this.dispose();
break;
}
}
}
else
{
l_error.setText("用戶名或密碼錯誤!");
until++;
}
}
}
catch(SQLException e2)
{
e2.printStackTrace();
}
}
}
//超出登錄次數
else
{
l_error.setText("你已經超出登錄次數");
t_name.setEnabled(false);
t_pass.setEnabled(false);
b_enter.setEnabled(false);
b_clear.setEnabled(false);
}
}
//單擊清除按鈕的事件處理程序
else if(e.getSource()==b_clear)
{
t_name.setText("");
t_pass.setText("");
l_error.setText("請輸入用戶名和密碼登錄");
}
//單擊取消按鈕的事件處理程序
else if(e.getSource()==b_cancle)
{
dispose();
}
}
}
如果你比較認可的話,請採納吧,不給分也沒關系。只希望對你有用。
『貳』 用JAVA編寫一個用戶或注冊登錄界面。請哪位高手能夠寫下具體的代碼,謝謝
效果圖
<!DOCTYPEhtml>
<html>
<head>
<metacharset="UTF-8">
<title>先鋒圖書館管理系統-登錄</title>
<style>
*{
margin:0;
padding:0;
list-style:none;
}
#top{
width:1000px;
height:95px;
margin:0auto;
margin-top:25px;
}
#top_top{
width:1000px;
height:65px;
background:deepskyblue;
}
#top_top_left{
width:300px;
height:65px;
float:left;
}
#top_top_left>label{
width:200px;
height:65px;
color:white;
float:right;
}
#top_top_left>#a2{
padding-left:10px;
padding-top:20px;
font-size:16px;
}
#top_bottom{
width:1000px;
height:30px;
}
#top_bottom_left{
width:340px;
height:30px;
line-height:30px;
font-size:12px;
background:skyblue;
color:white;
text-indent:2em;
float:left;
}
#top_bottom_right{
width:660px;
height:30px;
line-height:30px;
font-size:12px;
color:blueviolet;
text-align:center;
float:right;
background:lightskyblue;
}
#content{
width:1000px;
height:600px;
margin:0auto;
background:#587FBA;
}
#content>#text{
width:1000px;
height:50px;
line-height:50px;
padding-top:100px;
font-size:36px;
font-family:"楷體";
font-weight:bold;
text-align:center;
}
#content>#login{
width:480px;
height:210px;
margin-top:20px;
margin-left:260px;
background:#85A0CB;
}
#content>#login>img{
float:left;
}
#content>#login>#select{
width:305px;
height:210px;
float:right;
}
#content>#login>#select>div{
width:230;
height:30px;
margin-left:30px;
}
#content>#login>#select>#d1{
margin-top:30px;
margin-bottom:3px;
}
#content>#login>#select>p{
font-size:14px;
margin-left:95px;
}
#bottom{
width:1000px;
height:35px;
line-height:35px;
margin:0auto;
background:deepskyblue;
text-align:center;
color:white;
}
</style>
</head>
<body>
<divid="top">
<divid="top_top">
<divid="top_top_left">
<imgsrc="img/test/a13.png"width="78px"height="65px"><labelid="a2">先鋒圖書館系統管理平台</label>
</div>
</div>
<divid="top_bottom">
<divid="top_bottom_left">當前位置:首頁>系統管理>登錄</div>
<divid="top_bottom_right">當前時間:<labelid="lable"></label></div>
</div>
</div>
<divid="content">
<divid="text">歡迎登錄先鋒圖書館管理系統</div>
<divid="login">
<imgsrc="img/test/a14.png"width="175px"height="210px"/>
<formid="select">
<divid="d1">用戶名: <inputtype="text"/></div>
<div>密 碼: <inputtype="password"/></div>
<p>
<inputtype="radio"name="user"value="read"/>讀者
<inputtype="radio"name="user"value="admin"/>管理員
</p><br/>
<p>
<inputtype="button"value="確定"style="width:50px;"onclick="put()"/>
<inputtype="reset"value="重置"style="width:50px;"/>
</p>
</form>
</div>
</div>
<divid="bottom">欣欣科技有限公司版權所有</div>
</body>
<scripttype="text/javascript"src="JQuery/jquery.js"></script>
<scripttype="text/javascript"src="js/GetCurrentTime.js"></script>
<script>
//驗證用戶名和密碼
functionput(){
vard=$("#select>div>input");//獲取用戶名和密碼
varname=d[0].value;
varpass=d[1].value;
varuser=null;
varr=document.getElementsByName("user");//獲取用戶類型
for(i=0;i<r.length;i++){
if(r[i].checked){
user=r[i].value;
}
}
//console.log(name+","+pass+","+user);//輸出測試
if(user==null){
window.alert("請選擇用戶類型!");
}elseif(user=="admin"&&name!="admin"){
window.alter("用戶名錯誤!");
}elseif(user=="admin"&&name=="admin"&&pass!="123456"){
window.alert("密碼錯誤!");
}elseif(name=="admin"&&pass=="123456"&&user=="admin"){
window.location.href="work_02_welcome.html";//在js中在本頁面中打開新鏈接
}else{
window.alert("用戶名錯誤");
}
}
</script>
</html>
『叄』 JAVA設計一個用戶注冊的小程序
OK!這個不難,我去上自習,回來給你答案
不好意思,昨天晚上回來一時忘了,下面是源碼:
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
public class Register extends JFrame implements ActionListener {
JLabel name,password,repassword,age,email,address,zip,ID;
JTextField tname,tage,temail,taddress,tzip,tID;
JPasswordField tpassword,trepassword;
JButton OK,cancel;
public static void main(String [] args) {
new Register();
}
public Register() {
JPanel p=new JPanel(new GridLayout(9,2));
name=new JLabel("姓名:",JLabel.CENTER);
tname=new JTextField(8);
p.add(name);
p.add(tname);
password=new JLabel("密碼:",JLabel.CENTER);
tpassword=new JPasswordField(16);
p.add(password);
p.add(tpassword);
repassword=new JLabel("確認密碼:",JLabel.CENTER);
trepassword=new JPasswordField(16);
p.add(repassword);
p.add(trepassword);
age=new JLabel("年齡:",JLabel.CENTER);
tage=new JTextField(4);
p.add(age);
p.add(tage);
email=new JLabel("電子郵件:",JLabel.CENTER);
temail=new JTextField(35);
p.add(email);
p.add(temail);
address=new JLabel("地址:",JLabel.CENTER);
taddress=new JTextField(50);
p.add(address);
p.add(taddress);
zip=new JLabel("郵編:",JLabel.CENTER);
tzip=new JTextField(10);
p.add(zip);
p.add(tzip);
ID=new JLabel("身份證號碼:",JLabel.CENTER);
tID=new JTextField(18);
p.add(ID);
p.add(tID);
OK=new JButton("注冊");
cancel=new JButton("取消");
p.add(OK);
p.add(cancel);
OK.addActionListener(this);
cancel.addActionListener(this);
setContentPane(p);
setTitle("注冊");
setBounds(100,100,150,400);
setVisible(true);
}
public boolean checkPassword() {
String s1=new String(tpassword.getPassword());
String s2=new String(trepassword.getPassword());
if(s1.equals(s2))
return true;
else
return false;
}
public boolean checkEmail() {
String s=temail.getText();
if(s.contains("@"))
return true;
else
return false;
}
public boolean checkID() {
String id=tID.getText();
if(id.length()==15||id.length()==18)
return true;
else
return false;
}
public void actionPerformed(ActionEvent e) {
JButton b=(JButton)e.getSource();
if(b==OK) {
if(checkID()&&checkEmail()&&checkPassword()) {
JOptionPane.showMessageDialog(this,"恭喜你,注冊成功!");
}
else {
boolean b1=checkPassword();
boolean b2=checkEmail();
boolean b3=checkID();
if(!b1) {
JOptionPane.showMessageDialog(this,"兩次輸入密碼不一致");
}
if(!b2) {
JOptionPane.showMessageDialog(this,"電子郵件格式不正確");
}
if(!b3) {
JOptionPane.showMessageDialog(this,"身份證號碼位數必須為15或18");
}
clear();
}
}
if(b==cancel) {
clear();
}
}
public void clear() {
tname.setText("");
tpassword.setText("");
trepassword.setText("");
tage.setText("");
temail.setText("");
taddress.setText("");
tzip.setText("");
tID.setText("");
}
}
『肆』 java語言實現用戶注冊和登錄
//這個是我寫的,裡面有連接資料庫的部分。你可以拿去參考一下
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.sql.*;
class LoginFrm extends JFrame implements ActionListener// throws Exception
{
JLabel lbl1 = new JLabel("用戶名:");
JLabel lbl2 = new JLabel("密碼:");
JTextField txt = new JTextField(5);
JPasswordField pf = new JPasswordField();
JButton btn1 = new JButton("確定");
JButton btn2 = new JButton("取消");
public LoginFrm() {
this.setTitle("登陸");
JPanel jp = (JPanel) this.getContentPane();
jp.setLayout(new GridLayout(3, 2, 5, 5));
jp.add(lbl1);
jp.add(txt);
jp.add(lbl2);
jp.add(pf);
jp.add(btn1);
jp.add(btn2);
btn1.addActionListener(this);
btn2.addActionListener(this);
}
public void actionPerformed(ActionEvent ae) {
if (ae.getSource() == btn1) {
try {
Class.forName("com.mysql.jdbc.Driver");// mysql資料庫
Connection con = DriverManager.getConnection(
"jdbc:mysql://localhost/Car_zl", "root", "1");// 資料庫名為Car_zl,密碼為1
System.out.println("com : "+ con);
Statement cmd = con.createStatement();
String sql = "select * from user where User_ID='"
+ txt.getText() + "' and User_ps='"
+ pf.getText() + "'" ;
ResultSet rs = cmd
.executeQuery(sql);// 表名為user,user_ID和User_ps是存放用戶名和密碼的欄位名
if (rs.next()) {
JOptionPane.showMessageDialog(null, "登陸成功!");
} else
JOptionPane.showMessageDialog(null, "用戶名或密碼錯誤!");
} catch (Exception ex) {
}
if (ae.getSource() == btn2) {
System.out.println("1111111111111");
//txt.setText("");
//pf.setText("");
System.exit(0);
}
}
}
public static void main(String arg[]) {
JFrame.(true);
LoginFrm frm = new LoginFrm();
frm.setSize(400, 200);
frm.setVisible(true);
}
}
『伍』 如何用Java編寫用戶注冊界面
界面建議用可視化來做,美觀且便捷。下面這個是完全用代碼寫的,僅供參考。
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.sql.*;
public class Register extends JFrame {
JLabel jl1 = new JLabel("用戶名");
JTextField jt1 = new JTextField();
JLabel jl2 = new JLabel("郵箱");
JTextField jt2 = new JTextField();
JLabel jl3 = new JLabel("密碼");
JPasswordField jpw1 = new JPasswordField();
JLabel jl4 = new JLabel("密碼確認");
JPasswordField jpw2 = new JPasswordField();
JButton register = new JButton("注冊");
JButton clean = new JButton("清空");
public Register(){
setLayout(new GridLayout(5,2));
add(jl1);
add(jt1);
add(jl2);
add(jt2);
add(jl3);
add(jpw1);
add(jl4);
add(jpw2);
add(register);
add(clean);
String name = jt1.getText();
String email = jt2.getText();
String pw = jpw1.getText();
register.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
try{
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost/db","root","");
Statement sta = con.createStatement();
sta.executeUpdate("INSERT INTO register VALUES(name,email,pw)");
JOptionPane.showMessageDialog(null,"注冊成功","提示",JOptionPane.INFORMATION_MESSAGE);
}
catch(Exception ex){
ex.getStackTrace();
}
}
});
clean.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
jt1.setText("");
jt2.setText("");
jpw1.setText("");
jpw2.setText("");
}
});
}
public static void main(String[] args){
Register frame = new Register();
frame.setTitle("用戶注冊");
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400,400);
frame.setVisible(true);
}
}
『陸』 用java web編寫一個用戶注冊界面(只要寫出以下要求就行)
一步步更新:頁面
<form action="regist.servlet" method="post"><table width="99%" border="0" align="center" cellpadding="0" cellspacing="0" class="tableAdd borTop"> <tr> <th width="14%" height="30" nowrap>用戶名</th> <td class="pl5"> <INPUT id="sName" name="name" type="text" size="20"> </td> </tr> <tr> <th width="14%" height="30" nowrap>密碼</th> <td class="pl5"> <INPUT name="password" type="password" size="20"> </td> </tr> <tr> <th width="14%" height="30" nowrap>確認密碼</th> <td class="pl5"> <INPUT name="confrimPwd" type="password" size="20"> </td> </tr> <tr> <th width="14%" height="30" nowrap>性別</th> <td class="pl5"> 男<INPUT name="sex" type="radio" value="1" checked="checked" size="20"> 女<INPUT name="sex" type="radio" value="0" size="20"> </td> </tr> <tr> <th width="14%" height="30" nowrap>愛好</th> <td class="pl5"> <INPUT name="enjoy" type="checkbox" size="20" value="籃球">籃球 <INPUT name="enjoy" type="checkbox" size="20" value="足球">足球 </td> </tr> <tr> <th width="14%" height="30" nowrap>生日</th> <td class="pl5"> <INPUT name="brithday" type="text" size="20"> </td> </tr> <tr> <th width="14%" height="30" nowrap>備注</th> <td class="pl5"> <textarea rows="5" cols="200" name="remark"></textarea> </td> </tr> <tr> <th width="14%" height="30" nowrap> </th> <td class="pl5"> <input type="submit" value="提交"> <input type="reset" value="重置"> </td> </tr></table></form>
資料庫部分:
import java.sql.DriverManager;import java.sql.ResultSet;import java.sql.SQLException;import com.mysql.jdbc.Connection;import com.mysql.jdbc.Statement;public class DataBaseUtil { public static Connection getConnection() throws ClassNotFoundException, SQLException { Class.forName("com.mysql.jdbc.Driver"); Connection conn = (Connection) DriverManager.getConnection("jdbc:mysql://192.168.100.113/datebase", "username", "password"); return conn; } public static Statement getPS() throws ClassNotFoundException, SQLException { Statement statement = (Statement) getConnection().createStatement(); return statement; } public static void close(Connection conn,Statement st,ResultSet rs) throws SQLException{ if(rs != null) { rs.close(); } if(st != null) { st.close(); } if(conn != null) { conn.close(); } }}
『柒』 java編寫一個用戶注冊及登錄界面
mportjava.awt.HeadlessException;
importjava.awt.event.ActionEvent;
importjava.awt.event.ActionListener;
importjavax.swing.ImageIcon;
importjavax.swing.JButton;
importjavax.swing.JFrame;
importjavax.swing.JLabel;
importjavax.swing.JOptionPane;
importjavax.swing.JPanel;
importjavax.swing.JPasswordField;
importjavax.swing.JTextField;
@SuppressWarnings("serial")
{
JLabellbl1=newJLabel("用戶名:");
JLabellbl2=newJLabel("密碼:");
JTextFieldtxt=newJTextField("admin",20);
JPasswordFieldpwd=newJPasswordField(20);
JButtonbtn=newJButton("登錄");
JPanelpnl=newJPanel();
privateinterror=0;
publicMainFrame(Stringtitle)throwsHeadlessException{
super(title);
init();
}
privatevoidinit(){
this.setResizable(false);
pwd.setEchoChar('*');
pnl.add(lbl1);
pnl.add(txt);
pnl.add(lbl2);
pnl.add(pwd);
pnl.add(btn);
this.getContentPane().add(pnl);
btn.addActionListener(newActionListener(){
@Override
publicvoidactionPerformed(ActionEvente){
if("admin".equals(newString(pwd.getPassword()))){
pnl.removeAll();
JLabellbl3=newJLabel();
ImageIconicon=newImageIcon(this.getClass().getResource("pic.jpg"));
lbl3.setIcon(icon);
pnl.add(lbl3);
}
else{
if(error<3){
JOptionPane.showMessageDialog(null,"密碼輸入錯誤,請再試一次");
error++;
}
else{
JOptionPane.showMessageDialog(null,"對不起,您不是合法用戶");
txt.setEnabled(false);
pwd.setEnabled(false);
btn.setEnabled(false);
}
}
}
});
}
publicstaticvoidmain(String[]args){
MainFramefrm=newMainFrame("測試");
frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frm.setBounds(100,100,300,120);
frm.setVisible(true);
}
}
『捌』 java實現 做一個簡單的用戶注冊系統
packagetest1;
/**
*用戶類
*/
publicclassUser{
privateStringname;
privateintage;
publicStringgetName(){
returnname;
}
publicvoidsetName(Stringname){
this.name=name;
}
publicintgetAge(){
returnage;
}
publicvoidsetAge(intage){
this.age=age;
}
@Override
publicStringtoString(){
return"User[name="+name+",age="+age+"]";
}
}
package test1;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
/**
* 主方法類
*/
public class Test1 {
// 創建鍵盤輸入對象
static Scanner input = new Scanner(System.in);
// 存儲用戶
static List<User> userList = new ArrayList<>();
public static void main(String[] args) {
try {
// 初始化數據
User e1 = new User();
e1.setName("張三");
e1.setAge(18);
User e2 = new User();
e2.setName("李四");
e2.setAge(21);
userList.add(e1);
userList.add(e2);
while (true) {
System.out.println("=========菜單=========");
System.out.println("1.增加新用戶;2.修改用戶;3.刪除用戶;4.查找用戶;5.退出)");
System.out.println("請鍵入對應的數字(1-5)進入相應的功能");
int handle = input.nextInt();
caseMenu(handle);
}
} catch (Exception e) {
throw new RuntimeException("請輸入正確的內容!");
}
}
/*
* 選擇菜單
*/
private static void caseMenu(int handle) {
try {
s: switch (handle) {
case 1:
// 新增用戶
User user = new User();
System.err.print("請輸入用戶名:");
String name = input.next();
// 判斷用戶名是否已存在
f: for (User u : userList) {
if (u.getName().equals(name)) {
System.err.println("用戶名已存在, 新增失敗");
// 直接跳出switch
break s;
}
}
System.err.print("請輸入年齡:");
int age = input.nextInt();
// 將新增用戶插入List集合
user.setName(name);
user.setAge(age);
userList.add(user);
System.out.println("新增用戶成功!");
// 查詢所有用戶
showAllUsers();
break;
case 2:
// 修改用戶
System.err.print("請輸入需要修改年齡的用戶名:");
String uName = input.next();
// 判斷用戶名是否已存在
f: for (User u : userList) {
if (u.getName().equals(uName)) {
System.err.print(uName + "年齡修改為: ");
int uAge = input.nextInt();
u.setAge(uAge);
System.err.println("修改成功!");
showAllUsers();
// 直接跳出switch
break s;
}
}
System.err.println("用戶名為:" + uName + "的用戶不存在, 修改失敗!");
break;
case 3:
// 刪除用戶
System.err.print("請輸入要刪除的用戶名:");
String dName = input.next();
// 判斷用戶名是否已存在
f: for (User u : userList) {
if (u.getName().equals(dName)) {
System.err.print("確定要刪除"" + dName + ""用戶嗎?(y/n)");
String isYes = input.next();
if ("y".equals(isYes)) {
userList.remove(u);
System.err.println("刪除成功!");
} else {
System.err.println("刪除操作取消..");
}
showAllUsers();
// 直接跳出switch
break s;
}
}
System.err.println("用戶名為:" + dName + "的用戶不存在, 刪除失敗!");
break;
case 4:
// 查找用戶
System.err.print("請輸入要查找的用戶名:");
String sName = input.next();
// 判斷用戶名是否已存在
f: for (User u : userList) {
if (u.getName().equals(sName)) {
boolean flag = userList.contains(u);
System.err.println("用戶名: " + u.getName() + " 年齡: " + u.getAge());
// 直接跳出switch
break s;
}
}
System.err.println("用戶名為:" + sName + "的用戶不存在, 查詢失敗!");
break;
case 5:
// 系統退出
// 關閉鍵盤輸入流
input.close();
System.exit(0);
break;
default:
break;
}
} catch (Exception e) {
throw new RuntimeException("請輸入正確的內容!");
}
}
/*
* 查詢所有用戶
*/
private static void showAllUsers() {
for (User u : userList) {
System.out.println("姓名: " + u.getName() + " 年齡: " + u.getAge());
}
}
}
『玖』 用java編程實現用戶注冊並進行登錄操作
String username = "",password = "",passwordagain = ""; // 定義用戶名和密碼
將該變數等於為全局變數 或局部變數即可
『拾』 如何用JAVA生成注冊序列號
平常我們都接觸過軟體注冊,輸入序列號、激活碼、注冊碼、授權碼;對於這些字元碼到底代表什麼含義不甚了解,但一般來說,這些字元碼中都有幾個特點:
1、唯一性,肯定是一個唯一的序列號,否則就會存在濫用的問題。
2、加密性,肯定是經過加密或者混亂的,防止大家自己生成序列號。
3、解密性,軟體自身肯定可以解密,否則無法驗證合法性。
4、可讀性,序列號一般都比較標准,方便書寫和記憶,所以一般都為數字和字母。
以下給出簡單示例:
[java] view plain
/**
* byte轉哈希
* @param b
* @return
*/
public static String byte2hex(byte[] b) {
String hs = "";
String stmp = "";
for (int n = 0; n < b.length; n++) {
stmp = Integer.toHexString(b[n] & 0xFF);
if (stmp.length() == 1)
hs += ("0" + stmp);
else
hs += stmp;
}
return hs.toUpperCase();
}
/**
* 哈希轉byte
* @param b
* @return
*/
public static byte[] hex2byte(byte[] b) {
if ((b.length % 2) != 0)
throw new IllegalArgumentException("長度不是偶數");
byte[] b2 = new byte[b.length / 2];
for (int n = 0; n < b.length; n += 2) {
String item = new String(b, n, 2);
b2[n / 2] = (byte) Integer.parseInt(item, 16);
}
return b2;
}