日期:2014-05-20  浏览次数:20792 次

java新手求解
1:设计一个赛车类,RaceCar 包含属性:赛车编号carNO和车手名字driverName,另外在赛车类中声明一个静态变量count用来记录赛车的总数,初始化为0;在RaceCar的构造方法中除了给属性赋值外还应递增count的值,编写一个static int getCount()获取count的值
2:在main方法中创建若干个RacecCar的对象,保存在一个RaceCar类型的数组中,依次输出赛车的信息和赛车的总数


------解决方案--------------------
import java.util.ArrayList;
import java.util.List;

public class RaceCar {

private int carNO;
private String driverName;

public static int count;

public RaceCar(int carNo, String driverName) {
this.carNO = carNo;
this.driverName = driverName;
count++;
}

public int getCarNO() {
return carNO;
}

public void setCarNO(int carNO) {
this.carNO = carNO;
}

public String getDriverName() {
return driverName;
}

public void setDriverName(String driverName) {
this.driverName = driverName;
}

public static void main(String[] args) {

List<RaceCar> carList = new ArrayList<RaceCar>();
// 添加5RaceCar对象
for (int i = 0; i < 5; i++) {
RaceCar car = new RaceCar(i, "driver" + i);
carList.add(car);
}

System.out.println("赛车的总数为:" + RaceCar.count);
for (RaceCar raceCar : carList) {
System.out.println("赛车编号" + raceCar.getCarNO() + ",车手名字:"
+ raceCar.getDriverName());
}

}

}


这里网速有点卡,见谅不能格式化代码
------解决方案--------------------
for example
Java code
class RaceCar {
    private static int count = 0;
    private int carNo;
    private String driverName;
    public RaceCar(int carNo, String driverName) {
        this.carNo = carNo;
        this.driverName = driverName;
        count++;
    }
    public String toString() {
        return String.format("carNo:%d, driverName:%s", carNo, driverName);
    }
    public static int getCount() {return count;}

    public static void main(String[] args) {
        RaceCar[] rc = new RaceCar((int)(Math.random()*100 + 1));
        for (int i=0; i<rc.length; i++) {
            rc[i] = new RaceCar(i+1, "driver-"+(i+1));
        }
        for (RaceCar c : rc) {
            System.out.printf("赛车信息:%s\n", c.toString());
        }
        System.out.printf("赛车总数:%d\n", getCount());
    }
}

------解决方案--------------------
新人来看 来学习的 不回答
------解决方案--------------------
public class RaceCar {
private int carNo;
private String driverName;
private static int count=0;
public static int getCount(){
return count++;
}
public RaceCar(int carNo, String driverName,int count) {
super();
this.carNo = carNo;
this.driverName = driverName;
this.count = this.getCount();
}
@Override
public String toString() {
return "赛车编号:"+this.carNo+",车手名:"+this.driverName+",当前车数"+this.count;
}
public static void main(String[] args) {
RaceCar r1 = new RaceCar(132, "zhangsan",RaceCar.getCount());
RaceCar r2 = new RaceCar(456, "zhangsan",RaceCar.getCount());
RaceCar r3 = new RaceCar(457, "zhangsan",RaceCar.getCount());
RaceCar [] r ={r1,r2,r3};
for(int i=0;i<r.length;i++){
System.out.println(r[i]);
}
}
}
------解决方案--------------------
public class RaceCar{
private Integer carNo;
private String driverName;
private static int count=0;
public RaceCar(Integer carNo,String driverName){
this.carNo=carNo;
this.driverName=driverName;
count++;