의 값을 변경하려고 시도하지만 출력이 변경되지 않습니다. NetBeans에서이 두 프로그램을 동일한 프로젝트 및 패키지에 저장했습니다. 코드를 너무 길게 만들었 기 때문에 나는 여기에 포함시키지 않았지만 잘 작동하는 접근 자 메서드를 작성했기 때문에 왜 뮤 테이터 메서드가 작동하지 않는지 혼란 스럽습니다.뮤 테이터 메서드가 작동하지 않습니다. NetBeans
클래스 코드 :
package auto;
public class Auto
{
private String model;
private int milesDriven;
private double gallonsOfGas;
public Auto(String startModel,
int startMilesDriven,
double startGallonsOfGas)
{
model = startModel;
setMilesDriven(startMilesDriven);
setGallonsOfGas(startGallonsOfGas);
}
public void setModel(String newModel)
{
model = newModel;
}
public void setMilesDriven(int newMilesDriven)
{
if (newMilesDriven >= 0)
milesDriven = newMilesDriven;
else
{
System.err.println("Miles driven cannot be negative.");
System.err.println("Value not changed.");
}
}
public void setGallonsOfGas(double newGallonsOfGas)
{
if (newGallonsOfGas >= 0.0)
gallonsOfGas = newGallonsOfGas;
else
{
System.err.println("Gallons of gas cannot be negative.");
System.err.println("Value not changed.");
}
}
}
클라이언트 클래스 코드 :
package auto;
import java.text.DecimalFormat;
public class AutoClient
{
public static void main(String [] args)
{
DecimalFormat milesPattern = new DecimalFormat("#,###");
Auto coupe = new Auto("Corvette", 300000, 0.0);
String coupeModel = coupe.getModel();
int coupeMiles = coupe.getMilesDriven();
double coupeGallons = coupe.getGallonsOfGas();
System.out.println("coupe:"
+ "\nmodel: " + coupeModel
+ "\nmiles: " + milesPattern.format(coupeMiles)
+ "\ngallons: " + coupeGallons);
coupe.setModel("Viper");
coupe.setMilesDriven(10000);
coupe.setGallonsOfGas(50.0);
System.out.println("coupe:"
+ "\nmodel: " + coupeModel
+ "\nmiles: " + milesPattern.format(coupeMiles)
+ "\ngallons: " + coupeGallons);
}
}
Netbeans이 문제와 관련이 있다고 생각하는 이유를 설명 할 수 있습니까? –
원래 파일을 저장 한 방법 (예 : 동일한 패키지에 있는지 여부)에 문제가 있다고 생각했습니다. 나는 IDE와 프로그래밍에 익숙하지 않다. – pez
충분합니다. 그러나 그게 문제 였다면 실행하는 프로그램보다는 컴파일 오류가 발생하지만 잘못된 대답을 줄 수 있습니다. –