我有2个不同的Java文件 Mark.java
public class Mark
//class'name has to be the same with file's name
{
private String studentName;
private int studentMark;
//studentName and studentMark are private instance variables
//which cannot be accessed by other classes
public Mark() //a default constructor
{studentName = "unknown"; studentMark = 0;} //giving a starting value
public Mark(String n, int m) //a parameterised constructor
{studentName = n; studentMark = m;}
//2 set mthods
public void setname (String n)
{studentName = n;}
public void setmark (int m)
{studentMark = m;}
//2 get methods
public String getname()
{return studentName;}
public int getmark()
{return studentMark;}
//get Grade method
public String Grade;
//"final" means constant
final int PASS = 50;
final int CREDIT = 65;
final int DISTINCTION = 75;
final int highDISTINCTION = 85;
{
//if statement
if (studentMark < PASS)
Grade = "F";
else if (studentMark < CREDIT)
Grade="P";
else if (studentMark < DISTINCTION)
Grade="C";
else if (studentMark < highDISTINCTION)
Grade="D";
else
Grade="HD";
}
public void setGrade (String g) {Grade = g;}
public String getGrade() {return Grade;}
}
和GUI.java
import javax.swing.*; //to run JFrame
import java.awt.*; //to run FlowLayout
import java.awt.event.*; //to run ActionListener
import javax.util.*;
public class MarksGUI extends JFrame implements ActionListener
{
//declare class instance variables
String studentName = nameField.getText();
int studentMark = Integer.parseInt(markField.getText());
final int MAX_STUDENT = 10;
//declare maximum student constant
private Mark [] markArray = new Mark[MAX_STUDENT];
//declare current student variable
private int currentStudent = 0; //to count the numbers of students from 0 to 9
markArray[currentStudent] = new Mark (studentName, studentMark);
private void enterStudentNameAndMark()
{
}
目前我正试图找到一种方法,以便通过mark.java处理名称和标记输入数据并存储在数组中,这将是最有效的方法&gt;
答案 0 :(得分:0)
如果我的问题正确,您可以在调用参数化构造函数后立即调用该方法。
当您调用数组中的构造函数时,将生成Grade。
或者你可以在actionPerformed方法中调用构造函数。
答案 1 :(得分:0)
首先,移动你的&#34; if语句&#34;进入构造函数或另一个private
方法并将其添加到构造函数中。
注意: 不要public
,因为您将在构造函数中将其用作成绩初始化方法
private void updateGrade(int studentMark){
if (studentMark < PASS)
Grade = "F";
else if (studentMark < CREDIT)
Grade="P";
else if (studentMark < DISTINCTION)
Grade="C";
else if (studentMark < highDISTINCTION)
Grade="D";
else
Grade="HD";
}
不要忘记在二传手中更新成绩。
public void setmark (int m){
studentMark = m;
updateGrade(m);
}