我的程序是关于电话目录我使用的链接列表我有这个代码的问题它没有语法错误但是当我搜索和列表时我也看不到答案我也希望你添加更新方法< / p>
这是第一堂课:
//-----------------------------------------------------------------------------------------
class tel
{
private static String name;
private static String address;
private static int phoneNo;
private tel next;
private tel prev;
public tel(){
}
public tel(String name1,String address1 ,int phoneNo1)
{
name = name1 ;
address = address1;
phoneNo = phoneNo1;
}
public static String getName() {
return name;
}
public void setName(String name1) {
name = name1;
}
public static String getAddress() {
return address;
}
public void setAddress(String address1) {
address = address1;
}
public static int getPhoneNo() {
return phoneNo;
}
public void setPhoneNo(int phoneNo1) {
phoneNo = phoneNo1;
}
public tel getNext() {
return next;
}
public void setNext(tel next1) {
next = next1;
}
public tel getPrev() {
return prev;
}
public void setPrev(tel prev1) {
prev = prev1;
}
public void display()
{
System.out.println("Name : " + getName() + " PhoneNo : " + getPhoneNo() + " Address : " + getAddress());
}
}
//-------------------------------------------------------------------------------------------------------------------
//2ed class
public class telList {
private tel first;
private tel last;
private tel cur ;
public telList()
{
first = null ;
last = null ;
}
public boolean isEmpty()
{return first == null; }
public tel getCur() {
return cur;
}
public void setCur(tel cur1) {
cur = cur1;
}
public void insert(String na,String addr ,int pho)
{
tel newtel = new tel(na,addr,pho);
if (isEmpty())
{
last = newtel;
first = newtel;
}
else
{
newtel.setNext(first);
first.setPrev(newtel);
first = newtel;
}
}
//////////////////////////////////
public tel search(String Key)
{
tel cur = first;
while(cur.getName() != Key)
{
if(cur.getNext()== null)
return null;
else
cur = cur.getNext();
}
return cur;
}
public void displaythelist()
{
tel cur = first;
while(cur != null)
{
System.out.println(cur.display());
cur = cur.getNext();
}
System.out.println(" ");
}
}
//3ed class
//-------------------------------------------------------------------------------------------------------------------
import java.io.*;
import java.util.*;
public class telTest {
public static void main(String[] args)throws Exception {
Scanner input = new Scanner(System.in);
boolean quit = false;
do {
int menu = 0;
System.out.println("******************************");
System.out.println("Telephone Directory");
System.out.println();
System.out.println("1. Accept Data");
System.out.println("2. Search");
System.out.println("3. List of all persons");
System.out.println("4. Exit");
System.out.print("Please enter your choice: ");
menu = input.nextInt();
System.out.println();
tel t = new tel();
telList t1 = new telList();
switch (menu) {
case 1:
System.out.print("Enter Name: ");
String name = input.next();
t.setName(name);
System.out.print("Enter Address: ");
String address = input.next();
t.setAddress(address);
System.out.print("Enter Phone No: ");
int no = input.nextInt();
t.setPhoneNo(no);
t1. insert(t.getName(),t.getAddress(),t.getPhoneNo());
break ;
//...........................................................................
case 2:
System.out.print("Enter name to search information: ");
String n = input.next();
try {
t1.search(t.getName());
}
catch (Exception e) {
}
break;
//...........................................................................
case 3://list
try {
t1.displaythelist();
}
catch (Exception e) {
}
break;
//...........................................................................
case 4 ://exit
quit = true;
break;
//...........................................................................
default:
System.out.println("Invalid Entry!");
}
}while (!quit);
}
}
答案 0 :(得分:1)
替换
while(cur.getName() != Key)
与
while(!cur.getName().equals(Key))
您正在将对象平等与!=
进行比较。你想要比较字符串的字符(两个不同的字符串,相同的字符应该相等)。
答案 1 :(得分:0)
你确定它们应该是Static
??
private static String name;
private static String address;
private static int phoneNo;
我认为应该是
private String name;
//... etc.
修改:我认为
telList t1 = new telList();
main()
上的应该在do
循环
编辑2:
main()
t1.insert(t.getName(),t.getAddress(),t.getPhoneNo());
case 1:
上的{p>你真的没有使用你在主要创建的对象
所以就这样做:
case 1:
System.out.print("Enter Name: ");
String name = input.next();
//t.setName(name);
System.out.print("Enter Address: ");
String address = input.next();
//t.setAddress(address);
System.out.print("Enter Phone No: ");
int no = input.nextInt();
//t.setPhoneNo(no);
t1.insert(name,address,no);
从我看到你在t1
但请确保您放置telList t1 = new telList();
循环的do
OUTSIDE ,因为您每次都会重新实例化。