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-每个循环迭代后不递增的累加器变量_Java_Loops_Output - Fatal编程技术网

Java-每个循环迭代后不递增的累加器变量

Java-每个循环迭代后不递增的累加器变量,java,loops,output,Java,Loops,Output,当汽车离开时,汽车在车库内移动的次数应与汽车牌照一起显示。我目前得到的输出显示一切正常,但所有汽车的移动次数仍然为0。我在计算如何增加变量和在输出中显示累积值时遇到困难。我怎样才能修好它 汽车等级 package garagetester; public class Car { private String licensePlate;//stores the license plate of the car as a String private int movesCount

当汽车离开时,汽车在车库内移动的次数应与汽车牌照一起显示。我目前得到的输出显示一切正常,但所有汽车的移动次数仍然为0。我在计算如何增加变量和在输出中显示累积值时遇到困难。我怎样才能修好它

汽车等级

package garagetester;

public class Car 
{
    private String licensePlate;//stores the license plate of the car as a String
    private int movesCount = 0;//stores the number of times the car has been 
                           //moved 


    public Car(String licensePlate)//builds a Car object with 
    {
       this.licensePlate = licensePlate;
    } 


    public String getLicensePlate() {
        return licensePlate;
    }


    public int getMovesCount() {
        return movesCount;
    }


    public void incrementMovesCount(int movesCount) {
        movesCount++;
     }
}//end of Car class
车库类

package garagetester;


public class Garage {

private Car[] garage; //array, stores Car objects 
private final int LIMIT = 10; //determines length of the garage array 
private int count;//number of cars in garage 

public Garage() {
    garage = new Car[LIMIT];
    //creates an array of 10 elements 
    //first index = 0, last index = 9

public String arrive(Car newCar) {
    String str = ""; //stores the result of the ARRIVE operation            
    /* If the garage is empty, the first car to arrive is parked in 
            the first empty spot closest to the exit*/ 
    if (count != LIMIT) {
        garage[count] = newCar;
        count++;
        str = newCar.getLicensePlate() + " has been parked";
    } else {
        str = "Sorry, " + newCar.getLicensePlate() + " the garage is full.";
    }
    return str;
}//end of arrive()

public String depart(String plate) {
    String str = ""; //stores the result of executing the operation 
    int moves =0; //stores the number of times a car has been moved 
    boolean found = false; //flag 
    for (int i = 0; i < count - 1; i++) //for all elements in the array
    {
        //check if car with that plate number is in the garage 
        if (plate.equals(garage[i].getLicensePlate())) 
        {
            found = true; //car has been found 
            if (found)//if found=true 
            {
                //for all cars ahead of it 
                for (int j = i + 1; j < count; j++)//check if count or count-1
                {   
                    moves += garage[j].getMovesCount();
                    garage[j].incrementMovesCount(moves);
                }
                //for all cars behind it 
                for (int k = i; k > 0; k--) //or k=i-1, check when debugging 
                {
                    //move all cars behind it one position up
                    garage[k] = garage[k - 1];
                }
                str = plate + " has departed." + "it has been moved " + moves 
                        + " times. "; 
                count--; //decrease the number of cars in the garage by 1 
            }
            else 
                {
                    str = "Sorry " + plate + " is not in the garage.";
                }
            }
        }//end of for loop 
        return str;//prints the value stored in str            
    } //end of depart() 
} //end of Garage class
您的参数MoveScont正在跟踪类成员MoveScont。在下列情况下:

public void incrementMovesCount(int movesCount) {
    // movesCount++; --> this is incrementing the parameter

    // either remove the parameter `movesCount` from this mutator
    // since it's not being used, or do the following
    this.movesCount++; // --> this refers to the class member movesCount
}

在您的增量方法中,Car.java中应该是这样的

同时修复此方法的其他用法。不需要向新值发送任何数据。Car对象有一个MoveScont字段。这意味着,它可以增加移动量本身

如果您不想更改方法签名,请使用此选项

public void incrementMovesCount(int newMovesCount) {
    this.movesCount = newMovesCount;  //--newMovesCount refers to your calculation
 }
但在使用最后一个解决方案时要小心,因为您正在以以下方式发送参数:

moves += garage[j].getMovesCount();  //this moves never set to 0. Just equal to zero in the first iteration.
garage[j].incrementMovesCount(moves);
我认为这是错误的。因为我想你们想增加汽车的所有位置。如果您想应用我的文章的第一个解决方案,只需修复编译错误。但如果你想应用第二种解决方案,只需将这部分修改为:

garage[j].incrementMovesCount(garage[j].getMovesCount()+1);

您应该在构造函数中初始化count变量,我认为incrementmoves中的参数与类成员同名。该参数是不需要的,因此您可以将其删除。@LiliaCurbeloJalil我会将此答案更改为可接受的答案,因为此答案的作者提供了比我自己更彻底的解决方案。@JonnyHenly刚刚做了这件事。谢谢你,我还在解决问题:
moves += garage[j].getMovesCount();  //this moves never set to 0. Just equal to zero in the first iteration.
garage[j].incrementMovesCount(moves);
garage[j].incrementMovesCount(garage[j].getMovesCount()+1);