Java 为什么它不断地重印菜单提示,后面跟着,监视器语句。我需要它重新启动并再次运行,直到使用进入退出。

Java 为什么它不断地重印菜单提示,后面跟着,监视器语句。我需要它重新启动并再次运行,直到使用进入退出。,java,Java,对不起。。。我问错了问题。。。。。我稍微修改了我的代码。当我使用while语句时,我理解使用continue,但是它会重新打印主菜单提示,但它也会重新打印Monitor语句。这是更新后的代码。 顺便说一句,不是在寻找答案,只是一些指导 public static void main(String[] args) throws IOException { Scanner scnr = new Scanner(System.in); Scanner inFS = null;

对不起。。。我问错了问题。。。。。我稍微修改了我的代码。当我使用while语句时,我理解使用continue,但是它会重新打印主菜单提示,但它也会重新打印Monitor语句。这是更新后的代码。 顺便说一句,不是在寻找答案,只是一些指导

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

描述使用带有
标签的
break
语句来中断循环,这可能就是您所指的

break
是退出循环的关键字
continue
是一个关键字,用于退出循环的
迭代
,并使用循环的下一个元素

break
在这里的if语句中,如果为true,将退出循环并继续程序

  for (i = 0; i < arrayOfInts.length; i++) {
        if (arrayOfInts[i] == searchfor) {
            foundIt = true;
            break;
        }
    }
它还可以与
continue
一起使用,后者指的是外部循环,
标签
ed
测试

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

对于(inti=0;i谢谢你的帮助……我想出来了

public class MonitoringSystem {


/**
 * @param args the command line arguments
 * @throws java.io.IOException
 * 
 */
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 = "";


    while (!userInput.equals("exit")) {
    System.out.println("Would you like to monitor an Animal, Habitat, or exit?");
    monitorChoice = scnr.nextLine();


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



                }
            }
        }
        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);
                }
            }
        }
        else {
            System.out.println("Have a great day!");
            break;
                }
         }
   }

}

这句话可能就是你想要的。@dave抱歉,显然我的问题不清楚。继续不会解决我的问题。特别是考虑到我完全忘记了包含while语句……谢谢你的帮助。
public class MonitoringSystem {


/**
 * @param args the command line arguments
 * @throws java.io.IOException
 * 
 */
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 = "";


    while (!userInput.equals("exit")) {
    System.out.println("Would you like to monitor an Animal, Habitat, or exit?");
    monitorChoice = scnr.nextLine();


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



                }
            }
        }
        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);
                }
            }
        }
        else {
            System.out.println("Have a great day!");
            break;
                }
         }
   }