首页 > 解决方案 > 为什么一直重印菜单提示,后面跟着,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);

标签: java

解决方案


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指外循环一起使用,labeledtest

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");
}

推荐阅读