Java队列仅打印出for循环中的最后一个元素。
我正在创建一个Java队列数组程序,以在医生数组中表示患者。它让我可以在数据结构中添加节点,但当我试图打印出来时,它会在我身上爆炸。这里是方法Java队列仅打印出for循环中的最后一个元素。,java,arrays,for-loop,methods,queue,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","Doct
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);
}
}
最后,这里是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
}
有什么帮助吗?尝试了几种策略来修复它,但似乎没有任何效果。有很多代码,所以如果你需要完整的代码,请让我知道 代码的一个问题是您使用的是
静态变量。这意味着它们只能有一个值
所以改变
protected static String name;
protected static String telephone;
protected static int ID;
到
编辑并清理你的代码它会让我崩溃
,似乎什么都没用
都不是很好的解释。创建一个。有两件事让这个问题令人恐惧:代码的格式不是最佳的,另外,您应该遵循java命名约定
,因为这会增加混乱的格式。您提供的代码也不会编译。第一个编译错误将出现在这里:{“Doctor 1”、“Doctor 2”、“Doctor 3”、“Doctor 4”、“Doctor 5”、“Doctor 6”};
欢迎使用Stack Overflow!请查看,并通读,尤其是和。-请添加
protected static String name;
protected static String telephone;
protected static int ID;
protected String name;
protected String telephone;
protected int ID;