[英]Java Programming Help for 2 files with a controlling class
First java class is the primary one which invokes other classes.第一个 java 类是调用其他类的主要类。 I will copy the code for the two files.
我将复制这两个文件的代码。 I cannot compile this code and I do not know why.
我无法编译这段代码,我不知道为什么。
When I had the program in one file it ran fine, but the instructor wants it in two files.当我将程序放在一个文件中时,它运行良好,但讲师希望将它放在两个文件中。 Can someone explain to me what I am doing, or did incorrectly?
有人可以向我解释我在做什么,还是做错了?
File 1:文件 1:
public class Wk2ToddFoughty {
public static void main(String[] args) {
SalaryCalc FinalSalaryCalc = new SalaryCalc();
FinalSalaryCalc.SalaryCalc();
}
}
File 2:文件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 );
}
It might be easier to understand if you kept to Java naming conventions - also see inline comments如果您遵守 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 );
}
}
Your second file doesn't define a method called SalaryCalc
.您的第二个文件没有定义名为
SalaryCalc
的方法。 You should also honor standard Java naming practices, and keep input/output logic outside of your business classes.您还应该遵守标准的 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;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.