我正在尝试使用do while循环来确定用户是否想要将狗或猫检入
Java中的狗窝系统.这个想法是他们在提示时输入“dog”或“cat”,任何条目都会导致错误,并且会再次提示他们输入文件名.
如果输入了“cat”或“dog”,则会将等效文件分配给程序(dogs.txt或cats.txt),然后系统将运行并将该数据加载到程序中.
以下是当前变量:
private String filename; // holds the name of the file
private Kennel kennel; // holds the kennel
private Scanner scan; // so we can read from keyboard
private String tempFileName;
private String dogsFile = "dogs.txt";
private String catsFile = "cats.txt";
以及导致问题的方法:
private KennelDemo() {
scan = new Scanner(System.in);
boolean fileNotCorrect = false;
System.out.print("Which animal are you looking to check into the kennel?: " + "\n");
System.out.println("Dog");
System.out.println("Cat");
tempFileName = scan.next();
do {
tempFileName.equals("dog");
filename = dogsFile;
fileNotCorrect = true;
/*tempFileName.equals("cat");
filename = catsFile;
fileNotCorrect = true;*/
}
while(fileNotCorrect = false);
System.out.println("That is not a valid filename, please enter either 'dog' or 'cat' in lowercase.");
这是运行代码时打印的内容:
**********HELLO***********
Which animal are you looking to check into the kennel?:
Dog
Cat
cat
That is not a valid filename, please enter either 'dog' or 'cat' in lowercase.
Using file dogs.txt
无论输入什么,它都会为程序分配文件,然后继续加载程序.
我尝试过使用catch {但由于某种原因它不起作用,有人能提供任何帮助吗?
谢谢!