我正在编写一个程序来计算通过汽车为某人节省的金额。我有两个类,第一个名为Carsavings2编译好,但第二个类Crsavings3似乎不断出现一个错误,说我找不到符号就在我试图实例化另一个类的行上。我是java新手,所以它可能总结愚蠢,但我无法弄明白。任何帮助都会很棒,代码如何查找两个cladsses
import java.util.Scanner;
import java.lang.Math;
public class Carsavings3 {
public static void main(String[] args) {
double equalAmounts, l, amount, numberOfYears, carCost;
Carsavings2 Carsavings2Object = new Carsavings2(equalAmounts, l, amount, numberOfYears, carCost);
Scanner read = new Scanner(System.in);
do {
//This will print out a request for the user
System.out.println("Please input the cost of the car, a real number 0 or greater\n");
//This will ensure the input value will remain between 0 and above
carCost = read.nextDouble(); } while (carCost <=1);
do {
//This will print out a request for the user
System.out.println("Please input number of years, an integer between 1 and 10\n");
//This will ensure the input value will remain between 1 and 10
numberOfYears = read.nextDouble();} while ((numberOfYears<1)||(numberOfYears>10));
}
}
/ 我得到的错误如下 /
C:\Users\User\Documents\imran>javac Carsavings3.java
Carsavings3.java:22: error: cannot find symbol
carCost = read.nextdouble(); } while (carCost <=1);
^
symbol: method nextdouble()
location: variable read of type Scanner
Carsavings3.java:28: error: cannot find symbol
numberOfYears = read.nextdouble();} while ((numberOfYears<1)||(n
umberOfYears>10));
^
symbol: method nextdouble()
location: variable read of type Scanner
Carsavings3.java:30: error: cannot find symbol
Carsavings2 Carsavings2Object = new Carsavings2(equalAmounts, l,
amount, numberOfYears, carCost);
^
symbol: variable equalAmounts
location: class Carsavings3
Carsavings3.java:30: error: cannot find symbol
Carsavings2 Carsavings2Object = new Carsavings2(equalAmounts, l,
amount, numberOfYears, carCost);
^
symbol: variable l
location: class Carsavings3
Carsavings3.java:30: error: cannot find symbol
Carsavings2 Carsavings2Object = new Carsavings2(equalAmounts, l,
amount, numberOfYears, carCost);
^
symbol: variable amount
location: class Carsavings3
Carsavings3.java:30: error: cannot find symbol
Carsavings2 Carsavings2Object = new Carsavings2(equalAmounts, l,
amount, numberOfYears, carCost);
^
symbol: variable numberOfYears
location: class Carsavings3
Carsavings3.java:30: error: cannot find symbol
Carsavings2 Carsavings2Object = new Carsavings2(equalAmounts, l,
amount, numberOfYears, carCost);
^
symbol: variable carCost
location: class Carsavings3
7 errors
以下类编译正常,没有错误
import java.util.Scanner;
import java.lang.Math;
public class Carsavings2 {
//variables
private double carCost;
private double numberOfYears;
//constructor
public Carsavings2(double equalAmounts,double l,double amount, numberOfYears,double carCost){
}
public void equalAmounts(double numberOfYears){
System.out.println("Equal Amount Method: \n\n");
int l;
double amount;
for(l=0;l<=numberOfYears;l++){
amount = (carCost/numberOfYears);
System.out.println("Year "+numberOfYears+" saved this year: " +amount+ "total saved: " +amount+ "remaining: " +carCost);
}
System.out.print("");}
} *任何帮助将不胜感激。我一直试图弄清楚将近4个小时,现在只是没有更近了
答案 0 :(得分:2)
read.nextdouble();
!= read.nextDouble();
CapiTiLiZatiOn很重要!
更重要的是,你需要修复你的编码风格。不要键入大量代码然后测试它。相反,如果您不能使用现代IDE(如NetBeans或Eclipse)(几乎可以立即警告您编译问题),那么由您来编译代码早期和经常< / strong>,并且在修复当前编译问题之前不添加任何新代码。否则,你最终会得到一个难以修复错误的老鼠窝。
答案 1 :(得分:1)
编辑
public class Carsavings3 {
//These are your class attributes.
private double equalAmounts;
private double l;
private double amount;
private double numberOfYears;
private double carCost;
public static void main(String[] args) {
//Here you are declaring a variable to use later in the method.
double equalAmounts, l, amount, numberOfYears, carCost;
// Then you assign values to those variables. You must always assign the variables you declare with some value before you can use them.
equalAmounts = 0;
l = 0;
amount = 0;
//numberOfYears, carCost variables are being initialized inside your while loops, so you need not initialize them here.
Scanner read = new Scanner(System.in);
do {
//This will print out a request for the user
System.out.println("Please input the cost of the car, a real number 0 or greater\n");
//This will ensure the input value will remain between 0 and above
carCost = read.nextDouble();
} while (carCost <= 1);
do {
//This will print out a request for the user
System.out.println("Please input number of years, an integer between 1 and 10\n");
//This will ensure the input value will remain between 1 and 10
numberOfYears = read.nextDouble();
} while ((numberOfYears < 1) || (numberOfYears > 10));
//This is where you create an instance of the class Carsavings3 using a parametrized constructor. You can use the variables declared and initialzed above here. But in order to use a parametrized constructor, you need to create such a constructor in your class.
Carsavings3 Carsavings2Object = new Carsavings3 (equalAmounts, l, amount, numberOfYears, carCost);
}
// This is how a parametrized constructor is written. A parametrized constructor is used to initialize the attributes of that class to the values passed as the parameters to this method.
private Carsavings3 (double equalAmounts, double l, double amount, double numberOfYears, double carCost) {
this.equalAmounts = equalAmounts;
this.l = l;
this.amount = amount;
this.numberOfYears = numberOfYears;
this.carCost = carCost;
}
}
答案 2 :(得分:0)
改变这个..
read.nextDouble();
和Carsavings2
内部创建像这样的构造函数
public Carsavings2(double equalAmounts,double l,double amount,double numberOfYears,double carCost){
}
声明所有字段并使用此构造函数启动..