从循环中断 [英] Breaks from loop

查看:76
本文介绍了从循环中断的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我已经验证了我的代码,所以它一直重复,直到被问到继续?时给出是或否。但是我的代码在输入随机值后从循环中断开然后是。
例如:

first, I have validated my code so it keeps repeating until a "yes" or a "no" is given when being asked "Continue?". But my code breaks from the loop after entering a random value and then yes. for example:

Add or delete another name? Add
Please enter a name you want to add: Matt
Continue? f
Continue? yes

它应该说:

Add or delete another name? Add
Please enter a name you want to add: Matt
Continue? f
Continue? yes
Add or delete another name?

实际代码

import java.io.File;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Scanner;


public class AddOrDeleteNames {
public static void main(String[] args) throws Exception {

    ArrayList<String> names = new ArrayList<String>();
    Scanner scan = new Scanner(new File("names.txt"));
    Scanner myScan = new Scanner(System.in);
    Scanner scanRedo = new Scanner(System.in);
    String userRedo;
    String userResponse;

    while (scan.hasNext())
        names.add(scan.next());

    do {    
      System.out.print("Add or delete another name? ");
      userResponse = myScan.next();

      if (userResponse.equalsIgnoreCase("add")) {
        System.out.print("Please enter a name you want to add: ");
        names.add(myScan.next());
      } else if (userResponse.equalsIgnoreCase("delete")) {
        System.out.print("Please enter a name you want to delete: ");
        names.remove(myScan.next());
      } else {
        System.out.print("Invalid Choice");
      }

      PrintWriter writer = new PrintWriter("namesupdated.txt");
      for (int i = 0; i < names.size(); i++)
        writer.println(names.get(i));

      writer.close();

      System.out.print("Continue? ");
      userRedo = scanRedo.next();
    } while (userRedo.equalsIgnoreCase("yes"));

    do {
        if(userRedo.equalsIgnoreCase("no")) {
            System.out.print("Thank You.");
            userRedo = scanRedo.next();
        } else if (!userRedo.equalsIgnoreCase("yes")) {
            System.out.print("Continue? ");  // LOOP ENDS EARLY : FIX!
            userRedo = scanRedo.next();
        }
    } while (!userRedo.equalsIgnoreCase("yes")); 

    scan.close();
    myScan.close();
    scanRedo.close();
}

}

推荐答案

对不起,我看得不够好。

I'm sorry, I didn't look it through well enough.

Java从上到下,从左到右读取内容。你做的是在主要的一个之后部署另一个 do ... while 循环。所以,在你说是继续第二个之后......当循环停止执行并关闭所有扫描仪时。

Java reads things top to bottom, left to right. What you did is deploy another do...while loop after the main one. So, after you say "yes" to continuing the second do...while loop stops executing and you close all your scanners.

do {
    if(userRedo.equalsIgnoreCase("no")) {
        System.out.print("Thank You.");
        userRedo = scanRedo.next();
    } else if (!userRedo.equalsIgnoreCase("yes")) {
        System.out.print("Continue? ");  // LOOP ENDS EARLY : FIX!
        userRedo = scanRedo.next();
    }
} while (!userRedo.equalsIgnoreCase("yes")); 

scan.close();
myScan.close();
scanRedo.close();

}

之后你就关闭了你的班级和程序假设它没有什么可做的。如果你调用一个单独的方法,Java只会在它的读取中重新开始。所以,为了让它恢复并让你保持你的 do ... while 循环,你只需要将它们放在单独的方法中,在批准时调用它们回答是给定的。

After that you just close your class and the program assumes it has nothing left to do. Java only "goes back up" in it's reading if you call a separate method. So, for it to go back up and for you to keep both of your do...while loops you just need to put them in separate methods, calling them when the approved answer is given.

例如:

//Declare variables up here, it will make the entire class have access to them

public static void main(String[] args){
        loop();
    }
public static void loop(){
    //do...while here

    System.out.println("Continue?");
    check();
}
public static void check(){
    //Scanner.in()
    //do..while #2 here
    //User enters yes then:
    loop();
    //User enters no then:
    //whatever you want here
}

这篇关于从循环中断的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆