我的代碼中有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