第一个java类是调用其他类的主要类。我将复制这两个文件的代码。我无法编译此代码,我不知道为什么。
当我将程序放在一个文件中时它运行正常,但是教师想要它在两个文件中。有人可以向我解释我在做什么,或者做错了吗?
文件1:
public class Wk2ToddFoughty {
public static void main(String[] args) {
SalaryCalc FinalSalaryCalc = new SalaryCalc();
FinalSalaryCalc.SalaryCalc();
}
}
文件2:
import java.util.Scanner;
class SalaryCalc {
Scanner in = new Scanner(System.in);
System.out.println("Enter your annual sales: $");
double sales = in.nextDouble();
double salary = 35000.00;
double commission = (sales * .015);
double totalSalary = salary + sales;
System.out.println("Your Salary + Commission is: $" + totalSalary );
}
答案 0 :(得分:0)
如果您遵守Java命名约定,也可以更容易理解 - 也可以看到内联注释
public class Wk2ToddFoughty {
public static void main(String[] args) {
SalaryCalc myCalc = new SalaryCalc(); // SalaryCalc - name of class
// myCalc - name of object
myCalc.doCalc(); // doCalc - name of method (missing from your class)
}
}
// edit this class to add missing method
public class SalaryCalc {
public void doCalc () {
Scanner in = new Scanner(System.in);
System.out.println("Enter your annual sales: $");
double sales = in.nextDouble();
double salary = 35000.00;
double commission = (sales * .015);
double totalSalary = salary + sales;
System.out.println("Your Salary + Commission is: $" + totalSalary );
}
}
答案 1 :(得分:0)
您的第二个文件没有定义名为SalaryCalc
的方法。您还应该遵循标准的Java命名实践,并将输入/输出逻辑保留在业务类之外。
import java.util.Scanner;
public class Wk2ToddFoughty {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("Enter your annual sales: $");
SalaryCalc calc = new SalaryCalc(35000);
double totalSalary = calc.calcSalary(in.nextDouble());
System.out.println("Your Salary + Commission is: $" + totalSalary);
}
}
public class SalaryCalc {
private double salary;
public SalaryCalc(double salary) {
this.salary = salary;
}
public void calcSalary(double sales) {
double commission = sales * 0.015;
return salary + sales + commission;
}
}