你好,按照你的要求代碼如下,可以直接運行,并給出了運行結(jié)果:
成都創(chuàng)新互聯(lián)公司是一家集網(wǎng)站建設(shè),涪城企業(yè)網(wǎng)站建設(shè),涪城品牌網(wǎng)站建設(shè),網(wǎng)站定制,涪城網(wǎng)站建設(shè)報價,網(wǎng)絡(luò)營銷,網(wǎng)絡(luò)優(yōu)化,涪城網(wǎng)站推廣為一體的創(chuàng)新建站企業(yè),幫助傳統(tǒng)企業(yè)提升企業(yè)形象加強企業(yè)競爭力??沙浞譂M足這一群體相比中小企業(yè)更為豐富、高端、多元的互聯(lián)網(wǎng)需求。同時我們時刻保持專業(yè)、時尚、前沿,時刻以成就客戶成長自我,堅持不斷學(xué)習(xí)、思考、沉淀、凈化自己,讓我們?yōu)楦嗟钠髽I(yè)打造出實用型網(wǎng)站。
import java.util.Scanner;
public class test {
public static void main(String[] args) {
// 獲得用戶輸入
System.out.println("請輸入一個數(shù):");
Scanner s = new Scanner(System.in);
int i = s.nextInt();
s.close();
// 判斷是否是三位數(shù)
if (i 100 || i 999) {
System.out.println("不是三位數(shù)");
return;
}
// 輸入結(jié)果
String str = String.valueOf(i);
System.out.println("百位:" + str.charAt(0));
System.out.println("十位:" + str.charAt(1));
System.out.println("個位:" + str.charAt(2));
}
}
運行結(jié)果:
請輸入一個數(shù):
134
百位:1
十位:3
個位:4
很簡單的應(yīng)用,為了節(jié)省字數(shù),代碼注釋我就不加了
首先是顯示層,LoinWindow:
import?java.awt.FlowLayout;
import?java.awt.GridBagConstraints;
import?java.awt.GridBagLayout;
import?java.awt.GridLayout;
import?java.awt.event.ActionEvent;
import?java.awt.event.ActionListener;
import?java.awt.event.FocusEvent;
import?java.awt.event.FocusListener;
import?javax.swing.JButton;
import?javax.swing.JFrame;
import?javax.swing.JLabel;
import?javax.swing.JOptionPane;
import?javax.swing.JPanel;
import?javax.swing.JTextField;
import?javax.swing.border.EmptyBorder;
public?class?LoinWindow?extends?JFrame?implements?ActionListener,?FocusListener?{
private?JPanel?mainPanel,?namePanel,?btnPanel;
private?JTextField?tfName,?tfPsd;
private?JButton?btnLogin,?btnCancel;
private?static?final?int?WIDTH?=?300;
private?static?final?int?HEIGHT?=?200;
private?LoginService?service?=?new?LoginService();
public?LoinWindow()?{
super("登錄窗體");
}
public?void?launch()?{
setSize(WIDTH,?HEIGHT);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
GridLayout?mainLayout?=?new?GridLayout(2,?1);
mainLayout.setVgap(10);
mainPanel?=?new?JPanel(mainLayout);
GridBagLayout?nameLayout?=?new?GridBagLayout();
namePanel?=?new?JPanel(nameLayout);
namePanel.setBorder(new?EmptyBorder(10,?10,?10,?10));
JLabel?nameLabel?=?new?JLabel("姓名:");
tfName?=?new?JTextField();
JLabel?psdLabel?=?new?JLabel("密碼:");
tfPsd?=?new?JTextField();
JLabel?blank?=?new?JLabel("?");
namePanel.add(nameLabel);
namePanel.add(tfName);
namePanel.add(blank);
namePanel.add(psdLabel);
namePanel.add(tfPsd);
GridBagConstraints?s?=?new?GridBagConstraints();
s.fill?=?GridBagConstraints.BOTH;
s.gridwidth?=?1;
s.weightx?=?0;
s.weighty?=?0;
nameLayout.setConstraints(nameLabel,?s);
s.gridwidth?=?0;
s.weightx?=?1;
s.weighty?=?0;
nameLayout.setConstraints(tfName,?s);
s.gridwidth?=?0;
s.weightx?=?4;
s.weighty?=?0;
nameLayout.setConstraints(blank,?s);
s.gridwidth?=?1;
s.weightx?=?0;
s.weighty?=?0;
nameLayout.setConstraints(psdLabel,?s);
s.gridwidth?=?3;
s.weightx?=?1;
s.weighty?=?0;
nameLayout.setConstraints(tfPsd,?s);
FlowLayout?btnLayout?=?new?FlowLayout();
btnLayout.setAlignment(FlowLayout.CENTER);
btnPanel?=?new?JPanel(btnLayout);
btnLogin?=?new?JButton("確定");
btnCancel?=?new?JButton("取消");
btnPanel.add(btnLogin);
btnPanel.add(btnCancel);
btnCancel.addActionListener(this);
btnLogin.addActionListener(this);
mainPanel.add(namePanel);
mainPanel.add(btnPanel);
setContentPane(mainPanel);
tfName.addFocusListener(this);
tfPsd.addFocusListener(this);
pack();
setSize(WIDTH,?HEIGHT);
setLocationRelativeTo(null);
}
@Override
public?void?actionPerformed(ActionEvent?e)?{
Object?source?=?e.getSource();
if(source?==?btnCancel)?{
System.exit(0);
}?else?if(source?==?btnLogin)?{
String?username?=?tfName.getText();
String?password?=?tfPsd.getText();
boolean?success?=?service.login(username,?password);
if(success)?{
warn("成功",?"登錄成功!");
}?else?{
warn("失敗",?"您輸入的用戶名或密碼錯誤?!");
}
}
}
@Override
public?void?focusGained(FocusEvent?arg0)?{
}
@Override
public?void?focusLost(FocusEvent?e)?{
Object?source?=?e.getSource();
if(source?==?tfName)?{
String?username?=?tfName.getText();
try?{
service.matchUsername(username);
}?catch?(LoginException?e1)?{
warn("驗證錯誤",?e1.getMessage());
}
}?else?if(source?==?tfPsd)?{
String?password?=?tfPsd.getText();
try?{
service.matchPassword(password);
}?catch?(LoginException?e1)?{
warn("驗證錯誤",?e1.getMessage());
}
}
}
private?void?warn(String?title,?String?msg)?{
JOptionPane.showMessageDialog(null,?msg,?title,?JOptionPane.INFORMATION_MESSAGE);
}
public?static?void?main(String[]?args)?{
new?LoinWindow().launch();
}
}
然后是模型層:LoginDao
public?class?LoginDao?{
public?boolean?login(String?username,?String?password)?{
if(username.equals("admin")??password.equals("12345"))?{
return?true;
}
return?false;
}
}
LoginService
import?java.util.regex.Pattern;
public?class?LoginService?{
private?static?final?Pattern?LOGIN_PATTERN?=?Pattern.compile("[a-zA-Z]+");
private?static?final?Pattern?PASSWORD_PATTERN?=?Pattern.compile("[1-9]+");
private?LoginDao?dao?=?new?LoginDao();
public?boolean?matchUsername(String?username)?throws?LoginException?{
if(null?==?username?||?username.isEmpty())?{
return?false;
}
if(!LOGIN_PATTERN.matcher(username).matches())?{
throw?new?LoginException("您輸入的用戶名不合法,請輸入英文!");
}
return?true;
}
public?boolean?matchPassword(String?password)?throws?LoginException?{
if(null?==?password?||?password.isEmpty())?{
return?false;
}
if(!PASSWORD_PATTERN.matcher(password).matches())?{
throw?new?LoginException("您輸入的密碼不合法,請輸入數(shù)字!");
}
return?true;
}
public?boolean?login(String?username,?String?password)?{
if(null?==?username?||?username.isEmpty())?{
return?false;
}
if(null?==?password?||?password.isEmpty())?{
return?false;
}
if(!dao.login(username,?password))?{
return?false;
}
return?true;
}
}
LoginException
public?class?LoginException?extends?Exception?{
public?LoginException(String?arg0)?{
super(arg0);
}
}
不知道分層設(shè)計思想是不是我想的這樣
import?java.util.Comparator;
public?class?Student?implements?ComparableStudent?{
private?int?no;
private?String?name;
private?String?sex;
private?int?roomNo;
private?double?score;
public?Student(int?no,?String?name,?String?sex,?int?roomNo,?double?score)?{
this.no?=?no;
this.name?=?name;
this.sex?=?sex;
this.roomNo?=?roomNo;
this.score?=?score;
}
public?Student(int?no,?String?name,?String?sex,?double?score)?{
this.no?=?no;
this.name?=?name;
this.sex?=?sex;
this.score?=?score;
}
public?int?getNo()?{
return?no;
}
public?void?setNo(int?no)?{
this.no?=?no;
}
public?String?getName()?{
return?name;
}
public?void?setName(String?name)?{
this.name?=?name;
}
public?String?getSex()?{
return?sex;
}
public?void?setSex(String?sex)?{
this.sex?=?sex;
}
public?int?getRoomNo()?{
return?roomNo;
}
public?void?setRoomNo(int?roomNo)?{
this.roomNo?=?roomNo;
}
public?double?getScore()?{
return?score;
}
public?void?setScore(double?score)?{
this.score?=?score;
}
@Override
public?int?compareTo(Student?o)?{
if?(this.no??o.no)?return?1;
else?if?(this.no??o.no)?return?-1;
else?return?0;
}
@Override
public?String?toString()?{
return?"Student{"?+
"no="?+?no?+
",?name='"?+?name?+?'\''?+
",?sex='"?+?sex?+?'\''?+
",?roomNo="?+?roomNo?+
",?score="?+?score?+
'}';
}
}
//性別比較器類
class?SexComparator?implements?ComparatorStudent?{
@Override
public?int?compare(Student?o1,?Student?o2)?{
if?(o1.getSex().compareTo(o2.getSex())??0)?return?1;
else?if?(o1.getSex().compareTo(o2.getSex())??0)?return?-1;
else?return?0;
}
}
//寢室號比較器類
class?RoomNoComparator?implements?ComparatorStudent?{
@Override
public?int?compare(Student?o1,?Student?o2)?{
if?(o1.getRoomNo()??o2.getRoomNo())?return?1;
else?if?(o1.getRoomNo()??o2.getRoomNo())?return?-1;
else?return?0;
}
}
//入學(xué)成績比較器類
class?ScoreComparator?implements?ComparatorStudent?{
@Override
public?int?compare(Student?o1,?Student?o2)?{
if?(o1.getScore()??o2.getScore())?return?1;
else?if?(o1.getScore()??o2.getScore())?return?-1;
else?return?0;
}
}
import?java.util.ArrayList;
import?java.util.Arrays;
import?java.util.Collections;
import?java.util.List;
public?class?TestStudent?{
public?static?void?main(String[]?args)?{
Student?s1?=?new?Student(1,?"jack",?"boy",?90);
Student?s2?=?new?Student(5,?"jack",?"boy",?90);
Student?s3?=?new?Student(4,?"jack",?"boy",?90);
Student?s4?=?new?Student(2,?"jack",?"boy",?90);
ListStudent?studentList=new?ArrayList();
studentList.add(s1);
studentList.add(s2);
studentList.add(s3);
studentList.add(s4);
Collections.sort(studentList);
System.out.println(Arrays.toString(studentList.toArray()));
}
}
java編寫這個通訊錄管理系統(tǒng)
java編寫這個通訊錄管理系統(tǒng)_Java如何實現(xiàn)通訊錄管理系統(tǒng)
咕嚕嚕在芬蘭
原創(chuàng)
關(guān)注
3點贊·2305人閱讀
Java如何實現(xiàn)通訊錄管理系統(tǒng)
發(fā)布時間:2020-07-28 09:39:42
來源:億速云
閱讀:65
作者:Leah
這篇文章將為大家詳細講解有關(guān)Java如何實現(xiàn)通訊錄管理系統(tǒng),文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關(guān)知識有一定的了解。
本文實例為大家分享了java實現(xiàn)通訊錄管理系統(tǒng)的具體代碼,供大家參考,具體內(nèi)容如下
完成項目的流程:
1.根據(jù)需求,確定大體方向
2.功能模塊分析
3.界面實現(xiàn)
4.功能模塊設(shè)計
5.coding
6.代碼測試
下面是源代碼:import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.concurrent.SynchronousQueue;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.WindowConstants;
import javax.swing.text.html.HTMLDocument.Iterator;
class Infro{
public String id;
public String name;
public String sex;
public String address;
public String e_mail;
public String phoneNumber;
static int index = 0;
static ArrayList list = new ArrayList();
static int len = list.size();
//構(gòu)造函數(shù)
public Infro(String id,String name,String sex,String address,String e_mail,String phoneNumber){
this.id = id;
this.name = name;
this.sex = sex;
this.address = address;
this.e_mail = e_mail;
this.phoneNumber = phoneNumber;
}
public String toString(){
return "編號:"+id+" 姓名:"+name+" 性別:"+sex+" 通訊地址:"+address+" 郵箱地址:"+e_mail+" 電話:"+phoneNumber;
}
/**
* 添加功能
**/
public static void addFunction(){//添加功能
Infro infro = new Infro("","","","","","");
System.out.println("請輸入添加的數(shù)據(jù):");
Scanner in = new Scanner(System.in);
System.out.println("輸入編號:");
infro.id = in.next();
System.out.println("輸入姓名:");
infro.name = in.next();
System.out.println("輸入性別:");
infro.sex = in.next();
System.out.println("輸入通訊地址:");
infro.address = in.next();
System.
out.println("輸入郵箱地址:");
infro.e_mail = in.next();
System.out.println("輸入電話:");
infro.phoneNumber = in.next();
list.add(index,infro);
index++;
if(list.isEmpty()){
System.out.println("數(shù)據(jù)添加失敗啦");
}else{
System.out.println("數(shù)據(jù)添加成功啦");
len++;//list集合長度加一
// System.out.println(list.get(0).toString());
}
}
// public static void deleteFunction(){//刪除功能
// System.out.println("輸入要刪除的聯(lián)系人的編號");
// Scanner in_2 = new Scanner(System.in);
// String d1 = in_2.nextLine();
// for(int a= 0; a
// if(d1.equals(list.get(a).id)){
// list.remove(list.get(a));
// len --;
// }
// }
// }
/**
* 刪除功能
**/
public static void deleteFunction(){
System.out.println("輸入要刪除的聯(lián)系人的編號");
Scanner in_2 = new Scanner(System.in);
String d1 = in_2.nextLine();
java.util.Iterator it = list.iterator();
while (it.hasNext()){
Infro infro = it.next();
if (infro.id.equals(d1)){
it.remove();
--index;//一定要加這個,否則當做了刪除操作再做添加操作的時候會出現(xiàn)異常(類似于指針,棧)
System.out.println("刪除完畢"+"此時通訊錄記錄條數(shù)為:" + --len);
}
}
}
/**
* 修改功能
**/
public static void reditFunction(){
System.out.println("輸入要修改的通訊錄的Id");
Scanner in_r = new Scanner(System.in);
String r1 = in_r.nextLine();
for(int a = 0; a len;a++){
if(r1.equals(list.get(a).id)){
System.out.println("輸入修改后的姓名:");
String name_1 = in_r.next();
list.get(a).name = name_1;
System.out.println("輸入修改后的性別:");
String sex_1 = in_r.next();
list.get(a).sex = sex_1;
System.out.println("輸入修改后的通訊地址:");
String address_1 = in_r.next();
list.get(a).address = address_1;
System.out.println("輸入修改后的郵箱地址:");
String e_mail_1 = in_r.next();
list.get(a).e_mail = e_mail_1;
System.out.println("輸入修改后的電話:");
String phoneNumber_1 = in_r.next();
list.get(a).phoneNumber = phoneNumber_1;
System.out.println("數(shù)據(jù)修改完畢");
}
}
}
/**
* 查詢功能
**/
public static void searchFunction() throws Exception{//查詢功能
System.out.println("請輸入要查詢的姓名:");
Scanner in_1 = new Scanner(System.in);
String s1=in_1.nextLine();
for(int a= 0; a
if(s1.equals(list.get(a).name)){
System.out.println(list.get(a).toString());
}
}
}
/**
* 顯示功能
**/
public static void showFunction(){
for(int i = 0 ;i
System.out.println(list.get(i).toString());
}
}
/**
* 保存功能
**/
public static void writeFunction() throws IOException{
FileWriter writer = new FileWriter("通訊錄管理.txt");
for(int i = 0 ;i
String []strwriter = new String[len];
strwriter[i]=list.get(i).toString();
writer.write(strwriter[i]);
writer.write("\r\n");
System.out.println("成功寫入一行數(shù)據(jù)到 通訊錄管理.txt 中");
}
writer.close();//關(guān)閉寫入流,釋放資源
}
/**
* 讀取功能
**/
public static void readFunction() throws IOException{
FileReader reader = new FileReader("通訊錄管理.txt");
BufferedReader br = new BufferedReader(reader);
String str;
while((str = br.readLine()) != null){//每次讀取一行文本,判斷是否到達文件尾
System.out.println(str);
}
br.close();
}
}
public class Demo extends JFrame {
/**
* 界面設(shè)計
**/
public Demo(){
Container c = getContentPane(); //定義一個頂級容器c
JPanel jp = new JPanel(); //新建JPanel面板--jp
JButton button1 = new JButton("新建聯(lián)系人");
JButton button2 = new JButton("刪除聯(lián)系人");
JButton button3 = new JButton("編輯聯(lián)系人");
JButton button4 = new JButton("查找聯(lián)系人");
JButton button5 = new JButton("顯示所有聯(lián)系人");
JButton button6 = new JButton("保存聯(lián)系人到本地");
JButton button7 = new JButton("讀取本地聯(lián)系人");
jp.setLayout(new GridLayout(2,4,5,5));//新建網(wǎng)格布局管理器(行數(shù),列數(shù),組件間的水平垂直間距)
jp.add(button1);
jp.add(button2);
jp.add(button3);
jp.add(button4);
jp.add(button5);
jp.add(button6);
jp.add(button7);
c.add(jp);//將JPanel面板jp添加到頂級容器c中
setSize(600,500);
setTitle("*通 訊 錄 管 理 系 統(tǒng)*");
setVisible(true);
setResizable(false);//窗體大小由程序員決定,用戶不能自由改變大小
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
/**
*按鍵響應(yīng)
*
**/
button1.addActionListener(new ActionListener(){//添加功能實現(xiàn)
public void actionPerformed(ActionEvent arg0){
Infro.addFunction();
}
});
button2.addActionListener(new ActionListener(){//刪除功能實現(xiàn)
public void actionPerformed(ActionEvent arg0){
Infro.deleteFunction();
}
});
button3.addActionListener(new ActionListener(){//修改功能實現(xiàn)
public void actionPerformed(ActionEvent arg0){
Infro.reditFunction();
}
});
button4.addActionListener(new ActionListener(){//查詢功能實現(xiàn)
public void actionPerformed(ActionEvent arg0){
try {
Infro.searchFunction();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
button5.addActionListener(new ActionListener(){//顯示功能實現(xiàn)
public void actionPerformed(ActionEvent arg0){
Infro.showFunction();
}
});
button6.addActionListener(new ActionListener(){//保存功能實現(xiàn)
public void actionPerformed(ActionEvent arg0){
try {
Infro.writeFunction();
} catch (IOException e) {
e.printStackTrace();
}
}
});
button7.addActionListener(new ActionListener(){//讀取功能實現(xiàn)
public void actionPerformed(ActionEvent arg0){
try {
Infro.readFunction();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new Demo();
Infro a = new Infro("", "", "", "", "", "");
}
}
關(guān)于Java如何實現(xiàn)通訊錄管理系統(tǒng)就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
import java.awt.Color;
import java.awt.Font;
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.SwingConstants;
import javax.swing.border.LineBorder;
public class game21 extends JFrame {
private JLabel label_2;
private int number;
private int sum;
final JLabel label = new JLabel();
final JLabel label_1 = new JLabel();
public static void main(String[] args) {
new game21();
}
public game21() {
super("21點?!");
getContentPane().setLayout(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final JButton button = new JButton();
button.addActionListener(new ActionListener() {
public void actionPerformed(final ActionEvent arg0) {
onClick();
}
});
button.setText("出牌");
button.setBounds(170, 350, 106, 28);
getContentPane().add(button);
label.setBorder(new LineBorder(Color.black, 1, false));
label.setHorizontalAlignment(SwingConstants.CENTER);
label.setFont(new Font("", Font.BOLD, 26));
label.setText("背面");
label.setBounds(158, 81, 137, 153);
getContentPane().add(label);
label_1.setText("你已經(jīng)擁有的牌:");
label_1.setBounds(109, 22, 270, 45);
getContentPane().add(label_1);
this.setBounds(200, 300, 501, 528);
this.setVisible(true);
getContentPane().add(getLabel_2());
}
public int randNumber() {
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
return (int) (Math.random() * 10 + 1);
}
public void onClick() {
number = this.randNumber();
this.sum += number;
label.setText("" + number);
String strTemp = this.label_1.getText();
strTemp += "" + number + " ";
label_1.setText(strTemp);
String temp = "合計:" + sum;
label_2.setText(temp);
isWin();
}
public void isWin() {
if (sum 21) {
JOptionPane.showMessageDialog(this, "你輸了");
clear();
return;
} else if (sum == 21) {
JOptionPane.showMessageDialog(this, "你贏了");
clear();
return;
} else {
int i = JOptionPane.showOptionDialog(this, "是否繼續(xù)?", "提示",
JOptionPane.OK_CANCEL_OPTION,
JOptionPane.INFORMATION_MESSAGE, null, null, null);
if (i == JOptionPane.OK_OPTION) {
onClick();
} else
return;
}
}
private void clear() {
label_2.setText("合計:");
sum = 0;
number = 0;
label_1.setText("你已經(jīng)擁有的牌:");
}
/**
* @return
*/
protected JLabel getLabel_2() {
if (label_2 == null) {
label_2 = new JLabel();
label_2.setText("合計:");
label_2.setBounds(313, 35, 66, 18);
}
return label_2;
}
}
真好無聊中。。