java - 为什么一直重印菜单提示,后面跟着,monitor语句。我需要它重新启动并再次运行,直到使用进入退出。
问题描述
抱歉......我问错了问题......我稍微修正了我的代码。当我使用 while 语句时,我理解使用 continue,但是它会重新打印主菜单提示,但它也会重新打印 Monitor 语句。这是更新的代码。PS 不是真的在寻找答案……只是一些指导。
public static void main(String[] args) throws IOException {
Scanner scnr = new Scanner(System.in);
Scanner inFS = null;
FileInputStream animals = null;
FileInputStream habitats = null;
BufferedReader reader = null;
String monitorChoice = "";
String animalChoice = "";
String habitatChoice = "";
String userInput = "";
System.out.println("Would you like to monitor an Animal, Habitat, or exit?");
monitorChoice = scnr.nextLine();
while (!userInput.equals("exit")){
if (monitorChoice.equals("Animal")) {
System.out.println("Choose an animal to monitor: Lion, Tiger, Bear, or Giraffe.");
animals = new FileInputStream("animals.txt");
animalChoice = scnr.nextLine();
File file = new File("animals.txt");
BufferedReader br = new BufferedReader(new FileReader(file));
String currentLine = br.readLine();
while ((currentLine = br.readLine()) != null) {
if (currentLine.contains(animalChoice)) {
String nameline = br.readLine();
String ageline = br.readLine();
String healthline = br.readLine();
String feedline = br.readLine();
healthline = healthline.replaceAll("[*]", "");
feedline = feedline.replaceAll("[*]", "");
System.out.println(currentLine);
System.out.println(nameline);
System.out.println(ageline);
System.out.println(feedline);
System.out.println(healthline);
System.out.println("Would you like to monitor an Animal, Habitat, or exit");
}
}
}
else if (monitorChoice.equals("Habitat")) {
System.out.println("Choose a habitat to monitor: Penguin, Bird, Aquarium.");
habitats = new FileInputStream("habitats.txt");
habitatChoice = scnr.nextLine();
File habitat = new File("habitats.txt");
BufferedReader bh = new BufferedReader(new FileReader(habitat));
String habitatLine = bh.readLine();
while ((habitatLine = bh.readLine()) != null) {
if (habitatLine.contains(habitatChoice)) {
String tempLine = bh.readLine();
String foodLine = bh.readLine();
String cleanLine = bh.readLine();
foodLine = foodLine.replaceAll("[*]", "");
cleanLine = cleanLine.replaceAll("[*]", "");
System.out.println(tempLine);
System.out.println(foodLine);
System.out.println(cleanLine);
解决方案
https://docs.oracle.com/javase/tutorial/java/nutsandbolts/branch.html
描述使用break
语句labels
来跳出循环,这可能是您所指的。
break
是退出循环的关键字。continue
是退出iteration
循环的关键字,并使用循环的下一个元素。
break
在这里的if语句里面,当为true时会退出循环,继续程序。
for (i = 0; i < arrayOfInts.length; i++) {
if (arrayOfInts[i] == searchfor) {
foundIt = true;
break;
}
}
这在处理嵌套循环时最有用,例如
在这个例子search:
中是在外部 for 循环之前的一个标签,它可以用来说明你试图跳出哪个循环,否则 break 将默认为最里面的循环。
search:
for (i = 0; i < arrayOfInts.length; i++) {
for (j = 0; j < arrayOfInts[i].length;
j++) {
if (arrayOfInts[i][j] == searchfor) {
foundIt = true;
break search;
}
}
}
它也可以与continue
指外循环一起使用,label
edtest
test:
for (int i = 0; i <= max; i++) {
int n = substring.length();
int j = i;
int k = 0;
while (n-- != 0) {
if (searchMe.charAt(j++) != substring.charAt(k++)) {
continue test;
}
}
foundIt = true;
break test;
}
System.out.println(foundIt ? "Found it" : "Didn't find it");
}
推荐阅读
- r - R Markdown 根据列中的值创建输出文件
- python - Django 回滚迁移不执行任何操作。表还在
- r - 获取列在 R 中具有一个或两个字符串的位置的行
- java - 使用 Maven pom.xml 文件来包含 ConfigurableApplicationContext 类的单个兼容版本
- node.js - 我的 Paypal 结帐流程 Angular-Node 安全吗?
- python - 从 URL 下载压缩文件
- java - 我正在尝试创建一个新行但它不起作用
- flutter - Flutter - 共享首选项在 ListView 中保存状态
- android - 如何将 arrayList 添加到警报对话框并选择其中之一?
- python - 在 Anaconda 中使用 PyAudio 和 SpeechRecognition python 2 时出错