程序的功能:所以我有一個應該給用戶4個選項的程序......添加學生,刪除學生,打印學生信息並退出。如果用戶輸入1,那麼添加的學生將被選中,它會詢問你想添加多少學生。然後,他們將輸入他們需要添加的人數,他們將填寫學生課程添加的所有內容。如果選擇了選項2,則會按名稱從鏈接列表中刪除學生。如果選擇了選項3,那麼它只會打印學生姓名,郵編和專業(其他人將用於不同的項目)。如果選擇了選項4,它將退出。它將繼續顯示該菜單,直到選擇4。鏈接列表學生管理系統
我的問題是:我需要幫助將學生添加到鏈接列表中,並通過學生的名字將其刪除。還有 如果有人可以向我解釋toString(我的老師告訴我這樣做),這將非常感激。
學生類別:
public class Student {
private int studentID;
private String fName;
private String lName;
private String Address;
private String city;
private String state;
private String zip;
private String major;
public void Student(){
}
public void Student(int i, String f, String l, String a, String c, String s, String z, String m){
studentID = i;
fName = f;
lName = l;
Address = a;
city = c;
state = s;
zip = z;
major = m;
}
public int getStudentID(){
return studentID;
}
public void setStudentID(int i){
studentID = i;
}
public String getFName(){
return fName;
}
public void setFName(String f){
fName = f;
}
public String getLName(){
return lName;
}
public void setLName(String l){
lName = l;
}
public String getAddress(){
return Address;
}
public void setAddress(String a){
Address = a;
}
public String getCity(){
return city;
}
public void setCity(String c){
city = c;
}
public String getState(){
return state;
}
public void setState(String s){
state = s;
}
public String getZip(){
return zip;
}
public void setZip(String z){
zip = z;;
}
public String getMajor(){
return major;
}
public void setMajor(String m){
major = m;
}
}
鏈表類:
public class LinkedList {
private class Node{
String value;
Node next;
Node (String val, Node n){
value=val;
next = n;
}
Node (String val)
{
this(val, null);
}
}
private Node first;
private Node last;
public LinkedList(){
first = null;
last = null;
}
public boolean isEmpty(){
return first== null;
}
public int size(){
int count = 0;
Node p = first;
while (p !=null){
count++;
p = p.next;
}
return count;
}
public void add(String s){
if (isEmpty())
{
first = new Node(s);
last = first;
}
else
{
last.next = new Node(s);
last = last.next;
}
}
public void add(int index, String s){
if (index <0 || index > size()){
String message = String.valueOf(index);
throw new IndexOutOfBoundsException(message);
}
if (index ==0){
first = new Node(s, first);
if (last == null)
last = first;
return;
}
Node pred = first;
for (int k = 1; k <= index -1; k++){
pred = pred.next;
}
pred.next = new Node (s, pred.next);
if (pred.next.next == null){
last = pred.next;
}
}
public String toString(){
StringBuilder strBuilder = new StringBuilder();
Node p = first;
while (p != null){
strBuilder.append(p.value+"\n");
p = p.next;
}
return strBuilder.toString();
}
public String remove(int index){
if (index <0 || index >=size()){
String message = String.valueOf(index);
throw new IndexOutOfBoundsException(message);
}
String element;
if (index == 0){
element = first.value;
first = first.next;
if (first == null){
last = null;
}
}
else
{
Node pred = first;
for (int k = 1; k <=index -1; k++)
pred = pred.next;
element = pred.next.value;
pred.next = pred.next.next;
if (pred.next == null)
last = pred;
}
return element;
}
}
主(這是非常糟糕的,到目前爲止):
import java.util.Scanner;
public class StudentDemo {
public static void main(String[] args) {
Scanner key = new Scanner(System.in);
LinkedList list = new LinkedList();
menu();
int input = key.nextInt();
if (input == 1){
if (input ==1){
System.out.println("Enter name");
String name = key.nextLine();
list.add(name);
System.out.println(list);
}
}
}
public static void menu(){
System.out.println("Student Maintenence System:");
System.out.println("1. Add Student");
System.out.println("2. Remove Student");
System.out.println("3. Print Student Information");
System.out.println("4. Exit");
}
public static void optionOne(int input, LinkedList list, Scanner key){
if (input ==1){
System.out.println("Enter name");/*supposed to ask for a lot more but for the time being its only asking this*/
String name = key.nextLine();
list.add(name);
list.toString();
}
}
}
請自己做你的任務,先自己嘗試一下,如果你遇到任何問題,請在這裏提問。 – Omore