简体   繁体   English

程序打印额外的 null 我似乎无法摆脱

[英]program printing extra null that I can't seem to get rid of

public class Student {
    
    private String courses = null;
    
    }
.....Some code here.......
    
    //Enroll in courses
    public void enroll() {
        //Get inside a loop and user hits Q
        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 + "\n " + course;
                tuitionBalance = tuitionBalance + costOfCourse;
            
            }
            else{ break; }      
        }while ( 1!= 0);

    }


......Some code here......
    
    //Show Status
    public String showInfo() {
        return "Name: " + firstName + " " + lastName + 
                "\nGrade level: " + gradeYear +
                "\nStudent ID: " + studentId +
                "\nCourses Enrolled:" + courses +
                "\nBalance: $" + tuitionBalance;
    }


}

Everything seems to be running just fine.一切似乎都运行得很好。 But there's an extra null printed after the Courses Enrolled that sticks out like a sore thumb.但是在注册课程后打印了一个额外的 null,它像拇指酸痛一样突出。 How could I get rid of it?我怎么能摆脱它? I've tried setting the String variable courses to null and also without assigning but doesn't seem to affect the result我尝试将字符串变量课程设置为 null 并且也没有分配但似乎不会影响结果

Name: Frank Kuo
Grade level: 1
Student ID: 11001
Courses Enrolled:null
 Math101
Balance: $0

You can replace private String courses = null;您可以替换private String courses = null; with private String courses = ""; private String courses = ""; to get rid of the null .摆脱null

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM