gpt4 book ai didi

java - 我的 for 循环不会中断,无限打印数组

转载 作者:行者123 更新时间:2023-12-01 13:56:25 27 4
gpt4 key购买 nike

我有一个应该吐出数组内容的方法。但是,当我运行它时,它会无限循环。

这是我的代码:

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

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

}

这是怎么回事?

编辑:顺便说一句,这是 Java。这是我的整个代码:

Main.java:

package csc;
import java.util.*;
import java.io.*;
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws Exception {


System.out.println("Welcome to the Car Database");

Scanner keyboard = new Scanner(System.in);
System.out.println("Enter the size of the array:");
int input = keyboard.nextInt();
keyboard.nextLine();
CarDatabase cdb = new CarDatabase(input);



System.out.println("Enter the name of the input file:");
cdb.readFile(keyboard.next());

Scanner sc = new Scanner(System.in);
System.out.println("Enter make, mpg, weight, all, or quit:");
String command = sc.nextLine();


while(!command.equals("quit")) {

if(command.equals("all")) {
cdb.displayAll();
//some other methods
} }

汽车.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);
}
}

CarDatabase.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

该对象是获取 csv 文件并将对象放入数组中,并让用户搜索特征。例如:品牌、型号、英里数。但我的数组确实无限循环,即使我告诉它是 10 个槽或类似的小东西。

以下是具有 10 个插槽的 netbeans 的一些输出:

    Welcome to the Car Database
Enter the size of the array:
10
Enter the name of the input file:
cardb.csv
Enter make, mpg, weight, all, or quit:
all
Model:chevelle malibu Make:chevrolet mpg:18.0 weight:3504 year:70
Model:skylark 320 Make:buick mpg:15.0 weight:3693 year:70
Model:satellite Make:plymouth mpg:18.0 weight:3436 year:70
Model:rebel sst Make:amc mpg:16.0 weight:3433 year:70
Model:torino Make:ford mpg:17.0 weight:3449 year:70
Model:galaxie 500 Make:ford mpg:15.0 weight:4341 year:70
Model:impala Make:chevrolet mpg:14.0 weight:4354 year:70
Model:fury iii Make:plymouth mpg:14.0 weight:4312 year:70
Model:catalina Make:pontiac mpg:14.0 weight:4425 year:70
Model:ambassador dpl Make:amc mpg:15.0 weight:3850 year:70
Model:chevelle malibu Make:chevrolet mpg:18.0 weight:3504 year:70
Model:skylark 320 Make:buick mpg:15.0 weight:3693 year:70
Model:satellite Make:plymouth mpg:18.0 weight:3436 year:70
Model:rebel sst Make:amc mpg:16.0 weight:3433 year:70
Model:torino Make:ford mpg:17.0 weight:3449 year:70
Model:galaxie 500 Make:ford mpg:15.0 weight:4341 year:70
Model:impala Make:chevrolet mpg:14.0 weight:4354 year:70
Model:fury iii Make:plymouth mpg:14.0 weight:4312 year:70
Model:catalina Make:pontiac mpg:14.0 weight:4425 year:70
Model:ambassador dpl Make:amc mpg:15.0 weight:3850 year:70
Model:chevelle malibu Make:chevrolet mpg:18.0 weight:3504 year:70
Model:skylark 320 Make:buick mpg:15.0 weight:3693 year:70
Model:satellite Make:plymouth mpg:18.0 weight:3436 year:70
Model:rebel sst Make:amc mpg:16.0 weight:3433 year:70
Model:torino Make:ford mpg:17.0 weight:3449 year:70
Model:galaxie 500 Make:ford mpg:15.0 weight:4341 year:70
Model:impala Make:chevrolet mpg:14.0 weight:4354 year:70
Model:fury iii Make:plymouth mpg:14.0 weight:4312 year:70
Model:catalina Make:pontiac mpg:14.0 weight:4425 year:70
Model:ambassador dpl Make:amc mpg:15.0 weight:3850 year:70
Model:chevelle malibu Make:chevrolet mpg:18.0 weight:3504 year:70
Model:skylark 320 Make:buick mpg:15.0 weight:3693 year:70
Model:satellite Make:plymouth mpg:18.0 weight:3436 year:70
Model:rebel sst Make:amc mpg:16.0 weight:3433 year:70
Model:torino Make:ford mpg:17.0 weight:3449 year:70
Model:galaxie 500 Make:ford mpg:15.0 weight:4341 year:70
Model:impala Make:chevrolet mpg:14.0 weight:4354 year:70
Model:fury iii Make:plymouth mpg:14.0 weight:4312 year:70
Model:catalina Make:pontiac mpg:14.0 weight:4425 year:70
Model:ambassador dpl Make:amc mpg:15.0 weight:3850 year:70
Model:chevelle malibu Make:chevrolet mpg:18.0 weight:3504 year:70
Model:skylark 320 Make:buick mpg:15.0 weight:3693 year:70
Model:satellite Make:plymouth mpg:18.0 weight:3436 year:70
Model:rebel sst Make:amc mpg:16.0 weight:3433 year:70
Model:torino Make:ford mpg:17.0 weight:3449 year:70
Model:galaxie 500 Make:ford mpg:15.0 weight:4341 year:70
Model:impala Make:chevrolet mpg:14.0 weight:4354 year:70
Model:fury iii Make:plymouth mpg:14.0 weight:4312 year:70
Model:catalina Make:pontiac mpg:14.0 weight:4425 year:70
Model:ambassador dpl Make:amc mpg:15.0 weight:3850 year:70
Model:chevelle malibu Make:chevrolet mpg:18.0 weight:3504 year:70
Model:skylark 320 Make:buick mpg:15.0 weight:3693 year:70
Model:satellite Make:plymouth mpg:18.0 weight:3436 year:70
Model:rebel sst Make:amc mpg:16.0 weight:3433 year:70
Model:torino Make:ford mpg:17.0 weight:3449 year:70
Model:galaxie 500 Make:ford mpg:15.0 weight:4341 year:70
Model:impala Make:chevrolet mpg:14.0 weight:4354 year:70
Model:fury iii Make:plymouth mpg:14.0 weight:4312 year:70
Model:catalina Make:pontiac mpg:14.0 weight:4425 year:70
Model:ambassador dpl Make:amc mpg:15.0 weight:3850 year:70
Model:chevelle malibu Make:chevrolet mpg:18.0 weight:3504 year:70
Model:skylark 320 Make:buick mpg:15.0 weight:3693 year:70
Model:satellite Make:plymouth mpg:18.0 weight:3436 year:70
Model:rebel sst Make:amc mpg:16.0 weight:3433 year:70
Model:torino Make:ford mpg:17.0 weight:3449 year:70
Model:galaxie 500 Make:ford mpg:15.0 weight:4341 year:70
Model:impala Make:chevrolet mpg:14.0 weight:4354 year:70
Model:fury iii Make:plymouth mpg:14.0 weight:4312 year:70
Model:catalina Make:pontiac mpg:14.0 weight:4425 year:70
Model:ambassador dpl Make:amc mpg:15.0 weight:3850 year:70
Model:chevelle malibu Make:chevrolet mpg:18.0 weight:3504 year:70
Model:skylark 320 Make:buick mpg:15.0 weight:3693 year:70
Model:satellite Make:plymouth mpg:18.0 weight:3436 year:70
Model:rebel sst Make:amc mpg:16.0 weight:3433 year:70
Model:torino Make:ford mpg:17.0 weight:3449 year:70
Model:galaxie 500 Make:ford mpg:15.0 weight:4341 year:70
Model:impala Make:chevrolet mpg:14.0 weight:4354 year:70
Model:fury iii Make:plymouth mpg:14.0 weight:4312 year:70
Model:catalina Make:pontiac mpg:14.0 weight:4425 year:70
Model:ambassador dpl Make:amc mpg:15.0 weight:3850 year:70
Model:chevelle malibu Make:chevrolet mpg:18.0 weight:3504 year:70
Model:skylark 320 Make:buick mpg:15.0 weight:3693 year:70
Model:satellite Make:plymouth mpg:18.0 weight:3436 year:70
Model:rebel sst Make:amc mpg:16.0 weight:3433 year:70
Model:torino Make:ford mpg:17.0 weight:3449 year:70
Model:galaxie 500 Make:ford mpg:15.0 weight:4341 year:70
Model:impala Make:chevrolet mpg:14.0 weight:4354 year:70
Model:fury iii Make:plymouth mpg:14.0 weight:4312 year:70
Model:catalina Make:pontiac mpg:14.0 weight:4425 year:70
Model:ambassador dpl Make:amc mpg:15.0 weight:3850 year:70
Model:chevelle malibu Make:chevrolet mpg:18.0 weight:3504 year:70
Model:skylark 320 Make:buick mpg:15.0 weight:3693 year:70
Model:satellite Make:plymouth mpg:18.0 weight:3436 year:70
Model:rebel sst Make:amc mpg:16.0 weight:3433 year:70
Model:torino Make:ford mpg:17.0 weight:3449 year:70
Model:galaxie 500 Make:ford mpg:15.0 weight:4341 year:70
Model:impala Make:chevrolet mpg:14.0 weight:4354 year:70
Model:fury iii Make:plymouth mpg:14.0 weight:4312 year:70
Model:catalina Make:pontiac mpg:14.0 weight:4425 year:70
Model:ambassador dpl Make:amc mpg:15.0 weight:3850 year:70
Model:chevelle malibu Make:chevrolet mpg:18.0 weight:3504 year:70
Model:skylark 320 Make:buick mpg:15.0 weight:3693 year:70
Model:satellite Make:plymouth mpg:18.0 weight:3436 year:70
Model:rebel sst Make:amc mpg:16.0 weight:3433 year:70
Model:torino Make:ford mpg:17.0 weight:3449 year:70
Model:galaxie 500 Make:ford mpg:15.0 weight:4341 year:70
Model:impala Make:chevrolet mpg:14.0 weight:4354 year:70
Model:fury iii Make:plymouth mpg:14.0 weight:4312 year:70
Model:catalina Make:pontiac mpg:14.0 weight:4425 year:70
Model:ambassador dpl Make:amc mpg:15.0 weight:3850 year:70
Model:chevelle malibu Make:chevrolet mpg:18.0 weight:3504 year:70
Model:skylark 320 Make:buick mpg:15.0 weight:3693 year:70
Model:satellite Make:plymouth mpg:18.0 weight:3436 year:70
Model:rebel sst Make:amc mpg:16.0 weight:3433 year:70
Model:torino Make:ford mpg:17.0 weight:3449 year:70
Model:galaxie 500 Make:ford mpg:15.0 weight:4341 year:70
Model:impala Make:chevrolet mpg:14.0 weight:4354 year:70
Model:fury iii Make:plymouth mpg:14.0 weight:4312 year:70
Model:catalina Make:pontiac mpg:14.0 weight:4425 year:70
Model:ambassador dpl Make:amc mpg:15.0 weight:3850 year:70
Model:chevelle malibu Make:chevrolet mpg:18.0 weight:3504 year:70
Model:skylark 320 Make:buick mpg:15.0 weight:3693 year:70
Model:satellite Make:plymouth mpg:18.0 weight:3436 year:70
Model:rebel sst Make:amc mpg:16.0 weight:3433 year:70
Model:torino Make:ford mpg:17.0 weight:3449 year:70
Model:galaxie 500 Make:ford mpg:15.0 weight:4341 year:70
Model:impala Make:chevrolet mpg:14.0 weight:4354 year:70
Model:fury iii Make:plymouth mpg:14.0 weight:4312 year:70
Model:catalina Make:pontiac mpg:14.0 weight:4425 year:70
Model:ambassador dpl Make:amc mpg:15.0 weight:3850 year:70
Model:chevelle malibu Make:chevrolet mpg:18.0 weight:3504 year:70
Model:skylark 320 Make:buick mpg:15.0 weight:3693 year:70
Model:satellite Make:plymouth mpg:18.0 weight:3436 year:70
Model:rebel sst Make:amc mpg:16.0 weight:3433 year:70
Model:torino Make:ford mpg:17.0 weight:3449 year:70
Model:galaxie 500 Make:ford mpg:15.0 weight:4341 year:70
Model:impala Make:chevrolet mpg:14.0 weight:4354 year:70
Model:fury iii Make:plymouth mpg:14.0 weight:4312 year:70
Model:catalina Make:pontiac mpg:14.0 weight:4425 year:70
Model:ambassador dpl Make:amc mpg:15.0 weight:3850 year:70
Model:chevelle malibu Make:chevrolet mpg:18.0 weight:3504 year:70
Model:skylark 320 Make:buick mpg:15.0 weight:3693 year:70
Model:satellite Make:plymouth mpg:18.0 weight:3436 year:70
Model:rebel sst Make:amc mpg:16.0 weight:3433 year:70
Model:torino Make:ford mpg:17.0 weight:3449 year:70
Model:galaxie 500 Make:ford mpg:15.0 weight:4341 year:70
Model:impala Make:chevrolet mpg:14.0 weight:4354 year:70
Model:fury iii Make:plymouth mpg:14.0 weight:4312 year:70
Model:catalina Make:pontiac mpg:14.0 weight:4425 year:70
Model:ambassador dpl Make:amc mpg:15.0 weight:3850 year:70
Model:chevelle malibu Make:chevrolet mpg:18.0 weight:3504 year:70
Model:skylark 320 Make:buick mpg:15.0 weight:3693 year:70
Model:satellite Make:plymouth mpg:18.0 weight:3436 year:70
Model:rebel sst Make:amc mpg:16.0 weight:3433 year:70
Model:torino Make:ford mpg:17.0 weight:3449 year:70
Model:galaxie 500 Make:ford mpg:15.0 weight:4341 year:70
Model:impala Make:chevrolet mpg:14.0 weight:4354 year:70
Model:fury iii Make:plymouth mpg:14.0 weight:4312 year:70
Model:catalina Make:pontiac mpg:14.0 weight:4425 year:70
Model:ambassador dpl Make:amc mpg:15.0 weight:3850 year:70
Model:chevelle malibu Make:chevrolet mpg:18.0 weight:3504 year:70
Model:skylark 320 Make:buick mpg:15.0 weight:3693 year:70
Model:satellite Make:plymouth mpg:18.0 weight:3436 year:70
Model:rebel sst Make:amc mpg:16.0 weight:3433 year:70
Model:torino Make:ford mpg:17.0 weight:3449 year:70
Model:galaxie 500 Make:ford mpg:15.0 weight:4341 year:70
Model:impala Make:chevrolet mpg:14.0 weight:4354 year:70
Model:fury iii Make:plymouth mpg:14.0 weight:4312 year:70
Model:catalina Make:pontiac mpg:14.0 weight:4425 year:70
Model:ambassador dpl Make:amc mpg:15.0 weight:3850 year:70
Model:chevelle malibu Make:chevrolet mpg:18.0 weight:3504 year:70
Model:skylark 320 Make:buick mpg:15.0 weight:3693 year:70
Model:satellite Make:plymouth mpg:18.0 weight:3436 year:70
Model:rebel sst Make:amc mpg:16.0 weight:3433 year:70
Model:torino Make:ford mpg:17.0 weight:3449 year:70
Model:galaxie 500 Make:ford mpg:15.0 weight:4341 year:70
Model:impala Make:chevrolet mpg:14.0 weight:4354 year:70
Model:fury iii Make:plymouth mpg:14.0 weight:4312 year:70
Model:catalina Make:pontiac mpg:14.0 weight:4425 year:70
Model:ambassador dpl Make:amc mpg:15.0 weight:3850 year:70
Model:chevelle malibu Make:chevrolet mpg:18.0 weight:3504 year:70
Model:skylark 320 Make:buick mpg:15.0 weight:3693 year:70
Model:satellite Make:plymouth mpg:18.0 weight:3436 year:70
Model:rebel sst Make:amc mpg:16.0 weight:3433 year:70
Model:torino Make:ford mpg:17.0 weight:3449 year:70
Model:galaxie 500 Make:ford mpg:15.0 weight:4341 year:70
Model:impala Make:chevrolet mpg:14.0 weight:4354 year:70
Model:fury iii Make:plymouth mpg:14.0 weight:4312 year:70
Model:catalina Make:pontiac mpg:14.0 weight:4425 year:70
Model:ambassador dpl Make:amc mpg:15.0 weight:3850 year:70
Model:chevelle malibu Make:chevrolet mpg:18.0 weight:3504 year:70
Model:skylark 320 Make:buick mpg:15.0 weight:3693 year:70
Model:satellite Make:plymouth mpg:18.0 weight:3436 year:70
Model:rebel sst Make:amc mpg:16.0 weight:3433 year:70

无限循环。

最佳答案

执行 displayAll 后,您可能不会向用户查询新命令吗?这意味着 command 将保留 "all",这意味着程序会一遍又一遍地执行 displayAll。尝试移动该线

String command = sc.nextLine();

在命令循环内。

关于java - 我的 for 循环不会中断,无限打印数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19601227/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com