我不知道出了什么问题,我的教授也被难住了。只有当条件立即满足时,while 循环条件才会终止。一旦循环运行,满足条件就不再停止它,它只会继续运行。看起来好像不再检查状况了?下面是我的代码。感谢您的任何帮助。
import java.io.*;
import java.util.*;
public class createPhoneList {
public static void main(String[] args) {
DataOutputStream ostream;//creates output stream
Scanner input = new Scanner(System.in);//creates scanner in object
final String DONE = "DONE";//will be used to end data entry loop
String firstName;
String lastName;
long phoneNumber;
try{
ostream = new DataOutputStream(new FileOutputStream("javaContactsUnit4Assignment.txt"));
System.out.print("Enter First Name or type 'DONE' to quit: ");
firstName = input.nextLine();
while(!firstName.equalsIgnoreCase(DONE)){
/*
* Error occuring at runtime where while loop terminates only if
* "done" is typed first, but once the loop is running "done" no longer
* stops it. Not sure what is wrong...
*/
input.nextLine();
System.out.print("Please enter Last Name: ");
lastName = input.nextLine();
System.out.print("Please enter the Phone Number(with NO DASHES, as they will cause a fatal error): ");
phoneNumber = input.nextLong();
ostream.writeUTF(firstName);
ostream.writeUTF(lastName);
ostream.writeLong(phoneNumber);
System.out.print("Enter First Name or type 'DONE' to quit: ");
firstName = input.nextLine();
}
}
catch(IOException e){
System.err.println("Error opening file.");
}
catch(InputMismatchException e){
System.err.println("Invalid data was entered. Please try again.");
}
catch(Exception e){
System.err.println("You encountered a fatal error. Please try again.");
}
}
}