【发布时间】:2018-03-03 15:53:38
【问题描述】:
我正在度假和度假司机。假期文件将作为我的蓝图,驱动程序将成为创建假期实例的程序的交互部分。我的一切工作正常,但是当我在假期驱动程序类的第 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
}
由于请求提供部分代码的 sn-p 在这里不起作用,这证明了我的问题:
//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));
注意:如果你在假期驱动程序的sn-p中取出numSales,程序会正确执行但没有正确的输出,因为它缺少必要的输出变量
一个简洁明了的问题是 - 为什么当我使用它时 numSales 不起作用,如简短的 sn-p 中所示。我的问题再次是 Eclipse 说“无法将 numSales 解析为变量。”
【问题讨论】:
-
请注意,如果你在假期驱动程序的第47行取出numSales,程序执行正确但没有正确的输出,因为它缺少必要的输出变量
-
TL;DR 试着提出一个清晰简洁的问题
-
代码很多,sn-p中没有显示行号。你能把代码压缩成最小的不工作的例子吗?这将有助于任何正在查看代码的人
-
上次我在这里发帖时,我的问题被删除了,因为我没有发布足够的代码,而且我的问题太含糊,现在太多了。我被骂是因为我要求人们做我的工作,因为我没有足够的代码,所以我实际上提供了它来向你展示我已经做了一些事情。如果它太长,只需阅读底部。我为你编辑了问题。
-
好吧,如果您提到行号,那么至少在代码中添加一个注释,显示哪一行是 42 或 47。如果您希望用户计算行数或将代码复制并粘贴到一个编辑器,那么你的期望太高了。
标签: java variables methods scope