【问题标题】:Variable Scope and Visibility in JavaJava 中的变量范围和可见性
【发布时间】: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


【解决方案1】:

问题是您在 for {} 块中声明了 numSales

你需要在for块之前声明它:

    int numSales = 0;  // set initial value in case numPeople is 0 and the loop never runs

    // 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) + "?:");
        numSales = input.nextInt();  // line 42; this value is never used?  it is overwritten below

        // overwrite the never-used value from line 42?? 
        numSales = vacay.updateSales(); 

    } //end of inner loop

    // now numSales is still visible, because it was declared on this same 'level' and not in an inner block
    System.out.println("numSales: " + numSales);

第 42 行中设置的值从未使用过,并在第 45 行中被覆盖,因此您不妨调用input.nextInt(); 而不将值设置为numSales

【讨论】:

  • 我尝试在如图所示的 for 循环之前声明,但我仍然从 eclipse 中得到相同的错误。如果我初始化为 0,我会摆脱错误,但我的输出中仍然是 0。
  • 我怀疑您现在遇到了另一个错误。这段代码应该可以工作(虽然我没有测试它,在浏览器中输入)。
  • 我做了一些更新来澄清。请查看当前版本的答案。
猜你喜欢
  • 2014-04-05
  • 2016-08-24
  • 2017-10-07
  • 2018-11-14
  • 1970-01-01
  • 1970-01-01
  • 2015-07-06
  • 2022-10-24
  • 1970-01-01
相关资源
最近更新 更多