在我的代码中,我创建了一个名为Points的类,该类检查给定问题的答案并返回分数。另一个称为“报告”的类需要一些信息来进行打印
package finalproject;
public class Points {
public static float calculatePoints(byte[] userAnswer) {
byte realAnswer[], gradeCounter;
float score ;
gradeCounter = 0;
realAnswer = new byte[3];
realAnswer[0] = 3;
realAnswer[1] = realAnswer[2] = 1;
for (int i = 0; i < userAnswer.length; i++) {
if (userAnswer[i] == realAnswer[i]) {
gradeCounter++;
}
}
score = (gradeCounter / 3) * 100;
return score;
}
}
package finalproject;
public class Report {
public static void getLoginInfo(String[] loginInformation) {
loginInformation = new String[2];
String name, id;
name = loginInformation[0];
id = loginInformation[1];
}
public static void printReport() {
System.out.println("\n\n-------------------\n\n");
System.out.println("\t\tJava Certification");
System.out.println("\t\t Test Result\n\n");
System.out.println("\tName: ");
System.out.println("\tTest Registration ID: ");
System.out.println("\tPassing Score 52%");
System.out.println("\tYour Score: ");
/*
* if (score1 >= 52.0) { System.out.println("\n\nComment GRADE: pass\n\n"); }
* else { System.out.println("\n\nComment GRADE: fail\n\n"); }
*/
System.out.println("Max Score\t" + "---------------100%");
System.out.println("Max Score\t" + "--------52%");
}
}
在getLoginInfo变量中,应设置名为name和id的变量,我要将它们传递给printReport。
我想在函数calculatePoint()中将名为score的变量传递给printReport,我该怎么办?
答案 0 :(得分:0)
如果在任何函数之外声明name和id,则字符串name和id的范围将是该类的范围,这意味着它将对Report类中的所有函数可见。类报告的每个对象/实例都将具有名称和ID作为属性。然后,可以使用getLoginInfo进行设置,并使用printReport()来打印名称和ID。
public class Report {
String name, id;
public static void getLoginInfo(String[] loginInformation){
name = loginInformation[0];
id = loginInformation[1];
}
public static void printReport() {
...
}
}