Java队列只打印for循环中的最后一个元素。

时间:2017-04-26 07:49:41

标签: java arrays for-loop methods queue

我正在创建一个队列Java程序来代表医生阵列中的患者。它允许我在数据结构中添加节点,但是当我尝试将其打印出来时,它会让我感到震惊。这是方法

  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;

     }

最后,这是一个很棒的课程

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
  }

有任何帮助吗?尝试了几种策略来解决它,似乎没有任何工作。有很多代码,所以如果你需要完整的代码,请告诉我!

1 个答案:

答案 0 :(得分:0)

您的代码存在的问题是您使用的是static个变量。这意味着他们只能拥有一个值

所以改变

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

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

修改并清理代码