我的代码中有JTextfield,我要求用户输入他们的ID号。输入识别号码后,该代码将查看输入的识别号码是否在grade.txt(我创建的文件)中。如果输入的ID号码在文本文件中,它将显示用户的等级。当我运行这个程序并输入不同的ID号时,它显示第一行 (ID号)的相同等级。 我需要你的帮助,我该如何编码。Java中的控制结构
@Marcx这里是完整的代码:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package student_grade;
import java.util.*;
import java.io.*;
import java.awt.Component;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
/**
*
* @author Christian
*/
public class Student_grade {
public static void main(String[] args) throws FileNotFoundException {
//declaration
String firstName, lastName, discard;
double subject1, subject2, subject3, subject4, subject5;
long userID = 0;
//get file from the address
Scanner inFile= new Scanner(new FileReader("D:\\grade.txt"));
HashMap<Double, student> map = new HashMap<Double, student>();
while (inFile.hasNext())
{
student s = new student();
s.id = inFile.nextDouble();
s.subject1 = inFile.nextDouble();
s.subject2 = inFile.nextDouble();
s.subject3 = inFile.nextDouble();
s.subject4 = inFile.nextDouble();
s.subject5 = inFile.nextDouble();
map.put(s.id, s);
}
student student = map.get(userID);
//JTextField
JTextField fullName = new JTextField();
JTextField section = new JTextField();
JTextField idnumber2 = new JTextField();
Object[] message =
{
"Name:", fullName,
"Section:", section,
"ID Number:", idnumber2
};
Component parent = null;
int option = JOptionPane.showConfirmDialog(parent, message, "Check your Grades!", JOptionPane.OK_CANCEL_OPTION);
//display information
if (option == JOptionPane.OK_OPTION)
{
String value1 = fullName.getText();
String value2 = section.getText();
String value3 = idnumber2.getText();
String outputStr= "Name: "+ fullName.getText() +
"\nSection: "+ section.getText()+
"\nID Number: "+ student.id +
"\nMath: "+ student.subject1 +
"\nEnglish: "+ student.subject2 +
"\nFilipino: "+ student.subject3 +
"\nReligion: "+ student.subject4 +
"\nComputer: "+ student.subject5;
JOptionPane.showMessageDialog(null, outputStr,"User Information",JOptionPane.INFORMATION_MESSAGE);
}
//closing main arguments
}
}
您似乎只是抓住文件中的第一组数字,当然它只会显示第一个学生的成绩。 – Carcigenicate
你能帮我解答我该怎么做?我刚开始学习Java,所以对我来说很难。 –
阅读文件的内容,通过搜索找到您要查找的名称,然后获取它后面的数字。 – Carcigenicate