홈>
나는 휴가 및 휴가 운전사를 만들고 있습니다. 휴가 파일은 내 청사진 역할을하며 드라이버는 휴가 인스턴스를 만드는 프로그램의 대화식 부분이됩니다. 나는 모든 것이 완벽하게 작동하지만 한 가지 가치를 더할 때 휴가 운전자 클래스의 47 줄에 인쇄 명세서에 빠져 있습니다.
42 행에 선언 된 것으로 생각되는 numSales에 대한 값을 호출해야합니다. 그림과 같이 출력의 시작과 중간에 47 행에 numSales를 입력하면 아래에 빨간색 선이 표시되고 Eclipse가 알려줍니다."numSales를 변수로 해석 할 수 없습니다". 휴가 운전자의 47 행에있는 인쇄 명세서에 numSales 값을 적극적으로 출력하려면 어떻게해야합니까?
휴가 강의는 다음과 같습니다.
package cbrownmod4;
import java.text.NumberFormat;
public class Vacation {
// money formatting
NumberFormat money = NumberFormat.getCurrencyInstance();
// instance variables
private String vacationName;
private int numSold;
private Double priceEach;
// empty constructor
public Vacation() {
}
// partial constructor
public Vacation(String n, int s, double e) {
vacationName = n;
numSold = s;
priceEach = e = 0;
}
// updatSales method
public int updateSales() {
int updateSales = 0;
updateSales = updateSales + numSold;
return updateSales;
}
// totalValue method
public double totalValue() {
double totalValue = 0;
totalValue = totalValue + (numSold * priceEach);
return totalValue;
}
// toString method
public String toString() {
return vacationName + " has been sold " + numSold + " times for " + money.format(priceEach) +
" each for a total value of " + money.format(numSold*priceEach);
}
// getters and setters
public String getVacationName() {
return vacationName;
}
public void setVacationName(String vacationName) {
this.vacationName = vacationName;
}
public int getNumSold() {
return numSold;
}
public void setNumSold(int numSold) {
this.numSold = numSold;
}
public Double getPriceEach() {
return priceEach;
}
public void setPriceEach(Double priceEach) {
this.priceEach = priceEach;
}
}
휴가 드라이버는 다음과 같습니다.
package cbrownmod4;
import java.text.NumberFormat;
import java.util.Scanner;
public class VacationDriver {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
NumberFormat money = NumberFormat.getCurrencyInstance();
// ask for the number of vacations and read it into numVacations
System.out.println("How many vacations are there?:");
int numVacations = input.nextInt();
// ask for the number of sales people and read it into numPeople
System.out.println("How many sales people are there?: ");
int numPeople = input.nextInt();
// beginning of loop for number of vacations
for(int i = 0; i < numVacations; i++) {
//ask for the name and price and read these into variables
System.out.println("What is the name of vacation #" + Integer.toString(i+1) + "?:");
String name = input.next();
System.out.println("How much does " + name + " cost?: ");
Double price = input.nextDouble();
// create a Vacation instance using the data obtained above
Vacation vacay = new Vacation (name, i, price);
// loop through the sales people
for(int j = 0; j < numPeople; j++) {
// ask for the sales for the current vacation and read it in
System.out.println("What are the sales for the current vacation by person #" + Integer.toString(j+1) + "?:");
int numSales = input.nextInt(); // line 42
// call updateSales with this number
numSales = vacay.updateSales();
} //end of inner loop
//where line 47 begins
//print out this vacation info
System.out.println(numSales + " trips to " + name + " were sold for " + money.format(price) + " for a total of " + money.format(numSales * price));
} //end of outer for loop
} //end of main
}
여기서 작동하지 않는 코드 부분의 스 니펫을 제공하라는 요청으로 인해 문제가 발생했습니다.
//print out this vacation info
System.out.println(numSales + " trips to " + name + " were
sold for " + money.format(price) + " for a total of " +
money.format(numSales * price));
참고 : 휴가 드라이버 스 니펫에서 numSales를 꺼내면 프로그램이 올바르게 실행되지만 필요한 출력 변수가 없기 때문에 올바른 출력이 없습니다
간단한 질문은 짧은 스 니펫에 표시된 것처럼 numSales를 사용할 때 왜 작동하지 않는 것입니다. 다시 말하지만 이클립스는 "numSales를 변수로 해석 할 수 없습니다"라고 말합니다.
- 답변 # 1
관련 자료
- c - if 조건부 내부의 for 루프 내부에 정의 된 변수의 범위를 변경하여 외부에서 사용하는 방법은 무엇입니까?
- xmonad - 범위에없는 Haskell 변수
- python - 변수에 쓰기가 범위를 변경하는 이유는 무엇입니까?
- bash - 읽기 전용 변수 범위를 함수로 제한하는 방법은 무엇입니까?
- angularjs - Angular 1x에서 스코프 함수 외부 변수에 액세스
- C에서 포인터없이 로컬 범위 변수를 변경하는 방법은 무엇입니까?
- python - 가져 오기 후 전역 범위의 변수를 클래스에 제공
- SSIS - ssis - 모듈 식 "자식"패키지 — 변수 범위 및 패키지로 변수 전달
문제는 당신이
numSales
를 선언한다는 것입니다 for {} 블록 안에for 블록 전에 선언해야합니다 :
42 번째 줄에서 설정 한 값은 절대 사용되지 않으며 45 번째 줄에서 덮어 쓰므로
input.nextInt();
를 호출해도됩니다 값을numSales
로 설정하지 않고 .