使用程序逻辑时获取空输出

时间:2018-07-30 10:57:23

标签: java

在程序输出中使用字符串方法时,我得到的输出为null。

//主要方法     包studentDatabaseApp;

import java.util.Scanner;

public class StudentDatabaseApp {

public static void main(String[] args) {

    Scanner scan = new Scanner(System.in);
    System.out.print("Enter number of Students: ");

    int numOfStudents = scan.nextInt();

    Student[] students = new Student[numOfStudents];

    for(int n =0; n < numOfStudents; n++) {
        students[n] = new Student();
        students[n].enroll();
        students[n].payTution();

    }

    for(int n =0; n < numOfStudents; n++) {
        System.out.println(students[n].toString());
    }
}

}

//学生班级代码

package studentDatabaseApp;

import java.util.Scanner;

public class Student {

private String firstName;
private String lastName; 
private int gradeYear = 0;
private String studentID;
private String courses;
private static int courseCost = 600;
private int tutionBalance = 0;
private static int id = 1000;

//Constructor to enter student name and year for each student
public Student() {
    Scanner scan = new Scanner(System.in);
    System.out.print("Enter Student First Name: ");
    this.firstName = scan.nextLine();
    System.out.print("Enter Student Last Name: ");
    this.lastName = scan.nextLine();
    System.out.print("1 - Freshmen\n2 - Sophmore\n3 - Junior\n4 - Senior\nEnter Student Grade Year: ");
    this.gradeYear = scan.nextInt();
    //Setting student id
    setStudentID();

}
//Unique id and student grade level

private void setStudentID() {
    id++;
    this.studentID = gradeYear + "" + id;
}

//Create courses so student can enroll
public void enroll() {
    do {

        System.out.print("Enter course to enroll (Q to Quit): ");
        Scanner in = new Scanner(System.in);
        String course = in.nextLine();
        if(!course.equals("Q")) {
            courses = courses + ", " + course;
            tutionBalance = tutionBalance + courseCost;
            }
            else {
                break;
            }
    }while(1 != 0);

}

//Student should able to view their balance and pay the tution 
public void viewBalance() {
    System.out.println("BALANCE TOTAL: " + tutionBalance);
}

//Total balance
public void payTution() {
    viewBalance();
    Scanner scan = new Scanner(System.in);
    System.out.print("\nEnter your payment: ");  
    int tutionPaid = scan.nextInt();
    tutionBalance = tutionBalance - tutionPaid;
    viewBalance();
}
//Student status with their name, ID, course enrolled and balance
@Override
public String toString() {
    return "\nSTUDENT NAME: " + firstName + " " + lastName +
            "\nGRADE LEVEL: " + gradeYear + " " + "\nSTUDENT ID: " + studentID +
            "\nCOURSES ENROLLED: " + courses + 
            "\nTUTION BALANCE: " + tutionBalance;
}
}

//控制台输出

Enter number of Students: 1
Enter Student First Name: bilal
Enter Student Last Name: mujahid
1 - Freshmen
2 - Sophmore
3 - Junior
4 - Senior
Enter Student Grade Year: 4
Enter course to enroll (Q to Quit): Eng 101
Enter course to enroll (Q to Quit): Math 101
Enter course to enroll (Q to Quit): Q
BALANCE TOTAL: 1200

Enter your payment: 1000
BALANCE TOTAL: 200

STUDENT NAME: bilal mujahid
GRADE LEVEL: 4 
STUDENT ID: 41001
COURSES ENROLLED: null, Eng 101, Math 101
TUTION BALANCE: 200

2 个答案:

答案 0 :(得分:2)

问题出在

courses = courses + course;

您使用

private String courses = “”;

Courses为null,因为它从未初始化。尝试将其更改为

UITableView

(这将使它在开始时带有多余的逗号,您可以轻松地将其子串掉)

答案 1 :(得分:1)

第一次用course初始化

if(!course.equals("Q")) {
    courses = coursers == null ? course : courses + ", " + course;
    ...