我正在尝试创建一个控制台程序,允许您查看所有学生及其信息的列表,并使用指定的条目搜索列表。 我目前正在处理您可以输入学生ID的部分,并且将显示匹配结果。 问题是每当我使用ID选项作为输入时,do-while循环两次而不是一次。这很烦人,因为它会打印“CONSOLE>”消息两次。
我的示例输入是“20180001”
控制台上更具体的输入/输出:
Loop no: 0
CONSOLE>find
FIND >id
FIND ID >20180001
| ID: 20180001 | Name: Smith, John | Course: IT | Year: 1st Year | Age: 18 |
Loop no: 1
CONSOLE>Loop no: 2
CONSOLE>
我的预期输出:
Loop no: 0
CONSOLE>find
FIND >id
FIND ID >20180001
| ID: 20180001 | Name: Smith, John | Course: IT | Year: 1st Year | Age: 18 |
Loop no: 1
CONSOLE>
此处发生错误:
do {
System.out.println("Loop no: " + temp);
System.out.print("CONSOLE>");
input = Scanner.nextLine();
主要课程
public class Main {
public static void main(String[] args) throws InterruptedException {
String input = "";
int input2 = 0;
Student s0 = new Student();
Student s1 = new Student();
Student s2 = new Student();
Student s3 = new Student();
Student s4 = new Student();
Scanner Scanner = new Scanner(System.in);
int temp = 0;
fillStudents(s0, s1, s2, s3, s4);
do {
System.out.println("Loop no: " + temp);
System.out.print("CONSOLE>");
input = Scanner.nextLine();
if (input.toUpperCase().equals("LIST STUDENTS")) {
Student201.listStudents(s0, s1, s2, s3, s4);
} /* else System.out.println("ERROR MESSAGE: '" + input + "' is not recognized "
+ "as a command."); */
if (input.toUpperCase().equals("FIND")) {
System.out.print("FIND >");
input = Scanner.nextLine();
if (input.toUpperCase().equals("ID")) {
System.out.print("FIND ID >");
input2 = Scanner.nextInt();
if(s0.getStudentID() == input2) {
System.out.println(s0);
} /* else System.out.println("CONSOLE MESSAGE: No results were found with the first name '"
+ input + "'."); */
if(s1.getStudentID() == input2) {
System.out.println(s1);
}
if(s2.getStudentID() == input2) {
System.out.println(s2);
}
if(s3.getStudentID() == input2) {
System.out.println(s3);
}
if(s4.getStudentID() == input2) {
System.out.println(s4);
}
}
if (input.toUpperCase().equals("FIRST NAME")) { // If the user chooses FIRST NAME to find students
System.out.print("FIND FIRST NAME >");
input = Scanner.nextLine();
if(s0.getFirstName().toUpperCase().equals(input.toUpperCase())) {
System.out.println(s0);
} /* else System.out.println("CONSOLE MESSAGE: No results were found with the first name '"
+ input + "'."); */
if(s1.getFirstName().toUpperCase().equals(input.toUpperCase())) {
System.out.println(s1);
}
if(s2.getFirstName().toUpperCase().equals(input.toUpperCase())) {
System.out.println(s2);
}
if(s3.getFirstName().toUpperCase().equals(input.toUpperCase())) {
System.out.println(s3);
}
if(s4.getFirstName().toUpperCase().equals(input.toUpperCase())) {
System.out.println(s4);
}
}
if (input.toUpperCase().equals("LAST NAME")) { // If the user chooses LAST NAME to find students
System.out.print("FIND LAST NAME >");
input = Scanner.nextLine();
if(s0.getLastName().toUpperCase().equals(input.toUpperCase())) {
System.out.println(s0);
} /* else System.out.println("CONSOLE MESSAGE: No results were found with the last name '"
+ input + "'."); */
if(s1.getLastName().toUpperCase().equals(input.toUpperCase())) {
System.out.println(s1);
}
if(s2.getLastName().toUpperCase().equals(input.toUpperCase())) {
System.out.println(s2);
}
if(s3.getLastName().toUpperCase().equals(input.toUpperCase())) {
System.out.println(s3);
}
if(s4.getLastName().toUpperCase().equals(input.toUpperCase())) {
System.out.println(s4);
}
}
}
temp++;
}while (!input.equals("exit"));
}
public static void fillStudents(Student s0, Student s1, Student s2, Student s3, Student s4) throws InterruptedException {
clear();
System.out.println("Filling the information of Students...");
TimeUnit.SECONDS.sleep(1);
clear();
s0.setLastName("Smith");
s0.setFirstName("John");
s0.setAge(18);
s0.setCourse("IT");
s0.setYear("1st Year");
s0.setStudentID(20180001);
s1.setLastName("Wallace");
s1.setFirstName("Mark");
s1.setAge(18);
s1.setCourse("IT");
s1.setYear("1st Year");
s1.setStudentID(20180002);
s2.setLastName("Reese");
s2.setFirstName("Tony");
s2.setAge(18);
s2.setCourse("MA");
s2.setYear("1st Year");
s2.setStudentID(20180003);
s3.setLastName("Johnson");
s3.setFirstName("Liza");
s3.setAge(18);
s3.setCourse("BA");
s3.setYear("1st Year");
s3.setStudentID(20180004);
s4.setLastName("Nguyen");
s4.setFirstName("Kim");
s4.setAge(17);
s4.setCourse("ECE");
s4.setYear("1st Year");
s4.setStudentID(20180005);
}
public static void clear() {
for(int x = 0; x<=100; x++) {
System.out.println();
}
}
}
学生班
package studentdirectory;
public class Student extends Student201{
private String lastName;
private String firstName;
private String course;
private String year;
private int age;
private int studentID;
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getCourse() {
return course;
}
public void setCourse(String course) {
this.course = course;
}
public String getYear() {
return year;
}
public void setYear(String year) {
this.year = year;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getStudentID() {
return studentID;
}
public void setStudentID(int studentID) {
this.studentID = studentID;
}
@Override
public String toString() {
return "|\tID: " + this.getStudentID() + "\t|\t Name: " + this.getLastName() + ", "
+ this.getFirstName() + "\t|\t Course: " + this.getCourse() + "\t|\t Year: " +
this.getYear() + "\t\t|\t Age: " + this.getAge() + "\t|";
}
}
Student201班
package studentdirectory;
public class Student201{
public static void listStudents(Student s0, Student s1, Student s2, Student s3, Student s4) {
System.out.println(s0);
System.out.println(s1);
System.out.println(s2);
System.out.println(s3);
System.out.println(s4);
}
}
答案 0 :(得分:0)
最后,提出了解决方案。
首先问题不在于输入问题在于loop
do while循环首先执行语句,然后检查condition
。
我做的是我创建了一个infinite loop
然后接受输入,输入之后我放了condition
,如果input
退出则会打破loop
。< / p>
像这样的东西
while (true){
input = Scanner.nextLine();
if(input.toUpperCase().equals("EXIT"))
{
break;
}
else
{
// Put the whole logic inside this
}
你必须这样做,否则即使输入为exit
,循环也会自动执行一次。
希望有帮助
答案 1 :(得分:0)
我通过放置&#34; Scanner.nextLine();&#34;紧随&#34; Scanner.nextInt();&#34;。 这阻止了它跳过input = Scanner.nextLine();