[英]assigning values to the variables
您好,在我的项目中,我完成了所有工作,几乎完成了所有工作,但遇到了一个小问题,无法修复,因此我们将不胜感激。 它在我的学生班和大学班。 在学生类中,由于某种原因,在 readInfo() 方法中读取的值没有分配给 printInfo() 方法中的变量。 在大学课程中的 printStudentDetails() 方法中,当我调用 printInfo 方法时,除了标题之外没有任何打印内容。 提前致谢!
import java.util.Scanner;
public abstract class Person {
public String firstName ;
public String lastName;
public String emailID;
public long phoneNumber;
Person(){}
public void readInfo() {
Scanner sc;
}
public void printInfo() {
System.out.println(firstName + lastName + emailID + phoneNumber);
}
}
public interface Policies {
double MAX_MARKS = 100;
double MAX_GPA = 4.0;
public void calculateGPA(double[] marks) ;
}
import java.util.Scanner;
public class Student extends Person implements Policies {
public int studentNumber;
public String programName;
public double gpa;
Student(){}
@Override
public void readInfo() {
Scanner sc = new Scanner(System.in);
System.out.print("Enter program name: ");
programName = sc.next();
System.out.print("Enter student number: ");
studentNumber = sc.nextInt();
System.out.print("Enter first name: ");
firstName = sc.next();
System.out.print("Enter last name: ");
lastName = sc.next();
System.out.print("Enter email ID: ");
emailID = sc.next();
System.out.print("Enter phone number: ");
phoneNumber = sc.nextLong();
readMarks();
}
private void readMarks() {
Scanner sc = new Scanner(System.in);
System.out.print("Enter number of courses: ");
int numCourses = sc.nextInt();
double marks[] = new double[numCourses];
for(int i = 0; i < numCourses; i++) {
System.out.print("Enter mark " + (i+1) + ":");
marks[i] = sc.nextDouble();
}
calculateGPA(marks);
}
@Override
public void calculateGPA(double marks[]) {
double totalMarks =0;
for(int i=0; i < marks.length; i++)
totalMarks = totalMarks +marks[i];
gpa = (totalMarks/marks.length *MAX_MARKS)*MAX_GPA;
}
@Override
public void printInfo() {
super.printInfo();
System.out.printf("%25d | %25s | %25s | %25s | %25s | %25.2f |\n", studentNumber
,programName , firstName, lastName, emailID, gpa);
}
}
import java.util.Scanner;
public class FulltimeStudent extends Student {
double tuitionFees;
FulltimeStudent(){}
@Override
public void readInfo() {
Scanner sc = new Scanner (System.in);
super.readInfo();
System.out.println("Enter tuition fees");
tuitionFees = sc.nextDouble();
}
@Override
public void printInfo() {
super.printInfo();
System.out.printf("%25d |\n",tuitionFees);
}
}
import java.util.Scanner;
public class ParttimeStudent extends Student {
double courseFeesTotal;
double credits;
ParttimeStudent(){}
@Override
public void readInfo() {
Scanner sc = new Scanner (System.in);
super.readInfo();
System.out.println("Enter total course fees:");
courseFeesTotal = sc.nextDouble();
System.out.println("Enter credit hours:");
credits = sc.nextDouble();
}
@Override
public void printInfo() {
super.printInfo();
System.out.printf("%25d | %25d\n",courseFeesTotal, credits);
}
}
import java.util.Scanner;
public class College {
String name;
Student [] students;
int numStudents;
College(){}
College(String name, int num){
this.name = name;
students = new Student[num];
}
public void printTitle() {
System.out.println(name + " - List of Students");
for(int i = 0; i< 40; i++ )
System.out.print("*");
System.out.printf("%n%15s | %15s | %15s | %15s | %15s | %15s | %15s | %15s |\n",
"Program" , "Student#" , "Name" , "Email" , "Phone" , "GPA", "Fees", "Credits");
}
public void ReadStudentDetails() {
Scanner sc = new Scanner (System.in);
System.out.println("1 - Fulltime Student \n2 - Parttime Student");
System.out.print("Enter student type:");
int type = sc.nextInt();
if (type == 1) {
FulltimeStudent fts = new FulltimeStudent();
fts.readInfo();
}
else if (type == 2) {
ParttimeStudent pts = new ParttimeStudent();
pts.readInfo();
}
else if (type <1 || type >2) {
System.out.println("*** Wrong student type... PLease re-enter ***");
}
while (type <1 || type >2) {
System.out.println("1 - Fulltime Student \n2 - Parttime Student");
System.out.print("Enter student type:");
type = sc.nextInt();
if (type == 1) {
FulltimeStudent fts = new FulltimeStudent();
fts.readInfo();
}
else if (type == 2) {
ParttimeStudent pts = new ParttimeStudent();
pts.readInfo();
}
else if (type <1 || type >2) {
System.out.println("*** Wrong student type... PLease re-enter ***");
}
}
numStudents++;
}
public void printStudentDetails() {
if (numStudents == 0) {
System.out.println("*** No students to print ***");
}
else {
printTitle();
for (int i = 0; i < numStudents; i++) {
if (students[i] != null)
students[i].printInfo();
}
}
}
}
import java.util.Scanner;
public class CollegeSystemTest {
public static void main(String[] args) {
Scanner sc = new Scanner (System.in);
System.out.println("Enter name of college:");
String name = sc.next();
System.out.println("Enter number of students:");
int numStudents = sc.nextInt();
College c = new College(name, numStudents);
int option;
do {
System.out.println("1. Read student \n2. Print details of all students \n3. Exit
\nEnter your option:");
option = sc.nextInt();
if (option == 1) {
c.ReadStudentDetails();
}
else if (option == 2) {
//c.printTitle();
c.printStudentDetails() ;
}
}while(option != 3); //a do-while loop is used to display menu items and will keep running
until option 3 is chosen
sc.close();
}
}
让我们来解决这个问题,从printStudentDetails()
你说没有输出任何东西(大概......除了标题)。
for (int i = 0; i < numStudents; i++) {
if (students[i] != null)
students[i].printInfo();
}
问:为什么什么都不打印?
答:两个可能的原因:
numStudents
为零。students[i]
是null
... 对于所有i
。那么让我们看看上面的代码:
Student [] students;
int numStudents;
College(){}
College(String name, int num) {
this.name = name;
students = new Student[num];
}
在英语中,创建College
时, student
被初始化为一个长度为num
的Student
对象数组。 该数组的元素都将为null
。 numStudents
值将为零。
(这是敲钟了吗?)
那么让我们看看读取学生信息的ReadStudentDetails
代码。
int type = sc.nextInt();
if (type == 1) {
FulltimeStudent fts = new FulltimeStudent();
fts.readInfo();
}
...
问: FulltimeStudent
对象会发生什么?
A:你把信息读进去,然后……什么也没有。 fts
变量超出范围,没有任何东西能够看到/使用您刚刚创建的对象。
Q:如何进入student
数组?
答:没有!!
问:为什么numStudents
仍然为零1 ? 为什么students
仍然都是nulls
?
A:因为你的代码没有改变它们中的任何一个......当它创建Student
对象时!! 或其他任何地方。
问:那应该怎么办?
答:应该:
numStudents
小于students.length
。fts
的引用分配给fts
students[numStudents]
。numStudents
增加 1。通读方法的其余部分,您还有其他 3 个地方在做同样的事情……或多或少。 在每种情况下都需要类似的更正。
1 - 事实上,我注意到在ReadStudentDetails
方法的末尾你正在增加numStudents
... ONCE。 所以变量将是一而不是零。 但是您没有将任何学生放入students
,因此不会有任何可打印的内容。
这里的主要教训是应用程序执行您(程序员)告诉它执行的操作(在代码中)。 而已。 一点也不少。 如果你不告诉它分配一些东西......它不会。
所以如果你想弄清楚你的应用程序为什么不工作,你需要仔细阅读你在代码中告诉它做什么。 仔细。 然后将您看到的内容(例如输出、异常...或无输出)与代码进行比较。 并对问题应用一些推理。
我看到了行为 X。什么情况会导致这种行为? 我可以从程序的状态中推断出什么? 我可以通过其他方式确认吗? (通过添加跟踪打印语句来打印变量。通过使用我的 IDE 的调试器。通过查看其他证据。)
然后向后工作……弄清楚您可以观察到或假设的情况或状态如何/为什么会发生……直到您发现代码中的缺陷(错误)为止。
我建议您花时间阅读以下内容:
我还会注意到您的代码中还有其他错误……但我不会指出它们。 此外,在您编写的代码中存在很多样式错误。 我不知道你的作业是否会被标记为编码风格,但如果是的话,你应该(IMO)失去大部分的风格标记。 确保在提交作业之前修复它们。 (我不会指出它们。检查你的讲义,等等。)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.