當前位置:首頁 » 編程語言 » java編寫系統

java編寫系統

發布時間: 2024-06-24 12:27:48

A. 使用java語言連接資料庫編寫一個簡單的學生信息管理系統


public static void findInfo(String filePath) throws IOException {

//把之前存入到數據的文件,讀取到集合中來。

ArrayList<Student> list = new ArrayList<Student>();

readData(list,filePath);

//遍歷集合

for(int i=0;i<list.size();i++) {

Student stu = list.get(i);

System.out.println(stu.getId()+" "+stu.getName()+" "+stu.getAddress());

}

}

private static void readData(ArrayList<Student> list ,String filePath) throws NumberFormatException, IOException{

FileReader fr = new FileReader(filePath);

BufferedReader br = new BufferedReader(fr);

//讀物文件裡面的信息

String line = null;

while((line=br.readLine())!=null) {

String[] str = line.split(",");

//獲取的數據封裝成對象

//stu.getId()+","+stu.getName()+","+stu.getAge()

Student stu = new Student();

stu.setId(str[0]);

stu.setName(str[1]);

stu.setAge(Integer.valueOf(str[2]));

//將對象放到集合中區

list.add(stu);

}

}

//輸入學生的信息

public static void addInfo(String filePath) throws IOException{

ArrayList<Student> list = new ArrayList<Student>();

Scanner sc = new Scanner(System.in);

//將輸入的信息存放到集合裡面去

for(int i=1;i<=3;i++) {

System.out.println("請輸入第"+i+"個學生的id");

String id = sc.next();

System.out.println("請輸入第"+i+"個學生的name");

String name = sc.next();

System.out.println("請輸入第"+i+"個學生的age");

int age = sc.nextInt();

Student stu = new Student();

stu.setId(id);

stu.setAge(age);

stu.setName(name);

list.add(stu);

}

//將集合裡面的信息寫到文件裡面去

writeDate(list,filePath);

}

B. Java實現學生簡易信息管理系統(java學生信息管理系統設計)

importjava.util.*;

importjava.io.*;

classStuMgr{

publicstaticclassStudent{

publicintid;

publicStringname;

publicintage;

publicStudent(intid,Stringname,intage){

this.id=id;

this.name=name;

this.age=age;

}

@Override

publicStringtoString(){

returnid","name","age;

}

}

publicListstuList=newLinkedList<>();

publicvoidadd(){

Scannersc=newScanner(System.in);

System.out.println("請輸入學生學號:");

Stringid=sc.nextLine();

intintId=0;

try{

intId=Integer.parseInt(id);

}catch(ex){

System.out.println("學號輸入有誤,請輸入數字!");

return;

}

if(find(intId)!=null){

System.out.println("該學號已經存在!");

return;

}

System.out.println("請輸入學生姓名:");

Stringname=sc.nextLine();

System.out.println("請輸入學生年齡:");

Stringage=sc.nextLine();

intintAge=0;

try{

intAge=Integer.parseInt(age);

}catch(ex){

System.out.println("年齡輸入有誤,請輸入絕胡激數字!");

return;

}

Studentstu=newStudent(intId,name,intAge);

stuList.add(stu);

store();

System.out.println("-----------------------");

System.out.println("學生信息已增加");

System.out.println(stu);

System.out.println("-----------------------");

}

publicvoiddel(){

Scannersc=newScanner(System.in);

System.out.println("請輸入學生學號:");

Stringid=sc.nextLine();

intintId=0;

try{

intId=Integer.parseInt(id);

}catch(ex){

System.out.println("學號輸入有誤,請輸入數字!");

return;

}

Studentstu=find(intId);

if(stu==null){

System.out.println("該學號不存在!");

return;

}

stuList.remove(stu);

store();

System.out.println("-----------------------");

System.out.println("學生信息已刪除");

System.out.println(stu);

System.out.println("-----------------------");

}

publicvoidfind(){

Scannersc=newScanner(System.in);

System.out.println("請輸入學生學號:做棗");

Stringid=sc.nextLine();

intintId=0;

try{

intId=Integer.parseInt(id);

}catch(ex){

System.out.println("學號輸入有誤並襪,請輸入數字!");

return;

}

Studentstu=find(intId);

if(stu==null){

System.out.println("該學號不存在!");

return;

}

System.out.println("-----------------------");

System.out.println("查找學生信息如下");

System.out.println(stu);

System.out.println("-----------------------");

}

publicStudentfind(intid){

for(Studentstu:stuList){

if(stu.id==id){

returnstu;

}

}

returnnull;

}

publicvoidmodify(){

store();

}

publicvoidforeach(){

System.out.println("-----------------------");

for(Studentstu:stuList){

System.out.println(stu);

}

System.out.println("-----------------------");

}

publicvoidstore(){

Iteratoriterator=stuList.iterator();

Filefile=newFile("stuList.txt");

FileWriterfw=null;

writer=null;

try{

fw=newFileWriter(file);

writer=new(fw);

while(iterator.hasNext()){

writer.write(iterator.next().toString());

writer.newLine();//換行

}

writer.flush();

}catch(e){

e.();

}catch(IOExceptione){

e.();

}finally{

try{

writer.close();

fw.close();

}catch(IOExceptione){

e.();

}

}

}

publicstaticvoidmain(String[]args){

StuMgrmgr=newStuMgr();

while(true){

System.out.println("請選擇您要進行的操作:");

System.out.println("1:增加學生信息");

System.out.println("2:刪除學生信息");

System.out.println("3:查找學生信息");

System.out.println("4:修改學生信息");

System.out.println("5:遍歷學生信息");

System.out.println("6:退出");

System.out.println("-----------------------");

Scannersc=newScanner(System.in);

Stringop=sc.nextLine();

if("6".equals(op)){

return;

}

if("1".equals(op)){

mgr.add();

}

if("2".equals(op)){

mgr.del();

}

if("3".equals(op)){

mgr.find();

}

if("4".equals(op)){

mgr.modify();

}

if("5".equals(op)){

mgr.foreach();

}

}

}

}

時間倉促,還有一個modify方法沒實現,留給你自己練手。

C. 用java基礎編寫一個簡單的學生管理系統,有如下功能,添加學生,刪除學生,查詢學生。看好是用JAVA基礎。

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.util.*;

class Student implements java.io.Serializable{
String number,name,specialty,grade,borth,sex;
public Student(){};
public void setNumber(String number){ this.number=number;}
public String getNumber(){ return number;}
public void setName(String name){ this.name=name;}
public String getName(){ return name;}
public void setSex(String sex){ this.sex=sex;}
public String getSex(){ return sex;}
public void setSpecialty(String specialty){ this.specialty=specialty;}
public String getSpecialty(){ return specialty;}
public void setGrade(String grade){ this.grade=grade;}
public String getGrade(){ return grade;}
public void setBorth(String borth){ this.borth=borth;}
public String getBorth(){ return borth;}
}
public class StudentManager extends JFrame{
JLabel lb=new JLabel("錄入請先輸入記錄,查詢、刪除請先輸入學號,修改是對查詢" +
"內容改後的保存!");
JTextField 學號,姓名,專業,年級,出生;
JRadioButton 男,女;
ButtonGroup group=null;
JButton 錄入,查詢,刪除,修改,顯示;
JPanel p1,p2,p3,p4,p5,p6,pv,ph;
Student 學生=null;
Hashtable 學生散列表=null;
File file=null;
FileInputStream inOne=null;
ObjectInputStream inTwo=null;
FileOutputStream outOne=null;
ObjectOutputStream outTwo=null;
public StudentManager(){
super("學生基本信息管理系統");
學號=new JTextField(10);
姓名=new JTextField(10);
專業=new JTextField(10);
年級=new JTextField(10);
出生=new JTextField(10);
group=new ButtonGroup();
男=new JRadioButton("男",true);
女=new JRadioButton("女",false);
group.add(男);
group.add(女);
錄入=new JButton("錄入");
查詢=new JButton("查詢");
刪除=new JButton("刪除");
修改=new JButton("修改");
顯示=new JButton("顯示");
錄入.addActionListener(new InputAct());
查詢.addActionListener(new InquestAct());
修改.addActionListener(new ModifyAct());
刪除.addActionListener(new DeleteAct());
顯示.addActionListener(new ShowAct());
修改.setEnabled(false);
p1=new JPanel();
p1.add(new JLabel("學號:",JLabel.CENTER));
p1.add(學號);
p2=new JPanel();
p2.add(new JLabel("姓名:",JLabel.CENTER));
p2.add(姓名);
p3=new JPanel();
p3.add(new JLabel("性別:",JLabel.CENTER));
p3.add(男);
p3.add(女);
p4=new JPanel();
p4.add(new JLabel("專業:",JLabel.CENTER));
p4.add(專業);
p5=new JPanel();
p5.add(new JLabel("年級:",JLabel.CENTER));
p5.add(年級);
p6=new JPanel();
p6.add(new JLabel("出生:",JLabel.CENTER));
p6.add(出生);
pv=new JPanel();
pv.setLayout(new GridLayout(6,1));
pv.add(p1);
pv.add(p2);
pv.add(p3);
pv.add(p4);
pv.add(p5);
pv.add(p6);
ph=new JPanel();
ph.add(錄入);
ph.add(查詢);
ph.add(修改);
ph.add(刪除);
ph.add(顯示);
file=new File("學生信息.txt");
學生散列表=new Hashtable();
if(!file.exists()){
try{
FileOutputStream out=new FileOutputStream(file);
ObjectOutputStream objectOut=new ObjectOutputStream(out);
objectOut.writeObject(學生散列表);
objectOut.close();
out.close();
}
catch(IOException e){}
}
Container con=getContentPane();
con.setLayout(new BorderLayout());
con.add(lb, BorderLayout.NORTH);
con.add(pv, BorderLayout.CENTER);
con.add(ph, BorderLayout.SOUTH);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setBounds(100,100,600,300);
setVisible(true);

}
public static void main(String[] args) {new StudentManager();}
class InputAct implements ActionListener{
public void actionPerformed(ActionEvent e){
修改.setEnabled(false);
String number="";
number=學號.getText();
if(number.length()>0){
try{
inOne=new FileInputStream(file);
inTwo=new ObjectInputStream(inOne);
學生散列表=(Hashtable)inTwo.readObject();
inOne.close();
inTwo.close();
}
catch(Exception ee){System.out.println("創建散列表出現問題!");}
if(學生散列表.containsKey(number)){
String warning="該生信息已存在,請到修改頁面修改!";
JOptionPane.showMessageDialog(null,warning,"警告",
JOptionPane.WARNING_MESSAGE);
}//end if1
else{
String m="該生信息將被錄入!";
int ok=JOptionPane.showConfirmDialog(null,m,"確認",
JOptionPane.YES_NO_OPTION,JOptionPane.INFORMATION_MESSAGE);
if(ok==JOptionPane.YES_OPTION){
String name=姓名.getText();
String specialty=專業.getText();
String grade=年級.getText();
String borth=出生.getText();
String sex=null;
if(男.isSelected()){sex=男.getText();}
else{sex=女.getText();}
學生=new Student();
學生.setNumber(number);
學生.setName(name);
學生.setSpecialty(specialty);
學生.setGrade(grade);
學生.setBorth(borth);
學生.setSex(sex);
try{
outOne=new FileOutputStream(file);
outTwo=new ObjectOutputStream(outOne);
學生散列表.put(number,學生);
outTwo.writeObject(學生散列表);
outTwo.close();
outOne.close();
}
catch(Exception ee){System.out.println("輸出散列表出現問題!");}
學號.setText(null);
姓名.setText(null);
專業.setText(null);
年級.setText(null);
出生.setText(null);
}
}//end else1
}//end if0
else{
String warning="必須輸入學號!";
JOptionPane.showMessageDialog(null,warning,
"警告",JOptionPane.WARNING_MESSAGE);
}//end else0
}//end actionPerformed
}//end class
class InquestAct implements ActionListener{
public void actionPerformed(ActionEvent e){
String number="";
number=學號.getText();
if(number.length()>0){
try{
inOne=new FileInputStream(file);
inTwo=new ObjectInputStream(inOne);
學生散列表=(Hashtable)inTwo.readObject();
inOne.close();
inTwo.close();
}
catch(Exception ee){System.out.println("散列表有問題!");}
if(學生散列表.containsKey(number)){
修改.setEnabled(true);
Student stu=(Student)學生散列表.get(number);
姓名.setText(stu.getName());
專業.setText(stu.getSpecialty());
年級.setText(stu.getGrade());
出生.setText(stu.getBorth());
if(stu.getSex().equals("男")){男.setSelected(true);}
else{女.setSelected(true);}
}
else{
修改.setEnabled(false);
String warning="該學號不存在!";
JOptionPane.showMessageDialog(null,warning,
"警告",JOptionPane.WARNING_MESSAGE);
}
}
else{
修改.setEnabled(false);
String warning="必須輸入學號!";
JOptionPane.showMessageDialog(null,warning,
"警告",JOptionPane.WARNING_MESSAGE);
}
}
}
class ModifyAct implements ActionListener{
public void actionPerformed(ActionEvent e){
String number=學號.getText();
String name=姓名.getText();
String specialty=專業.getText();
String grade=年級.getText();
String borth=出生.getText();
String sex=null;
if(男.isSelected()){sex=男.getText();}
else{sex=女.getText();}
Student 學生=new Student();
學生.setNumber(number);
學生.setName(name);
學生.setSpecialty(specialty);
學生.setGrade(grade);
學生.setBorth(borth);
學生.setSex(sex);
try{
outOne=new FileOutputStream(file);
outTwo=new ObjectOutputStream(outOne);
學生散列表.put(number, 學生);
outTwo.writeObject(學生散列表);
outTwo.close();
outOne.close();
學號.setText(null);
姓名.setText(null);
專業.setText(null);
年級.setText(null);
出生.setText(null);
}
catch(Exception ee){
System.out.println("錄入修改出現異常!");
修改.setEnabled(false);
}
}
}
class DeleteAct implements ActionListener{
public void actionPerformed(ActionEvent e){
修改.setEnabled(false);
String number=學號.getText();
if(number.length()>0){
try{
inOne=new FileInputStream(file);
inTwo=new ObjectInputStream(inOne);
學生散列表=(Hashtable)inTwo.readObject();
inOne.close();
inTwo.close();
}
catch(Exception ee){}
if(學生散列表.containsKey(number)){
Student stu=(Student)學生散列表.get(number);
姓名.setText(stu.getName());
專業.setText(stu.getSpecialty());
年級.setText(stu.getGrade());
出生.setText(stu.getBorth());
if(stu.getSex().equals("男")){男.setSelected(true);}
else{女.setSelected(true);}
}
String m="確定要刪除該學生的記錄嗎?";
int ok=JOptionPane.showConfirmDialog(null,m,"確認",
JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE);
if(ok==JOptionPane.YES_OPTION){
學生散列表.remove(number);
try{
outOne=new FileOutputStream(file);
outTwo=new ObjectOutputStream(outOne);
outTwo.writeObject(學生散列表);
outTwo.close();
outOne.close();
學號.setText(null);
姓名.setText(null);
專業.setText(null);
年級.setText(null);
出生.setText(null);
}
catch(Exception ee){System.out.println(ee);}

}
else if(ok==JOptionPane.NO_OPTION){
學號.setText(null);
姓名.setText(null);
專業.setText(null);
年級.setText(null);
出生.setText(null);
}
else{
String warning="該學號不存在!";
JOptionPane.showMessageDialog(null,warning,
"警告",JOptionPane.WARNING_MESSAGE);
}
}
else{
String warning="必須輸入學號!";
JOptionPane.showMessageDialog(null,warning,
"警告",JOptionPane.WARNING_MESSAGE);
}
}
}
class ShowAct implements ActionListener{
public void actionPerformed(ActionEvent e){
new StudentShow(file);
}
}
class StudentShow extends JDialog{
Hashtable 學生散列表= null;
JTextArea 顯示=null;
FileInputStream inOne=null;
ObjectInputStream inTwo=null;
File file=null;
public StudentShow(File file){
super(new JFrame(),"顯示對話框");
this.file=file;
顯示=new JTextArea(16,30);
try{
inOne=new FileInputStream(file);
inTwo=new ObjectInputStream(inOne);
學生散列表=(Hashtable)inTwo.readObject();
inOne.close();
inTwo.close();
}
catch(Exception ee){}
if(學生散列表.isEmpty())顯示.append("目前還沒有學生的信息記錄!\n");
else{
顯示.setText("學號 姓名 性別 專業 年級 出生\n");
for(Enumeration enm=學生散列表.elements();enm.hasMoreElements();){
Student stu=(Student)enm.nextElement();
String sex="";
if(stu.getSex().equals("男"))sex="男";
else sex="女";
String str=stu.getNumber()+","+stu.getName()+","+sex+","
+stu.getSpecialty()+","+stu.getGrade()+","+stu.getBorth()+"\n";
顯示.append(str);
}
}
JScrollPane scroll=new JScrollPane(顯示);
Container con=getContentPane();
con.add("Center",scroll);
con.validate();
setVisible(true);
setBounds(200,200,400,300);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){setVisible(false);}
}
);
}
}
}

D. JAVA編寫一個界面 用戶登陸系統

import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
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;

@SuppressWarnings("serial")
public class MainFrame extends JFrame {
JLabel lbl1 = new JLabel("用戶名:");
JLabel lbl2 = new JLabel("密 碼:");
JTextField txt = new JTextField("admin",20);
JPasswordField pwd = new JPasswordField(20);
JButton btn = new JButton("登錄");
JPanel pnl = new JPanel();
private int error = 0;

public MainFrame(String title) throws HeadlessException {
super(title);
init();
}

private void init() {
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(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if ("admin".equals(new String(pwd.getPassword()))){
pnl.removeAll();
JLabel lbl3 = new JLabel();
ImageIcon icon = new ImageIcon(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);
}
}
}
});
}

public static void main(String[] args) {
MainFrame frm = new MainFrame("測試");
frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frm.setBounds(100, 100, 300, 120);
frm.setVisible(true);
}
}

隨手寫的, 沒調試圖片, 太麻煩

E. 用java編寫一個學生成績管理系統. //1.學生類 學號 姓名 年齡 語數外三科成績(三科成績)

  1. 用數組可以,也可以用集合來存儲各個同學的成績。

  2. 添加刪除等信息簡單。

這里有個迷你圖書管理系統,類似,我剛寫的。你看看之後應該會有思路了。如果用集合utils做的話,也簡單,知識用到了集合框架和泛型等機制。

package cn.kgc.sg.MiTuSys;


import java.util.Scanner;


public class MiNiBookSys {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

// 定義書的信息

String[] bookNames = new String[4];

int[] borrowDate = new int[5];

int[] borrowCount = new int[4];

int[] states = new int[4];

// 定義書單信息

String[] bookMeg = new String[5];

bookMeg[0] = "Java開發";

bookMeg[1] = "C#開發";

bookMeg[2] = "C語言開發";

states[2] = 0; // 0:可借閱 1:已借出


// 初始化兩個圖書借閱記錄信息

bookNames[0] = "安徒生童話";

borrowDate[0] = 15;

borrowCount[0] = 4;

states[0] = 0; // 0:可借閱 1:已借出

bookNames[1] = "格林童話";

states[1] = 1; // 0:可借閱 1:已借出

borrowDate[1] = 26;

borrowCount[1] = 9;


// 搭建項目框架

int num = -1;// 定義用戶初始輸入的數字

do {

System.out.println("*********************歡迎使用圖書管理系統**********************");

System.out.println(" 1.新增圖書");

System.out.println(" 2.查看圖書");

System.out.println(" 3.刪除圖書");

System.out.println(" 4.圖書借閱");

System.out.println(" 5.歸還圖書");

System.out.println(" 6.退出系統");


System.out.print("請輸入您的選擇:");

int choose = sc.nextInt();

// 判斷輸入的選擇是否正確

while (choose < 0 || choose > 6) {

System.out.print("輸入有誤,請重新輸入:");

choose = sc.nextInt();

}

// 定義一個flag變數,來定義是否退出系統

boolean flag = true;// true:不退出系統 false:退出系統

String addName = null;

switch (choose) {

case 1:

System.out.println(" *********1.新增圖書*********");

boolean isAdd = true;

System.out.println("序號 圖書書名");

for (int i = 0; i < bookMeg.length; i++) {

// 判斷為空的不輸出

if (bookMeg[i] != null) {

System.out.println((i + 1) + " " + bookMeg[i]);

}

}

System.out.print("請輸入新增圖書名稱:");

addName = sc.next();

for (int i = 0; i <bookMeg.length; i++) {

// 判斷為空的不輸出

if (bookMeg[i] == null) {

bookMeg[i] = addName;

break;

}

}

/*bookMeg[bookMeg.length-1] = addName; //添加圖書信息

*/ System.out.println(" 序號 圖書書名");

for (int i = 0; i < bookMeg.length; i++) {

// 判斷為空的不輸出

if (bookMeg[i] != null) {

System.out.println((i + 1) + " " + bookMeg[i]);

}

}

if (!isAdd) {

System.out.println("對不起,圖書已滿,不能添加!!!");

}

break;


case 2:

System.out.println(" *********2.查看圖書*********");

System.out.println("序號" + " 圖書書名" + " 借閱次數 借閱狀態 借閱日期 ");

// 遍歷數組

for (int i = 0; i < bookNames.length; i++) {

// 判斷信息為空的不輸出

if (bookNames[i] != null) {

String borrowCounts = borrowCount[i] + "次";

String borrowDates = borrowDate[i] + "日";

String state = (states[i] == 0) ? "可借閱" : "已借出";

System.out.println((i + 1) + " " + bookNames[i] + " " + borrowCounts + " " + state

+ " " + borrowDates);

}

}

break;


case 3:

System.out.println(" *********3.刪除圖書*********");

System.out.println("序號 圖書書名");

for (int i = 0; i < bookMeg.length; i++) {

// 判斷為空的不輸出

if (bookMeg[i] != null) {

System.out.println((i + 1) + " " + bookMeg[i]);

}

}

System.out.print("請輸入刪除的圖書序號:");

int delNo = sc.nextInt();

// 判斷輸入的序號是否有誤

while (delNo < 1 || delNo > bookMeg.length) {

System.out.print("輸入有誤,重新輸入:");

delNo = sc.nextInt();

}

// 刪除圖書

for (int i = 0; i <=bookMeg.length-1;i++) {

if (bookMeg[i] != null && i == delNo - 1) {

bookMeg[i] = bookMeg[i+1];

bookMeg[i+1] = null; //

// 最後置空

bookMeg[bookMeg.length - 1] = null;

}

}

for (int i = 0; i < bookMeg.length; i++) {

// 判斷為空的不輸出

if (bookMeg[i] != null) {

System.out.println((i + 1) + " " + bookMeg[i]);

}

}

break;


case 4:

System.out.println(" *********4.圖書借閱*********");

System.out.println(" 序號 圖書書名 借閱狀態 借閱次數 借閱日期");

for (int i = 0; i < bookMeg.length; i++) {

// 判斷為空的不輸出

if (bookMeg[i] != null) {

String state = (states[i] == 0) ? "可借閱" : "已借出";

String borrowCi = borrowCount[i] + "次";

System.out.println((i + 1) + " " + bookMeg[i] + " " + state + " " + borrowCi + " "

+ borrowDate[i]);

}

}

System.out.print("請輸入您要借閱的圖書序號:");

int borrowNo = sc.nextInt();

boolean isFind = true;// false:找不到


for (int i = 0; i < bookMeg.length; i++) {

// 查到了,但是狀態是0可借閱,可以借閱

if (i == borrowNo - 1 && states[i] == 0) {

isFind = true;

// 改變狀態為已借出

states[i] = 1;

// 借閱次數加一

borrowCount[i]++;

// 輸入借閱日期

System.out.print("請輸入借閱日期:");

int borrow = sc.nextInt();

// 對輸入的日期判斷

while (borrow < 1 || borrow > 31) {

System.out.print("您輸入日期有誤,請重新輸入:");

borrow = sc.nextInt();

}

// 更新借閱日期

borrowDate[i] = borrow;

} else if (i == borrowNo - 1 && states[i] == 1) {

// 查到了,但是狀態是1已借出,不可以借閱

isFind = true;

System.out.println("狀態是已借出,不可以借閱!!!");

}

}

if (!isFind) {

System.out.println("找不到圖書!");

break;

}

break;


case 5:

System.out.println(" *********5.歸還圖書*********");

System.out.println(" 序號 圖書書名 借閱狀態 借閱次數 借閱日期");

for (int i = 0; i < bookMeg.length; i++) {

// 判斷為空的不輸出

if (bookMeg[i] != null) {

String state = (states[i] == 0) ? "可借閱" : "已借出";

String borrowCi = borrowCount[i] + "次";

System.out.println((i + 1) + " " + bookMeg[i] + " " + state + " " + borrowCi + " "

+ borrowDate[i]);

}

}

System.out.print("請輸入您要歸還的圖書序號:");

int huanNo = sc.nextInt();

// 判斷歸還的序號是否合法

while (huanNo < 1 || huanNo > bookMeg.length - 1) {

System.out.print("輸入的歸還圖書序號有誤,請重新輸入:");

huanNo = sc.nextInt();

}

boolean isHuan = true;// false:找不到

for (int i = 0; i < bookMeg.length; i++) {

// 查到了,但是狀態是1已借出,可以還

if (i == huanNo - 1 && states[i] == 1) {

isHuan = true;

// 改變狀態為已借出

states[i] = 0;

// 輸入歸還日期

System.out.print("請輸入歸還日期:");

int huan = sc.nextInt();

// 對歸還的日期判斷,必須大於借閱日期

while (huan < 1 || huan > 31) {

System.out.print("您輸入日期有誤,請重新輸入(1-31):");

huan = sc.nextInt();

}

while (huan < borrowDate[i]) {

System.out.print("歸還日期必須大於借閱日期" + borrowDate[i] + "號,請重新輸入:");

huan = sc.nextInt();

}

borrowCount[i]++;

System.out.println("歸還圖書成功!!!");


} else if (i == huanNo - 1 && states[i] == 0) {

// 查到了,但是狀態是0可借閱,不能還

isFind = true;

System.out.println("狀態是可借閱,不可以歸還!!!");

}

}

if (!isHuan) {

System.out.println("抱歉,找不到圖書,不能歸還圖書!");

break;

}

break;


case 6:

System.out.println(" *********6.退出系統*********");

flag = false;

break;

default:

flag = false;

break;

}

if (flag) {

System.out.print("輸入0返回:");

num = sc.nextInt();

} else {

break;

}

} while (num == 0);

System.out.println("**********謝謝使用**********");

}

}

希望能幫到你!!!

熱點內容
linuxpython教程 發布:2024-06-28 18:06:41 瀏覽:937
伺服器搭建需要哪些技術 發布:2024-06-28 18:05:16 瀏覽:669
伺服器管理中心是干什麼的 發布:2024-06-28 17:39:31 瀏覽:943
黑色存儲卡 發布:2024-06-28 17:22:43 瀏覽:450
php腳本運行時間 發布:2024-06-28 17:08:20 瀏覽:198
打開我的世界伺服器連接不上 發布:2024-06-28 17:08:19 瀏覽:213
android圖片的縮放 發布:2024-06-28 17:07:56 瀏覽:6
tomcat資料庫配置文件 發布:2024-06-28 17:03:12 瀏覽:38
qq有哪些文件夾 發布:2024-06-28 16:58:48 瀏覽:634
php計算時間戳 發布:2024-06-28 16:58:45 瀏覽:935