gpt4 book ai didi

java - 数组仅打印最后存储的值,数组元素删除仅替换元素

转载 作者:行者123 更新时间:2023-12-02 04:33:05 25 4
gpt4 key购买 nike

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

此外,我怎样才能使只有当该人拥有 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));
}

最佳答案

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

正如上面正确提到的,数组的大小不会改变 - 两个数组的大小始终为 1。如果您输入多个学生,然后在管理中输入最后一个学生的姓名,这也会导致索引越界异常。

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

为了仅存储信用 >=30 且 <=90 的人,您可以使用简单的 if。

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

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

由于 if 后面的分号什么都不做。大括号中的部分(我在其中添加了“做某事”注释)将始终被执行,它与 if 分开。 (Java 允许您将代码块放在花括号中以限制局部变量的范围)。

附注这是一个稍微好一点的版本(至少它有效):

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));
}

关于java - 数组仅打印最后存储的值,数组元素删除仅替换元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31196401/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com