Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 我的for loop赢了';t break,无限打印数组_Java_Loops_For Loop - Fatal编程技术网

Java 我的for loop赢了';t break,无限打印数组

Java 我的for loop赢了';t break,无限打印数组,java,loops,for-loop,Java,Loops,For Loop,我有一个方法,可以吐出数组的内容。然而,当我运行它时,它会无限循环 这是我的密码: public void displayAll() { for (int i = 0; i < cars.length; i++) { System.out.println(cars[i]); } Car.java: package csc; public class Car { String model; String make;

我有一个方法,可以吐出数组的内容。然而,当我运行它时,它会无限循环

这是我的密码:

    public void displayAll() {
    for (int i = 0; i < cars.length; i++) {

        System.out.println(cars[i]);

    }
Car.java:

    package csc;
public class Car {

    String model;
    String make;
    double mpg;
    int weight;
    int year;

    public Car(String carModel, String carMake, double carMpg, int carWeight, int carYear) {
        model = carModel;
        make = carMake;
        mpg = carMpg;
        weight = carWeight;
        year = carYear;
    }
    /**
     *
     * @return
     */
    @Override
    public String toString() {
        return ("Model:" + model + " Make:" + make + " mpg:" + mpg + " weight:" + weight + " year:" + year);
    }
}
java

    package csc;
import java.io.File;
import java.util.*;

public class CarDatabase {

    private Car[] cars;

    public CarDatabase(int s) {
        cars = new Car[s];
    }


    public void readFile(String a) throws Exception{
       Scanner sc = new Scanner(new File(a));
       Scanner sc2;
       for (int i = 0; i < cars.length; i++) {
           if (sc.hasNextLine()) {
               sc2 = new Scanner(sc.nextLine());
               sc2.useDelimiter(",");
               cars[i] = new Car(sc2.next(),sc2.next(),sc2.nextDouble(),sc2.nextInt(),sc2.nextInt());
               //System.out.println(cars[i]);
           }

       }

    }

    public void displayAll() {
        for (int i = 0; i < cars.length; i++) {

            System.out.println(cars[i]);

        }

    } //some other methods

无限期。

你确定程序挂在这个地方吗?我看不出这个循环有什么问题。尝试使用调试器或调试日志来定位问题。

执行
displayAll
后,您可能不会向用户查询新命令?这意味着
命令
将保持
“全部”
,这意味着程序会反复执行
displayAll
。试着移动这条线

String command = sc.nextLine();

在命令循环内。

通过在主类的while循环后重新提示用户来解决

if(command.equals("all")) {
     cdb.displayAll();
     sc.nextLine();
}

你能发布你的全部代码吗?请用你正在使用的语言标记你的问题。从我所看到的,你缺少一个括号?我不认为这是原因…你的代码是好的。也许你的数组很大,“无限”是什么意思?它是否循环并从一开始就列出数据?它是否反复列出相同的项目?请发布数组中的内容和输出。您提出的概念有效,但不是确切的代码。我要做的是在“cdb.displayAll();”之后添加“sc.nextLine();”,循环正常工作。谢谢的确,我忽略了一个事实,即循环需要在进入之前设置命令。我自己通常使用while(true)循环作为命令循环。很抱歉我的疏忽,很高兴你发现了!
String command = sc.nextLine();
if(command.equals("all")) {
     cdb.displayAll();
     sc.nextLine();
}