我可以從用戶的scan2 scanner.But掃描不會產生在此代碼中我怎樣才能進入這while while循環我無法從用戶掃描掃描儀獲得輸入。我如何從用戶獲得掃描儀掃描儀的輸入。Java從用戶獲得輸入
Scanner scan2 = new Scanner(System.in);
System.out.println("Register Section\nID: ");
String id = scan2.next();
System.out.println("Password: ");
String pass = scan2.next();
createAccount(id, pass, accounts);
System.out.println("Login Section\nID: ");
id = scan2.next();
System.out.println("Password: ");
pass = scan2.next();
boolean X = logIN(id, pass, students);
scan2.close();
//Login succeed
if(X){
Scanner scan = new Scanner(System.in);
String line = "";
while (scan.hasNextLine()) {
line = scan.nextLine();
if (line.equalsIgnoreCase("quit"))
System.exit(0);
int timetablestart = line.indexOf('[');
int timetablefinish = line.indexOf(']');
String strlistoftimetable = "";
String command = line;
String[] timetableslots = null;
if ((timetablestart > 0) && (timetablefinish > 0)) {
strlistoftimetable = line.substring(timetablestart + 1,
timetablefinish);
timetableslots = strlistoftimetable.split(", ");
command = line.substring(0, timetablestart - 1);
}
String[] tokens = command.split("\\s");
if (tokens.length < 2) {
System.out.println("something is wrong!");
return;
}
if (tokens[0].equals("add")) {
if (tokens[1].equals("O")) {
CSDriver.addO(tokens,timetableslots,departments);
} else if (tokens[1].equals("C")) {
CSDriver.addC(tokens, students);
} else if (tokens[1].equals("I")) {
CSDriver.addI(tokens,instructors);
}
}
else if (tokens[0].equals("print")) {
if (tokens[1].equals("S")) {
CSDriver.printS(tokens, students);
} else if (tokens[1].equals("I")) {
CSDriver.printI(tokens,instructors);
}
else if (tokens[1].equals("Announcement")) {
CSDriver.printAnnouncement(departments);
}
else if (tokens[1].equals("AcedemicActivities")) {
CSDriver.printAcedemicActivities(departments);}
else if (tokens[1].equals("Attendance")) {
CSDriver.checkAttendance(tokens, students);
}
else if (tokens[1].equals("Internship")) {
CSDriver.checkInternship(tokens, students);
}
else if (tokens[1].equals("SemesterGrades")) {
CSDriver.checkSemesterGrades(tokens, students);
}
} else
System.out.println("something is wrong!");
}
scan.close();
} }
我們需要看到您的「登錄」的方法。很可能不會將布爾X設置爲true – bpgeck
line = scan.nextLine();即使X = true也無法正常工作 –
請包括重現問題所需的最短程序。我們不能用您提供的代碼重現問題。 –