Java 数组仅打印最后一个存储的值,数组元素删除仅替换元素
有人知道为什么当我打印最后一条消息(“报告标题..等等”)时,列表上的计数会更新,但我只打印最后一个人的输入值 另外,我怎样才能使只有当这个人有30个或更多的信用或少于90个信用时,他们的名字和信用才会被存储在数组中,否则对输入什么都不做 最后,在“admin review”提示部分,如果我键入与输入匹配的名称,它应该删除该名称,但在我当前的代码中,它仅用我输入的名称替换该名称Java 数组仅打印最后一个存储的值,数组元素删除仅替换元素,java,arrays,Java,Arrays,有人知道为什么当我打印最后一条消息(“报告标题..等等”)时,列表上的计数会更新,但我只打印最后一个人的输入值 另外,我怎样才能使只有当这个人有30个或更多的信用或少于90个信用时,他们的名字和信用才会被存储在数组中,否则对输入什么都不做 最后,在“admin review”提示部分,如果我键入与输入匹配的名称,它应该删除该名称,但在我当前的代码中,它仅用我输入的名称替换该名称 final int MAX_ON_LIST = 50; String[] stuName = new Str
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));
}
final int MAX_ON_LIST=50;
String[]stuName=新字符串[1];
int[]numCredits=新int[1];
int currentSize=0;
String question=JOptionPane.showInputDialog(“您输入学生了吗?(输入'Y'或'N')”;
while(问题等信号情况(“n”)){
for(int i=0;i
每个数组都有一个元素。每次添加名称时,都会将其放在数组中的相同位置
String[] stuName = new String[1];
int[] numCredits = new int[1];
此循环始终只有一个过程,i=0
for (int i = 0; i < stuName.length; i++) {
for(int i=0;i
备选方案包括:
- 创建学生的
,每次调用List.add(),学生人数会根据需要增加java.util.List
- 创建学生从名字到值的
java.util.Map
为了只存储积分>=30的人,我如何设置数组的大小,使其仅与用户得到提示的次数相同?但是,如果不询问用户希望在收到提示之前得到提示的次数,您可以进一步解释这一点吗?因此,我应该将stuName设置为while和for循环之间的某个值?如上面的said;如果单个过程仍然存在问题,则do-while循环可能比for循环更好。如果用户只想输入5个名称,它将显示这5个名称?或者,无论用户想输入多少名称,但最大值为50个名称。确定最大值的一种方法是在最顶端的out中添加一个附加条件ErMyLoor——在而不是<>代码> 时,您可能想考虑生成<代码>。您可以通过循环循环来显示所有输入的名称。当前,您的代码搜索特定的输入名称。
if (nxtQuestion.equalsIgnoreCase("y"));
{
// Do something
}
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));
}