내 클래스 자동차 내에서 getYear, getMake 및 getModel 메서드에 대해이 오류 메시지가 표시됩니다. 분명히 인수가 전달되지 않았기 때문입니다. 그들이 주장을 전달받는 것처럼 보이지만 아직 Java 초보자이므로 어디에서 엉망인지 모르겠습니다.
public class NextCar {
public static final void main(String args[]) {
//Creates objects from Car class
Car c = new Car ();
Car c1 = new Car ();
Car c2 = new Car ();
Car c3 = new Car ();
//First object
//Prints mileage
c.start();
c.moveForward(6);
c.moveBackward(2);
c.moveForward(4);
System.out.println ("The car went " + c.mileage() + " miles.");
//Second object
//Prints year of car
c1.getYear(2050);
System.out.println("The year of the car is " + c1.getYear());
//Third object
//Prints year and make of car
c2.getYear(2055);
c2.getMake("Google");
System.out.println("The year of the car is " + c2.getYear() + " and the make is " + c2.getMake());
//Fourth object
//Prints year, make, and model of car
c3.getYear(2060);
c3.getMake("Google");
c3.getModel("Smart");
System.out.println("The year of the car is " + c3.getYear() + " and the make is " +
c3.getMake() + " and the model is " + c3.getModel());
}
}
//creates Car class
class Car {
public int year = 0;
public String make = "";
public String model = "";
public int miles = 0;
public boolean power = false;
public void start() {
power = true;
}
public void moveForward(int mf) {
if (power == true) {
miles += mf;
}
}
public void moveBackward(int mb) {
if (power == true) {
miles -= mb;
}
}
public int mileage() {
return miles;
}
public int getYear(int y) {
year = y;
return year;
}
public String getMake(String ma) {
make = ma;
return make;
}
public String getModel(String mo) {
model = mo;
return mo;
}
}
귀하의 Car
클래스 getYear
메소드는 정수 입력을 받아 :
public int getYear(int y)
그러나 입력을 제공하지 않고 몇 번 호출
System.out.println("The year of the car is " + c1.getYear());
System.out.println("The year of the car is " + c2.getYear() + " and the make is " + c2.getMake());
System.out.println("The year of the car is " + c3.getYear() + " and the make is " +
그것이 당신의 오류의 이유입니다.
getYear
(연도 값을 얻기 위해)와 setYear
(연도 값을 설정 하기 위해) 두 가지 방법을 원 하지만 하나만 정의했습니다. 아마도 이것이 필요한 것입니다.
public void setYear(int y) {
year = y;
}
public int getYear() {
return year;
}
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다