简体   繁体   English

如何避免Java中的无限循环

[英]How can I avoid infinite loop in Java

How can i re write this so I'm not using a while(true) loop? 我该如何重新编写此代码,以便不使用while(true)循环? I need the method to break upon the conditions of the while loop and I can't seem to work it out. 我需要打破while循环条件的方法,但似乎无法解决。

ArrayList<Account> accounts = new ArrayList<Account>();

public void enterCustomers()
{
   System.out.println("Enter customer names or q to quit entering names");

   while(true)
    {
        Scanner scan = new Scanner(System.in);                          //find alternative to while true

        System.out.print("Enter a customer name: ");
        String name = scan.nextLine();

        if(name.equalsIgnoreCase("q"))
        {
            break;
        }

        System.out.print("Enter openning balance: ");
        Double balance = scan.nextDouble();

        Account a = new Account(name, balance);
        accounts.add(a);}
}

So if you want to remove the while(true) loop, you could use the following: 因此,如果要删除while(true)循环,可以使用以下代码:

String name = scan.nextLine();
while(!name.equalsIgnoreCase("q")){
  //do stuff here
  name = scan.nextLine();
}

Or an even better way would be, (avoiding the duplicate name assignments,) using the do while loop, because do while would check the condition after we enter the loop: 或者更好的方法是使用do while循环(避免重复的名称分配),因为do while将在进入循环后检查条件:

String name;
do{
  name = scan.nextLine();
  //do stuff here
}while(!name.equalsIgnoreCase("q"));

I think the easiest way to approach this is to set the condition of a while loop to the opposite of the if condition like so: 我认为解决此问题的最简单方法是将while循环的条件设置为与if条件相反的条件,如下所示:

ArrayList<Account> accounts = new ArrayList<Account>();

public void enterCustomers()
{
   Scanner scan = new Scanner(System.in);
   System.out.println("Enter customer names or q to quit entering names");
   System.out.println("Enter a customer name:");
   Stirng name = scan.nextLine();

   while(!name.equalsIgnoreCase("q"))
    {     
        System.out.print("Enter openning balance: ");
        Double balance = scan.nextDouble();

        Account a = new Account(name, balance);
        accounts.add(a);            

        System.out.print("Enter a customer name: ");
        name = scan.nextLine();
    }
}

first of all be aware of scan.nextDouble() as it is reading the number but not the breakline, you will have to add a dummy scan.nextLine() or sth similar to read the break line after the number. 首先要知道scan.nextDouble()正在读取数字,而不是折线,您必须添加一个虚拟scan.nextLine()或类似的东西,以读取数字后的折线。

I always prefer to have methods doing one thing for example askForData(Scanner scan), so It would look like that: 我总是喜欢让方法做一件事情,例如askForData(Scanner scan),所以看起来像这样:

import java.util.Scanner;

public class SomeTest {

public static void main(String[] args) {
    System.out.println("Enter customer names or q to quit entering names");
    Scanner scan = new Scanner(System.in);
    String name="notExit"; //some name that is not exiting
    while(!name.equalsIgnoreCase("q")){
        name = askForData(scan);
    }
}

private static String askForData(Scanner scan) {
    System.out.print("Enter a customer name: ");
    String name = scan.nextLine();
    if (!name.equalsIgnoreCase("q")) {
        System.out.print("Enter openning balance: ");
        Double balance = scan.nextDouble();
        scan.nextLine(); //to read the break line
    }
    return name;
}
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM