使用数组&面向对象编程Java的方法

时间:2013-04-17 03:44:53

标签: java arrays oop methods

我正在学习Java,据说,我的知识仍处于学习java的爬行阶段。我已经读过在数组中使用非常强大的对象。我正在尝试编辑这个简单的示例类和测试类,以显示谁是最快的驱动程序,以及什么是最快的速度。我有两种方法在思考如何解决这个问题。一个是找到最大速度,并找到什么数组编号(例如数组[2])。

我设法捕获循环中的最高速度和数组编号。将它放入一个方法中,然后调用它会让我感到困惑。任何帮助深表感谢。

错误代码我得到了
TestBikeWithArray.java:73:错误:二元运算符'>'的坏操作数类型             if(bikeRace [i]> bikeRace [0])
                            ^
  第一种:自行车
  第二种:自行车
1错误

工具已完成,退出代码为1 他们都是自行车课程,我不明白。

public class Bike{

private String driver;
private int gear;
private int cadence;
private int speed;
private int id;

private static int numberOfBikes = 0;

public Bike(String theDriver, int startGear,int startCadence, int startSpeed){
    driver = theDriver;
    gear = startGear;
    cadence = startCadence;
    speed = startSpeed;
    numberOfBikes++;
    id = numberOfBikes;
}

public String getDriver() {return driver;}
public void setDriver(String d) {driver = d;}


public int getGear(){return gear;}
public void setGear(int g){gear = g;}

public int getCadence(){return cadence;}
public void setCadence(int c){cadence = c;}


public int getSpeed(){return speed;}
public void setSpeed(int s) {speed = s;}

public int getID() {return id;}

public static int getNumberOfBikes() {return numberOfBikes;}

// method to print out contents of object
public String toString ()
{
    StringBuffer sb = new StringBuffer();
    sb.append("\nDriver: " + driver);
    sb.append("\nGear: " + gear);
    sb.append("\nCadence: " + cadence);
    sb.append("\nSpeed: " + speed);
    sb.append("\nID: " + id + "\n");

    return (new String(sb));
} // end toString method

} // end class


主要测试车

import java.util.Scanner;
import javax.swing.JOptionPane;

class TestBike
{
public static void main(String[] args)
{
    String message ="";

    Bike[] bikeRace = new Bike[10];

    bikeRace[0] = new Bike("Cersei Lannister", 0, 0, 0);
    bikeRace[1] = new Bike("Daenerys Targaryen", 10, 60, 22);
    bikeRace[2] = new Bike("Robb Stark", 21, 50, 15);

    //testing values
    //bikeRace[3] = new Bike("Melisandre", 31,90,155);
    //bikeRace[4] = new Bike("Margaery Tyrell", 2, 3, 100);

    bikeRace[0].setGear(3);

    for(int i=0; i < Bike.getNumberOfBikes(); i++)
    {
        System.out.println(bikeRace[i].toString());

        //testing, must use a proper function call!
        //System.out.println(bikeRace[i].getDriver());
    } // end for loop

    //find max speed & driver findTheFastestDriver

    //testing stuffs get all drivers
    /*for(int i=0; i < Bike.getNumberOfBikes(); i++)
    {
        System.out.println("Driver: " + bikeRace[i].getDriver());
    }

    //testing get all speeds
    for(int i=0; i <Bike.getNumberOfBikes(); i++)
    {
        System.out.println("Speed: " + bikeRace[i].getSpeed());
    } */

    //****************************************************************/
    //finding fastest bike without a method
    //fastest speed is 22
    /*int fastestBike = bikeRace[0].getSpeed();
    int fastestBikeIndex = 0;
    for(int i=0; i<Bike.getNumberOfBikes(); i++)
    {
        if (bikeRace[i].getSpeed() >= fastestBike)
        {
            fastestBike = bikeRace[i].getSpeed();
            fastestBikeIndex = i;
        }//end if
    }//end for
    System.out.println("fastest Bike Speed: " + fastestBike + " " + " Bike index of " + fastestBikeIndex);*/
    //********************************************************************


    int fastestBikeIndex = findTheFastestDriver(bikeRace);
    System.out.println("\nThe fastest driver is " + bikeRace[fastestBikeIndex].getDriver() + " and the speed of " + bikeRace[fastestBikeIndex].getSpeed() + " mph.");


    System.out.println("\nNumber of Bikes: " + Bike.getNumberOfBikes() + "\n");

} //end of main

public static int findTheFastestDriver(Bike[] bikeRace)
{
    int fastestBike = bikeRace[0].getSpeed();
    int fastestBikeIndex = 0;
    for(int i=0; i < Bike.getNumberOfBikes(); i++)
    {
        if (bikeRace[i].getSpeed() >= fastestBike)
        {
            fastestBike = bikeRace[i].getSpeed();
            fastestBikeIndex = i;
        }//end if
    }//end for
    return fastestBikeIndex;
}

} //end of class

代码工作:)很高兴。

1 个答案:

答案 0 :(得分:3)

if (bikeRace[i] > bikeRace[0])

更改为

if (bikeRace[i].getSpeed() > bikeRace[0].getSpeed())

推理:你无法在数字上比较非数字对象。