我如何更正我的阵列?

时间:2017-05-19 21:06:33

标签: java

我已经制作了两个类,这些将在下面显示(主类中的一个是不正确的,不会运行)。我想打印数组,我也想知道如何允许用户输入一个数字,以便从给定的数组位置显示详细信息。另一件事是我应该在我的代码中使用什么格式的邮政编码。谢谢你的帮助。

if(input.matches("S")){

    KeyStroke[] patientsDetails = new KeyStroke[5];
    patientsDetails[0] = new KeyStroke(Alex,8,OX3YUN,2039489);
    patientsDetails[1] = new KeyStroke(John,19,OX4BJO,2434587);
    patientsDetails[2] = new KeyStroke(Nick,42,OX1VYN,2233842);
    patientsDetails[3] = new KeyStroke(Melvin,52,OX2YUD,2432340);
    patientsDetails[3] = new KeyStroke(Gian,27,OX43YUM,2234098);

    for (int i = 0; i < 10; i++){
         System.out.println("Patient name is: " + patientsDetails[i].nameLable);
         System.out.println("Patients age is: " + patientsDetails[i].howOld);
         System.out.println("Patient postcode is: " + patientsDetails[i].postcode);
         System.out.println("Patients cell phone number is" + patientsDetails[i].cellPhoneNumber);
    }
}

第二个儿童班是:

public class KeyStroke{
   public int howOld;
   public String postcode;
   public String nameLable;
   public int cellPhoneNumber;

   public KeyStroke(int age, String postcode, String nameLable, int cellPhoneNumber){
       this.nameLable = nameLable;
       this.howOld = age;
       this.postcode = postcode;
       this.cellPhoneNumber = cellPhoneNumber;   
   }
}

1 个答案:

答案 0 :(得分:1)

使用&#34;在字符串值之前和之后,请确保您的数组索引正确并且您没有在同一索引上插入数据。此外,在传递参数构造函数时,请确保顺序与构造函数中提到的相同。

最后,在运行循环时,请确保循环数组索引与循环运行相对应。这是代码:

KeyStroke[] patientsDetails = new KeyStroke[5];
patientsDetails[0] = new KeyStroke(8,"OX3YUN","Alex",2039489);
patientsDetails[1] = new KeyStroke(19,"OX4BJO","John",2434587);
patientsDetails[2] = new KeyStroke(42,"OX1VYN","Nick",2233842);
patientsDetails[3] = new KeyStroke(52,"OX2YUD","Melvin",2432340);
patientsDetails[4] = new KeyStroke(27,"OX43YUM","Gian",2234098);

for (int i = 0; i < 5; i++){
    System.out.println("Patient name is: " + patientsDetails[i].nameLable);
    System.out.println("Patients age is: " + patientsDetails[i].howOld);
    System.out.println("Patient postcode is: " + patientsDetails[i].postcode);
    System.out.println("Patients cell phone number is: " + patientsDetails[i].cellPhoneNumber);
}

它将为您提供所需的解决方案。