使用“i”作为我的数组索引后,为什么我得到1?

时间:2015-06-07 02:01:45

标签: java arrays

我创造了一个名为Car的类,其方法应该类似于真车的某些功能。我正试图让这辆车最重的汽车和最小的汽车发动机的数量

如下面的代码所示,您可以看到我使用索引进行了验证,然后将其与“i”进行比较。但是,无论我输入什么号码,我都会在两次验证中得到“1”。

可能有什么不对?我应该得到汽车的号码而不是“1”。

这是我的代码,以获得最重的汽车

int weight = x[i].getweight();
if(weight > max) {
    maxweight = weight;
    maxIndex = i;
}

我的汽车课程:

public class Car
{
    private String color;
    private int weight;
    private int state;
    private int fuel;
    private int Maxspeed ;
    private int engine;

    public Car() {
        this.color = "White";
        this.weight = 1000;
        this.state = 0;
        this.fuel =0;
        this.Maxspeed = 0;
        this.engine = 0;
    }

    public Car (String color, int weight,
            int state, int fuel, int Maxspeed 
            ) {
        this.color = color;
        this.weight = weight;
        this.state = state;
        this.fuel = fuel;
        this.Maxspeed = Maxspeed;
    }

    public String getColor() { return this.color; }

    public int getweight() { return this.weight; } 

    public int getstate() { return this.state; }

    public int getfuel() { return this.fuel; }

    public int getMaxspeed() { return this.Maxspeed; }

    public int getengine() { return this.engine; }


    public void setColor( String color ){
        this.color = color;
    }

    public void setweight( int weight ){
        this.weight = weight;
    }

    public void setstate( int state ){
        this.state = state;
    }

    public void setfuel( int fuel ){
        this.fuel = fuel;
    }

    public void setMaxspeed( int Maxspeed ){
        this.Maxspeed = Maxspeed;
    }

    public void setengine(int engine){
        this.engine = engine;
    }

    public void showdata() {
        System.out.println( "\nCar's color is: " + this.getColor() );
        System.out.println( "Car's weight is: " + this.getweight() );
        System.out.println( "State: " + this.getstate() );
        System.out.println( "Fuel: " + this.getfuel());
        System.out.println( "Max speed: "  + this.getMaxspeed());
    }

    public void accelerate( int speed ){
        if( this.getstate() == 0 || this.getstate() == 3 || 
            this.getstate() == 4 || this.getMaxspeed() < speed )
        {
            System.out.println("\nCar cannot accelerate...");
        }
        else {
            System.out.println("\nCar is accelerating...");
            this.setfuel(this.getfuel()-2);
            this.setstate(2);
            if( this.getfuel() <= 0 ) {
                this.setstate(4);
            }
        }
    }

    public void crash() {
        this.setstate(3);
        System.out.println("\nCrash!!!");
    }
    public void stop() {
        this.setstate(1);
        System.out.println("\nCar has stopped.");
    }

   public void addfuel(int fuel) {
        if(this.getstate() == 0 || this.getstate() == 4){
            this.setfuel(this.getfuel()+ fuel);
        }
        else {
            System.out.println("You can't add fuel.");
        }
    }

    public void repair() {
        if(this.getstate() == 3){
           this.setstate(1);
           System.out.println("The car has been repaired");
        }
        else{
           System.out.println("The car is not broken");
        } 
    }
}

我的主要

import java.util.Scanner;
public class aaa {
    public static void main (String args []) {    
        Car x[] = new Car[2];
        int keep=1;
        int counter = 0;
        int counter_stopped = 0;
        int max = Integer.MIN_VALUE;
        int min = Integer.MAX_VALUE;
        int maxIndex = 0;
        int maxweight = 0;
        int index_engine = 0;
        int min_engine = 0;

        Scanner input = new Scanner(System.in);

        for(int i = 0; i < x.length; i++) {
            String color;
            int weight;
            int fuel;
            int Maxspeed;
            int engine;

            x[i] = new Car();

            System.out.print("\nEnter car color " + (i + 1) + ": ");
            color = input.next();

            System.out.print("Enter car weight " + (i + 1) + ": ");
            weight = input.nextInt();

            System.out.print("Enter car fuel " + (i + 1) + ": ");
            fuel = input.nextInt();

            System.out.print("Enter car max speed " + (i + 1) + ": ");
            Maxspeed = input.nextInt();

            System.out.print("Enter car engine weight " + (i + 1) + ": ");
            engine = input.nextInt();


            x[i].setColor(color);
            x[i].setweight(weight);
            x[i].getstate();
            x[i].setfuel(fuel);
            x[i].setMaxspeed(Maxspeed);
            x[i].setengine(engine);
        }        

        for(int i = 0; i < x.length; i++) {
            int state;

            System.out.print("\nEnter car state " + (i + 1) + ": ");
            state = input.nextInt();
            x[i].setstate(state);

            while(state > 4 || state < 0){
                System.out.print("state not valid.\nTry again: ");
                state = input.nextInt();
                x[i].setstate(state);
            }

            do {
                keep = menu();

                switch( keep ) {
                case 1:
                    accelerate(x[i]);
                    break;

                case 2:
                    stop(x[i]);
                    break;

                case 3:
                    crash(x[i]);
                    break;

                case 4:
                    addfuel(x[i]);
                    break;

                case 5:
                    repair(x[i]);
                    break;      

                case 6:
                    x[i].showdata();
                }
            } while(keep != 7);   

            if(x[i].getstate() == 4 || x[i].getfuel() <= 0){
                counter += 1;
            }

            if(x[i].getstate() == 1){
                counter_stopped += 1;
            }

            int weight = x[i].getweight();
            if(weight > max){
                maxweight = weight;
                maxIndex = i;
            }

            int weightengine = x[i].getengine();
            if(weightengine < min){
                min_engine = weightengine;
                index_engine = i;
            }
        }

        System.out.println("\nSUMMARY");
        System.out.println("Amount of cars with no fuel: " + counter);
        System.out.println("Amount of stopped cars: " + counter_stopped);
        System.out.println("Heaviest car: " + maxIndex);
        System.out.println("Car with the smallest engine: " + index_engine);
        System.out.println("=============================================");
    }

    public static int menu() {
      int option = 0;
      Scanner s = new Scanner(System.in);

      System.out.println("\n1. Accelerate Car ");
      System.out.println("2. Stop Car ");
      System.out.println("3. Crash Car ");
      System.out.println("4. Add fuel ");
      System.out.println("5. Repair ");
      System.out.println("6. Show data ");
      System.out.println("7. Exit ");
      System.out.println("=============================================");
      System.out.print("Choose an option : ");
      option = s.nextInt();
      System.out.println("=============================================");
      return option;
    } 

    public static void accelerate(Car myCar){

        Scanner input = new Scanner(System.in);  
        int s;

        System.out.print("Enter speed: ");
        s =  input.nextInt();
        myCar.accelerate(s);
        //myCar.showdata();
    }

    public static void stop(Car myCar){
        myCar.stop();
    }

    public static void crash(Car myCar){
        myCar.crash();
    }

    public static void addfuel(Car myCar){
        int fuel;
        Scanner input = new Scanner(System.in);  
        System.out.print("Amount to add: ");
        fuel = input.nextInt();
        myCar.addfuel(fuel);
    }

    public static void repair(Car myCar){
        myCar.repair();
    }
}

现在,当我编译并测试哪个引擎或汽车更小或更重时,我得到数字1作为结果。

1 个答案:

答案 0 :(得分:0)

最明显的问题是

if(weight > max){
    maxweight = weight;

您正在比较maxweight,但随后是maxweight。另外,我建议你喜欢Math.max(int, int)Math.min(int, int)喜欢

max = Math.max(max, weight);
min = Math.min(min, weight);

修改要存储minmax值的索引,您可以将maxmin初始化为0,从索引1循环到x.length。这可能看起来像

int max = 0;
int min = 0;
for (int i = 1; i < x.length; i++) {
    if (x[i].getweight() > x[max].getweight) {
        max = i;
    } else if (x[i].getweight() < x[min].getweight) {
        min = i;
    }
}