2017-04-26 120 views
-4

我正在創建一個隊列程序的Java數組來代表醫生陣列中的病人。它可以讓我在數據結構中添加節點,但是當我嘗試打印出來時,它會炸燬我。這裏是方法Java隊列僅打印for循環中的最後一個元素。

static boolean [] openflag = new boolean[6]; 
static queue [] Clinic = new queue[6]; 
static String [] Doctor = {"Doctor 1", Doctor 2", "Doctor 3","Doctor 
4","Doctor 5","Doctor 6"}; 
final static String HEADING = "The clinic moniter of Dylan Rychlik"; 
static int MAX = 6; 
    public static void Listpaitents() 
    { 
    int queuechoice; 
JOptionPane.showMessageDialog(null, "Which doctor would you like to print?"); 
String InputString = JOptionPane.showInputDialog(null,Doctor, HEADING, 
JOptionPane.QUESTION_MESSAGE); 

queuechoice = Integer.parseInt(InputString); 
if (openflag[queuechoice -1 ] == false){ 
    JOptionPane.showMessageDialog(null, "Sorry, that doctor is notaviable"); 
    } 
else{ 
    Paitent[] array = Clinic[queuechoice -1].toArray(); 
    //int size = Clinic[queuechoice -1].getSize(); 
    int limit = Clinic[queuechoice -1].getSize(); 
    //System.out.println(limit); 
    int x; String out = " Members of the list are: \n"; 
    // boolean exit = false; 
    for(x = 1; x <= limit; x++) { 
    out += array[x-1].Print() + "\n"; 
    // System.out.println(array[x-1] + "\n"); 
    } 
    JOptionPane.showMessageDialog(null,out); 
    } 
} 

這裏是隊列類中的toarray()方法。

public static Paitent[] toArray() 
{ 
    int x = Length; 
    Paitent[] Array = new Paitent[Length]; 
    queuenode Current = rear; 
    for (x = Length; ((Current != null) && (x >= 1));x--) 
    { 
     Array[x-1] = new Paitent(); 
     Array[x-1].update(Current.info); 
     Current = Current.next; 

    } 
    return Array; 

    } 

最後,這裏是paitent類

public class Paitent { 
protected static String name; 
protected static String telephone; 
protected static int ID; 
//Creates a constructor for a paitent object 
public void paitent() 
{ 

name = ""; 
telephone = " "; 
ID = 0; 

    } 
//updates the country object 
public void update(Paitent thisThing) 
{ 

name = thisThing.name; 
telephone = thisThing.telephone; 
ID = thisThing.ID; 
} 
//asks for user input for country objects 
public void input(int i) 
    { 
String PatronHeading = "Country Data Entry"; 
String entername; 

int enterID; 
String enterphone; 


entername = JOptionPane.showInputDialog(null, "Please Enter the name of 
paitent #" + i +": ", PatronHeading, JOptionPane.QUESTION_MESSAGE); 

enterphone = JOptionPane.showInputDialog(null, "Please Enter the telephone 
number for paitent #" + i +": ", PatronHeading, 
JOptionPane.QUESTION_MESSAGE); 
String PNumberString = JOptionPane.showInputDialog(null, "Please Enter the 
ID for paitent #" + i +": ", PatronHeading, JOptionPane.QUESTION_MESSAGE); 
enterID = Integer.parseInt(PNumberString); 



name = entername; 

telephone = enterphone; 
ID = enterID; 
} 




//prints the results 
public String Print() 
{ 
String outputString; 
outputString = "Paitent: " + "-" + name + "\n" + " Telephone number " + 
telephone + " ID " + ID; 
return outputString; 
} 
//gets and sets the PCI in order to sort them 
    } 

任何幫助嗎?嘗試了幾種策略來解決它,似乎沒有任何工作。有很多代碼,所以如果你需要完整的代碼,請讓我知道!

+2

'它me'和'炸燬似乎沒有任何工作可以完全解決問題。創建一個[mcve]。 – Idos

+3

有兩個主要的問題使得這個問題變得很恐怖:你的代碼的格式不夠理想,另外你應該遵循'java命名約定',因爲這增加了混亂的格式。 – SomeJavaGuy

+1

Aslo你提供的代碼不能編譯。第一個編譯錯誤將在這裏:'{「Doctor 1」,Doctor 2「,」Doctor 3「,」Doctor 4「,」Doctor 5「,」Doctor 6「};' – Jens

回答

0

您的代碼存在的問題是您正在使用static變量。這意味着他們永遠只能有一個值

所以更改

protected static String name; 
protected static String telephone; 
protected static int ID; 

protected String name; 
protected String telephone; 
protected int ID; 

編輯和清理你的代碼

相關問題