Java While循环遍历数组列表在获取用户输入时过早退出

时间:2018-06-12 13:28:18

标签: java loops while-loop

我有一个从另一个类继承的Java程序。一切都很好,除了用while循环添加用户输入。如果用户键入" no"当被问及列表是否完成时,应该继续接收信息。不幸的是,循环过早退出(即完全=否,但它仍然退出)。任何帮助都非常感谢。

import java.io.Console;
import java.util.*;
public class Student extends Person {

int gradYear; //We add an additional instance variable 
boolean enrolled = false;

  public Student(){
  }
  void enrolledStudent(){
    enrolled = true;
  }
  static Student addStudents(String name, int age, int gradYear, boolean enrolled){

    Student new_student = new Student();
    new_student.name = name;
    new_student.age = age;
    new_student.gradYear = gradYear;
    new_student.enrolled = enrolled;
    return new_student;
    }
  public static void main(String[] args) {
    Console console =  System.console();
      ArrayList<Student> studentList = new ArrayList<Student>();
      String studentName;
      int studentAge;
      int studentGradYear;
      boolean enrollmentStatus;
      String complete = "no";
      while (complete == "no") {
        studentName = console.readLine("Enter a name: ");
        studentAge = Integer.parseInt(console.readLine("Enter Age: "));
        studentGradYear = Integer.parseInt(console.readLine("Year of Graduation: "));
        enrollmentStatus = Boolean.parseBoolean(console.readLine("Enrolled (enter true or false: "));
        studentList.add(addStudents(studentName, studentAge, studentGradYear, enrollmentStatus));
        complete = console.readLine("List complete? Enter yes or no: ");
      } 
   System.out.println(studentList.size());

    for (Student s : studentList){
      System.out.println("Name: " + s.name + "\nAge: " +  s.age +"\nGraduation Year: " + s.gradYear +  "\nEnrolled? " + s.enrolled);
    }
  }
}

0 个答案:

没有答案