gpt4 book ai didi

java - Java 中的变量作用域和可见性

转载 作者:太空宇宙 更新时间:2023-11-04 10:40:06 25 4
gpt4 key购买 nike

我正在做一名假期和假期司机。假期文件将作为我的蓝图,驱动程序将是创建假期实例的程序的交互部分。我一切都工作得很好,但是当我添加一个值时,我在假期驱动程序类第 47 行的打印语句中缺少了一个值,我破坏了程序。

我需要调用 numSales 的值,我认为该值是在第 42 行中声明的。当我在第 47 行的开头和输出中间输入 numSales 时,如图所示,我在下面看到一条红线,并且 Eclipse 告诉我“numSales 无法解析为变量”。我需要做什么才能让 numSales 的值在假期驱动第 47 行的 print 语句中主动输出?

这是假期类(class):

    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 时它不起作用。我的问题又是 Eclipse 说“numSales 无法解析为变量。”

最佳答案

问题在于您在 for {} block 内声明了 numSales

您需要在 for block 之前声明它:

    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

关于java - Java 中的变量作用域和可见性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49086054/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com