java 两层while_java – while while循环满足2个条件之一

在Java程序中,开发者遇到一个问题,使用dowhile循环检查用户是否想将狗或猫检入狗窝系统。用户输入“dog”或“cat”时应分配相应文件,但无论输入什么,程序总是分配“dogs.txt”并继续运行。问题可能在于循环条件的设置和错误处理。
摘要由CSDN通过智能技术生成

我正在尝试使用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 {但由于某种原因它不起作用,有人能提供任何帮助吗?

谢谢!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值