저는 Java에 익숙하지 않아 현재 학기 (CS 전공)의 최종 프로젝트를 진행하고 있으며 "변수가 초기화되지 않았을 수 있습니다"오류가 발생합니다. 사이트에서 다른 픽스를 시도했지만, 할 때마다 내가 만든 루프에서 정의 된 것보다는 초기화 된 변수를 사용합니다.초기화 코드 유실
public static void main(String[] args) {
double priceperpound = 2; // price per pound of coffee
int numberofbags = 2; // number of pounds of coffee
double bagweightinpounds = 1; // weight of the bag in punds
// double pricebeforetaxes = 8; // total before taxes
// double totalpricewithtaxes = 0; // total price
double taxrate = .065; // whats the tax?
double TotalWeight = 0; // total weight of purchase
// double discountprice = 0; // discounted price
// double discount = .9; // if you qualify the price before taxes will be multiplied by this
int row;
int col;
PriceCalculator calculations = new PriceCalculator(priceperpound, numberofbags,
bagweightinpounds, taxrate);
for (bagweightinpounds = 1; bagweightinpounds <= 5; bagweightinpounds = bagweightinpounds + 1) {
for (numberofbags = 2; numberofbags <= 1024; numberofbags = numberofbags * 2)
{
System.out.printf("%f", calculations.getBasePrice());
}
}
Scanner input = new Scanner(System.in);
}
자바가 C 또는 파스칼이 아닙니다. 메소드의 시작 부분에서 변수를 모두 선언하는 대신 필요할 때만 변수를 선언하고 동시에 초기화하십시오. 그리고 가능한 한 짧은 방법을 만들어보십시오. –
이 전체 코드입니까? 왜냐하면 결국 a가 없어 졌기 때문입니다. – fonZ
중단 점을 넣고 오류가 정확히 어디서 발생하는지 확인할 수 있습니까? Java 컴파일러 보증인이 0.0, 0.0d 또는 0.0D로 선언되지 않은 'double'을 컴파일할지 여부는 확실하지 않습니다. – bonCodigo