Array仅打印最后存储的值,并且数组元素删除仅替换元素

时间:2015-07-02 23:47:41

标签: java arrays

有人知道为什么当我打印最后一条消息(“report header..etc etc”)时,列表上的计数会更新,但我只打印最后一个人的输入值?

另外,我怎样才能这样做,只有当这个人有30个或更多学分或者少于90个学分时,他们的名字和学分才会被存储在数组中,否则对输入什么都不做?

最后,在“管理员审核”提示部分中,如果我输入与输入匹配的名称,它应该删除该名称,但在我当前的代码中,它只会将名称替换为我输入的名称..

  final int MAX_ON_LIST = 50;

  String[] stuName = new String[1];
  int[] numCredits = new int[1];

  int currentSize = 0;

  String question = JOptionPane.showInputDialog("Are you done entering students? (Enter 'Y' or 'N')");

  while (question.equalsIgnoreCase("n")) {

     for (int i = 0; i < stuName.length; i++) {
        do {
           try {
              stuName[i] = JOptionPane.showInputDialog("Enter student name:");        
              currentSize++;
           }
           catch (NumberFormatException e) {
              stuName[i] = "";
           }
           if (stuName[i].equals("")) {
              JOptionPane.showMessageDialog(null, "Name cannot be blank");
           }
        } while (stuName[i].equals(""));
     }


     for (int i = 0; i < numCredits.length; i++) {

        do {
           try {
              numCredits[i] = Integer.parseInt(JOptionPane.showInputDialog("Enter # of completed credits:"));
           }
           catch (NumberFormatException e) {
              numCredits[i] = -1;
           }
           if (numCredits[i] < 0) {
              JOptionPane.showMessageDialog(null, "# of credits can't be less than 0");
           }
        } while (numCredits[i] < 0);
     }

     JOptionPane.showMessageDialog(null, Arrays.toString(stuName) + "\n" + Arrays.toString(numCredits));

     question = JOptionPane.showInputDialog("Are you done entering students? (Enter 'Y' or 'N')");

  }




  String nxtQuestion = JOptionPane.showInputDialog("Are you done with the admin. review? (Enter 'Y' or 'N')");



  while (nxtQuestion.equalsIgnoreCase("n")) {

     String searchValue = JOptionPane.showInputDialog("Enter a name:");;
     int position = 0;
     boolean found = false;

     while (position < stuName.length && !found) {
        if (stuName[position].equalsIgnoreCase(searchValue)) {
           found = true;
        }
        else {
           ++position;
        }
     }
     if (found) {
        stuName[1] = stuName[currentSize - 1];
        --currentSize;

        JOptionPane.showMessageDialog(null, Arrays.toString(stuName) + "\n" + Arrays.toString(numCredits));
     }
     else {
        JOptionPane.showMessageDialog(null, "Name not on list");
        JOptionPane.showMessageDialog(null, Arrays.toString(stuName) + "\n" + Arrays.toString(numCredits));

     }

     nxtQuestion = JOptionPane.showInputDialog("Are you done with the admin. review? (Enter 'Y' or 'N')");
  }



  if (nxtQuestion.equalsIgnoreCase("y")); 
  {

     JOptionPane.showMessageDialog(null, 
           "Report Header\n\n" + "# of student's on list: " + currentSize + "\nNames: " + Arrays.toString(stuName) +
           "\nCredits: " + Arrays.toString(numCredits));
  }

3 个答案:

答案 0 :(得分:1)

每个数组都有一个元素。每次添加名称时,都会将其放入数组中的相同位置。

String[] stuName = new String[1];
int[] numCredits = new int[1];

此循环始终只有一次传递,i = 0。

for (int i = 0; i < stuName.length; i++) {

替代方案包括:

  • 创建java.util.List名学生,每次调用List.add()时都会根据需要增长。
  • 从名称到值创建java.util.Map名学生。

答案 1 :(得分:1)

我不确定你为什么要经历使用数组的所有痛苦,而List(ArrayList或LinkedList)更适合你的需求。我假设这是某种必须使用数组的任务。否则,应该重写整个代码。

如上所述,数组不会改变大小 - 两个数组的大小始终都是1。如果您输入多个学生,那么这也会导致索引越界异常,然后在管理员中输入最后一个学生的姓名。

最后一个学生的名字是唯一保存的名字,这就是为什么它是唯一打印的名字。

为了只存储信用卡&gt; = 30和&lt; = 90的人,您可以使用简单的if。

另请注意程序最后部分的代码:

if (nxtQuestion.equalsIgnoreCase("y"));
{
// Do something
}

如果if无效则由于分号。 花括号中的部分(我在其中放置“做某事”注释)将始终执行,它与if分开。 (Java允许您将代码块放在花括号中以限制局部变量的范围。)

P.S。这是一个稍好的版本(至少它可以工作):

public static void main(String[] args) {
    final int MAX_ON_LIST = 50;
    final int bottomCreditsLimit = 30;
    final int topCreditsLimit = 90;

    String[] stuName = new String[0];
    int[] numCredits = new int[0];

    String question = JOptionPane.showInputDialog("Are you done entering students? (Enter 'Y' or 'N')");

    while (question.equalsIgnoreCase("n") && stuName.length < MAX_ON_LIST) {
        String stuNameInput = "";
        do {
            stuNameInput = JOptionPane.showInputDialog("Enter student name:").trim();
            if (stuNameInput.equals("")) {
                JOptionPane.showMessageDialog(null, "Name cannot be blank");
            }
        } while (stuNameInput.equals(""));

        int numCreditsInput = -1;
        do {
            try {
                numCreditsInput = Integer.parseInt(JOptionPane.showInputDialog("Enter # of completed credits:").trim());
                if (numCreditsInput < 0) {
                    JOptionPane.showMessageDialog(null, "# of credits can't be less than 0");
                }
            } catch (NumberFormatException e) {
                JOptionPane.showMessageDialog(null, "Please input integer value");
            }
        } while (numCreditsInput < 0);

        if (numCreditsInput >= bottomCreditsLimit && numCreditsInput <= topCreditsLimit) {
            stuName = Arrays.copyOf(stuName, stuName.length + 1);
            stuName[stuName.length - 1] = stuNameInput;
            numCredits = Arrays.copyOf(numCredits, numCredits.length + 1);
            numCredits[numCredits.length - 1] = numCreditsInput;
            JOptionPane.showMessageDialog(null, Arrays.toString(stuName) + "\n" + Arrays.toString(numCredits));
        }

        question = JOptionPane.showInputDialog("Are you done entering students? (Enter 'Y' or 'N')");
    }

    String nxtQuestion = JOptionPane.showInputDialog("Are you done with the admin. review? (Enter 'Y' or 'N')");

    while (nxtQuestion.equalsIgnoreCase("n")) {
        String searchValue = JOptionPane.showInputDialog("Enter a name:").trim();
        int position = -1;

        for (int i = 0; i < stuName.length; i++) {
            if (stuName[i].equalsIgnoreCase(searchValue)) {
                position = i;
                break;
            }
        }
        if (position >= 0) {
            stuName[position] = stuName[stuName.length - 1];
            stuName = Arrays.copyOf(stuName, stuName.length - 1);
            numCredits[position] = numCredits[numCredits.length - 1];
            numCredits = Arrays.copyOf(numCredits, numCredits.length - 1);
        } else {
            JOptionPane.showMessageDialog(null, "Name not on list");
        }
        JOptionPane.showMessageDialog(null, Arrays.toString(stuName) + "\n" + Arrays.toString(numCredits));

        nxtQuestion = JOptionPane.showInputDialog("Are you done with the admin. review? (Enter 'Y' or 'N')");
    }

    JOptionPane.showMessageDialog(null, "Report Header\n\n" + "# of student's on list: " + stuName.length + "\nNames: " + Arrays.toString(stuName)
            + "\nCredits: " + Arrays.toString(numCredits));
}

答案 2 :(得分:0)

for循环预编译,所以你需要在使用循环之前将stuName设置为某个东西。