我真的在努力使用 get/set 方法。我了解基本概念-首先设置值然后检索它。我发现用我已经学过的最小概念很难找到有关它的信息。我在第 6 章或我的第一个 Java 和编程课上,它都是在线的。我创建了其他几个使用 set/get 方法的类,但它们似乎并不适合这个项目。
public class Purchase{
int inv;
double sale;
double tax;
double taxAmount = 0.05;
public int getInv()
{
return inv;
}
public void setInv(int inv)
{
inv = inv;
}
public void setSale(double s)
{
sale = s;
tax = sale * taxAmount;
}
public double getSale()
{
return sale;
}
//display
public void display()
{
System.out.println("Invoice number: " + getInv() + "\nSale amount: $" + getSale() + "\nTax: $" + tax + "\nTotal: $" + (tax + sale));
}
}
import java.util.Scanner;
public class CreatePurchase{
public static void main(String[] args)
{
Purchase one = new Purchase();
Scanner input = new Scanner(System.in);
do
{
System.out.print("Please enter you invoice number. This will be a number between 1000 and 8000. ");
inv = input.nextInt();
one.setInv(inv);
}
while(inv < 1000 && inv > 8000);
{
System.out.println("Please enter the amount of your sale. ");
sale = input.nextInt();
}
}
}
CreatePurchase 类尚未完成,但我对其进行编译并在每次出现时为每个变量获取以下内容:
CreatePurchase.java:16: error: cannot find symbol
inv = input.nextInt();
^
我的理解是创建了一个默认构造函数,所以我没有添加一个,而是在 CreatePurchase 中调用它。有什么建议么?