【发布时间】:2014-05-03 23:40:38
【问题描述】:
因此,用户在不同的字段中输入他/她的名字和姓氏。 (必须这样)
参考图片:
我要做的是在他们的 4 个测试分数旁边显示他们的名字。
所以它看起来像这样
约翰·史密斯 55.0 100.0 23.0 50。 简·史密斯 100.0 50.0 76.0 22.0 等等等等。
我的代码是
public class StudentGradesView extends FrameView {
final static int students=15;
final static int numOfTest=4;
final static int firstNLast=2;
//Start with the first student in the 2D array
private int row = 0;
//DELCARING THE 2D ARRAY
double[][] marks = new double[students][numOfTest];
String[][] names = new String[students][firstNLast];
//1D Arrays
double testScores[]=new double[4];
String studentNames[]=new String[2];
public void addInfo(){
studentNames[0]= firstNameIn.getText();
studentNames[1]= lastNameIn.getText();
testScores[0]=Double.parseDouble(testOneIn.getText());
testScores[1]=Double.parseDouble(testTwoIn.getText());
testScores[2]=Double.parseDouble(testThreeIn.getText());
testScores[3]=Double.parseDouble(testFourIn.getText());
//Add first and last name to 2d array
for(int col=0;col <studentNames.length; col++){
names[row][col]= studentNames[col];
}
//Add all four test scores to the current student
for(int col=0;col < testScores.length; col++){
marks[row][col]= testScores[col];
}
//Advance to the next student
row++;
}
public void displayArray(){
for(int i=0;i<names.length;i++){
for(int j=0; j<firstNLast;j++){
finalOutput.append(names[i][j]+" ");
}
finalOutput.append("\n");
}
for(int i=0; i<marks.length;i++){
for(int j=0; j <numOfTest; j++){
finalOutput.append(marks[i][j]+" ");
}
finalOutput.append("\n");
}
}
}
当我点击 list/displayArray(); 时我最终得到了什么这是:
所以我被困在如何让它们并排打印。还有一种方法可以只打印用户输入的条目数吗?如果一个用户输入了一组信息,只打印一行?
谢谢。
【问题讨论】:
-
请修正您的代码格式,因为它很难阅读,难以理解和帮助。
-
好的。我对此很抱歉。 :)
-
只需将所有制表符替换为空格即可。一般来说,最好不要在代码中使用制表符。
-
所有这些文本修改都是红鲱鱼;使用 JTable 并完成它!
标签: java arrays swing user-interface