作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试通过让用户输入标识号来访问并行数组的完整内容。该数组似乎只返回前四项 [0-3] 的结果。其余的则作为未找到而返回。使用 Eclipse,我尝试将数组的尺寸完全调整为 10 个内存位置,但是,我收到错误。
import java.util.*;
import javax.swing.JOptionPane;
public class StudentIDArray {
static String[] studentNum = new String[]
{"1234", "2345", "3456", "4567", "5678", "6789", "7890", "8901", "9012", "0123"};
static String[] studentName = new String[]
{"Peter", "Brian", "Stewie", "Lois", "Chris", "Meg", "Glen", "Joe", "Cleveland", "Morty"};
static double[] studentGpa = new double[]
{2.0, 3.25, 4.0, 3.6, 2.26, 3.20, 3.45, 3.8, 3.0, 3.33};
public static void main(String[] args) {
String studentId = null;
while ((studentId = JOptionPane.showInputDialog(null, "Please enter your Student ID number to view your name and GPA")) != null) {
boolean correct = false;
for (int x = 0; x < studentId.length(); ++x) {
if (studentId.equals(studentNum[x])) {
JOptionPane.showMessageDialog(null, "Your name is: " + studentName[x] + "\n" + "Your GPA: " + studentGpa[x], "GPA Results", JOptionPane.INFORMATION_MESSAGE);
correct = true;
break;
}
}
if (!correct) {
JOptionPane.showMessageDialog(null, "Student ID not found, try again.", "Not found", JOptionPane.INFORMATION_MESSAGE);
}
}
}
}
最佳答案
在for循环中改变:
studentId.length();
至
studentNum.length;
您现在使用输入字符串的长度,同时需要数组的长度。
关于java - 如何在java中访问并行数组的全部内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28932618/
我是一名优秀的程序员,十分优秀!