Так что у меня проблемы с моей программой здесь. Я должен написать программу, которая требует методов ускорения/торможения в автомобиле. Я получаю синтаксическую ошибку в первой половине кода, но вторая половина отмечена как правильная.Ошибка синтаксиса на токенах (-ах), неправильная конструкция (-ы) в Eclipse
Car
класс:
public class Car {
private int yearModel;
private String make;
private int speed;
public Car(String m, int year) {
yearModel = year;
make = m;
speed = 0;
}
// Declare mutators (methods).
// Declare accessors (methods).
public int getModel() { // Model year of car.
return yearModel;
}
public String getMake() {
return make;
}
public int getSpeed() {
return speed;
}
public void setModel(int year) {
yearModel = year;
}
public void setMake(String carMake) {
make = carMake;
}
public void setSpeed(int s) { // Incorrect??? Possible outSpeed = speed;
speed = s; // Not sure if correct; should be "0" | or equal to "carSpeed"
}
public void accelerateSpeed() {
speed += 5;
// Each call will increase by 5.
}
public void brakeSpeed() {
speed -= 5;
// Each call will decrease by 5.
}
}
CarResults
класс:
import javax.swing.JOptionPane;
class CarResults {
public static void main(String[] args) {
String input, carMake;
int model, yearModel, year, s;
Car myCar = new Car("Car", 2011);
// Retrieve car's Make & Model.
carMake = JOptionPane.showInputDialog("What is the Make of your car? ");
myCar.setMake(carMake);
year = Integer.parseInt(JOptionPane.showInputDialog("What is the Model Year of your car? "));
myCar.setModel(year);
input = JOptionPane.showInputDialog("Enter your car's speed: ");
s = Integer.parseInt(input);
myCar.setSpeed(s);
for (int i = 0; i < 5; i++) {
myCar.accelerateSpeed();
System.out.println();
System.out.println("The " + " " + myCar.getModel() + " " + myCar.getMake() +
" is gradually accelerating. ");
// Apply acceleration.
System.out.println("Your current speed is: " + myCar.getSpeed());
}
// Begin applying brakes.
System.out.println();
System.out.println("\t>>> Now, let's get the results for applying the brakes... ");
System.out.println();
for (int i = 0; i < 5; i++) {
myCar.brakeSpeed();
System.out.println();
System.out.println("[Braking] Your" + " " + myCar.getModel() + " " + myCar.getMake() + " is now traveling at: ");
// Apply brakes.
System.out.println("Now your speed is: " + myCar.getSpeed());
}
// End the program.
System.exit(0);
}
}
Не могли бы вы быть немного более конкретно о том, что ошибка, которую вы получаете? Часто сообщение об ошибке сообщает вам точно, что не так :) –