-4

当谈到我缺乏 Java 技能时,我仍然在 HelloWorld。我无法理解这一点来挽救我的生命。我正在编写一个覆盖方法?在我开始尝试完成其余部分之前,我刚刚开始使用我学校提供的代码,并且我已经遇到了我什至无法开始考虑如何纠正的错误。任何帮助都将不胜感激。我只是想让这个错误消失,这样我就可以创建一些新的:) 下面是代码:

public class Vehicle
public static void main (String [] args)}
{
private boolean moving; // whether or not the vehicle
private double speed; 
private char bearing; 
('N','E','S', or 'W')
public Vehicle(){ // Vehicle class no-arg constructor
    moving = false; // assume not moving
    speed = 0.0; // not moving
    bearing = 'N'; // assume 'N'orth
    System.out.println("Created a vehicle (no-arg)"); 
}
public Vehicle (double initialSpeed) // Vehicle 1-arg constructor
    bearing = 'W';
    speed = initialSpeed;
    if (speed > 0.0)
    {
        moving = true;
    }
    System.out.println("Created a vehicle (1-arg)"); 
public Vehicle (double initialSpeed, char initialBearing) // Vehicle 2-arg constructor
    bearing = initialBearing;
    speed = initialSpeed;
    if (speed > 0.0){
        moving = true;
    }
    System.out.println("Created a vehicle (2-arg)");
public void start(double initialSpeed, char initialBearing){
    moving = true;
    if (initialSpeed >= 5.0 && initialSpeed <= 20.0){
        speed = initialSpeed; // valid expected range
    } else if (initialSpeed >= 0.0 && initialSpeed < 5.0){
        speed = 5.0; // minimum
    } else if (initialSpeed < 0.0){
        speed = 0.0; // assume no movement
        moving = false;
    } else if (initialSpeed > 20.0){
        speed = 20.0; // maximum allowed
    }
    switch(initialBearing){
    case 'N':
        bearing = initialBearing;
        break;
    case 'E':
        bearing = initialBearing;
        break;
    case 'S':
        bearing = initialBearing;
        break;
    case 'W':
        bearing = initialBearing;
    default:
System.out.println("invalid bearing " + 
     initialBearing +
     " set to N"); // additional user notification
     bearing = 'N';
}
public double getSpeed() { // get and return current speed in mph
    return speed;
}
public void setSpeed(double newSpeed){ // set new speed in mph
     speed = newSpeed;
}
    /**
     *
     * @return
     */
public char getBearing(){
    return bearing;
}
public void speedUp(double mphSteps, int numSteps){
    int counter = 0;
    while (counter < numSteps)
    speed += mphSteps;
    System.out.println("counter= " + counter + ", " + 
    this.toString());
    counter++;
}
public String toString(){
    return "From toString(): speed= " + getSpeed() +
    " mph and bearing= " + getBearing();
} 
}
public class Car extends Vehicle{
    private String color;
    private int doors; 
    private double hp; 
    public Car(String carColor, int numDoors, 
    double horsePower, double
    startingSpeed) 
{
super(startingSpeed);
color = carColor;
doors = numDoors; 
hp = horsePower; 
System.out.println("Created a car"); 
}
public String getColor()
{
return color;
}
public int getDoors()
{
return doors;
}
public double getHp()
{
return hp;
}
public String toString()
{
return "From Car toString(): color= " + getColor() +
" doors= " + getDoors() + 
" hp= " + getHp() +
" speed= " + getSpeed() +
" mph and bearing= " + getBearing();} 
}
public class TestCar2
{
public static void main(String[] args)
{ 
Car myCar2 = new Car("blue", 4, 300., 10.0); 
System.out.println(myCar2.toString());
myCar2.speedUp(5.0, 2); 
}
}

请拜托,谢谢你的帮助!

4

2 回答 2

1
public class Vehicle

public static void main (String [] args)} // this is totally wrong
     // This not compile at least 
{

实际上parenthesis,您使用的 ({}) 是完全错误的。以下结构应该跟随你。我猜您没有使用 IDE 进行编码。我建议你使用IDE来做代码。

public class MyClass{

 public static void main(String[] args){
   // main method
 }
 // some other method

}
于 2013-08-16T06:31:56.687 回答
0

首先,你不能在一个文件中有两个公共类!!

其次,你的大括号完全不匹配。

public class Vehicle
public static void main (String [] args)}
{

类有它自己的范围,而 main() 函数有它自己的。因此,将您的代码更改为

public class Vehicle {
public static void main (String [] args) { //your code}
}

您的多个 arg 构造函数没有正确的括号。

public Vehicle (double initialSpeed) // Vehicle 1-arg constructor
    bearing = 'W';
    speed = initialSpeed;
    if (speed > 0.0)
    {
        moving = true;
    }
    System.out.println("Created a vehicle (1-arg)"); 

将其更改为

public Vehicle (double initialSpeed) { // Vehicle 1-arg constructor
    bearing = 'W';
    speed = initialSpeed;
    if (speed > 0.0)
    {
        moving = true;
    }
    System.out.println("Created a vehicle (1-arg)"); 
}

最后,您需要在所有情况下(默认除外)更改您的 switch 语句

case 'W':
bearing = initialBearing;

case 'W':
bearing = initialBearing;
break;

如果您是 Java 新手并学习基础知识,我建议您使用 Eclipse、Netbeans 或 Intellij IDEA 之类的 IDE。谷歌他们找到更多信息。

于 2013-08-16T07:02:25.777 回答