为什么工资中的值没有从Salary方法解析为Commission方法?
package salesrepresentativeapp;
import java.util.Scanner;
/**
*
* @author Kirin
*/
public class SalesRepresentativeAPP {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String ID;
String name;
int yearServiced;
double salesVolume;
//System.out.println("Please enter your ID : ");
// ID = sc.nextLine();
// System.out.println("Please enter your Name : ");
// name = sc.nextLine();
System.out.println("Please enter your Year of Service : ");
yearServiced = sc.nextInt();
System.out.println("Please enter your sales volume : ");
salesVolume = sc.nextDouble();
SalesRepresentative s1 = new SalesRepresentative("id", "name", yearServiced, salesVolume);
//System.out.println("ID : " + s1.getID() + "\nName : " + s1.getName());
System.out.println("Your total salary is : " + s1.Commision() + "\nBasic Salary is : " + s1.Salary());
}
}
package salesrepresentativeapp;
/**
*
* @author Kiin
* v1.0
*/
public class SalesRepresentative {
private String ID;
private String name;
private int yearServiced,salary;
private double salesVolume,commision;
public SalesRepresentative(String ID, String name, int yearServiced, double salesVolume){
this.ID = ID;
this.name = name;
this.yearServiced = yearServiced;
this.salesVolume = salesVolume;
}
public int Salary(){
if (yearServiced >=1 && yearServiced <= 5) {
salary = 1200;
}
else if (yearServiced >=6 && yearServiced <= 10 ) {
salary = 1800;
}
else if (yearServiced > 10) {
salary = 2300;
}
return salary;
}
public double Commision(){
if (salesVolume >= 1 && salesVolume <= 99.99) {
commision = salary + (salary * 0.05);
}
else if (salesVolume >= 100.00 && salesVolume <= 299.99 ) {
commision = salary + (salary * 0.10);
}
else if (salesVolume >= 300.00) {
commision = salary + (salary * 0.15);
}
return commision;
}
public String getID(){
return ID;
}
public String getName(){
return name;
}
}
当我没有将薪水放在等式中时,该方法运作良好。但是当我把薪水放在等式中时,方法返回值为0.0
答案 0 :(得分:0)
首先调用Commision方法,在调用Salaray后编辑该值。 改为将System.out改为此。
System.out.println("Basic Salary is : " + s1.Salary() + "\nYour total salary is : " + s1.Commision());
除了该方法之外,名称应以小写字符开头。
编辑:我希望我能正确理解你的问题。
答案 1 :(得分:0)
查看您的代码,您应该进行相当多的更改以改进查看已编辑的副本,我将解释
public class SalesRepresentative {
private String ID;
private String name;
private int yearServiced,salary;
private double salesVolume,commision;
public SalesRepresentative(String ID, String name, int yearServiced, double salesVolume){
this.ID = ID;
this.name = name;
this.yearServiced = yearServiced;
this.salesVolume = salesVolume;
salary();
commision();
}
private void salary(){
if (yearServiced >=1 && yearServiced <= 5) {
salary = 1200;
}
else if (yearServiced >=6 && yearServiced <= 10 ) {
salary = 1800;
}
else if (yearServiced > 10) {
salary = 2300;
}
}
private void commision(){
if (salesVolume >= 1 && salesVolume <= 99.99) {
commision = salary + (salary * 0.05);
}
else if (salesVolume >= 100.00 && salesVolume <= 299.99 ) {
commision = salary + (salary * 0.10);
}
else if (salesVolume >= 300.00) {
commision = salary + (salary * 0.15);
}
}
public double getCommision()
{
return commision;
}
public int getSalary()
{
return salary;
}
public String getID(){
return ID;
}
public String getName(){
return name;
}
}
按照惯例,您的方法名称应该是驼峰式的。我已将其范围从public
更改为private
(意味着只能在此类中调用)并在构造函数中调用它们。您调用这些方法的顺序非常重要,因为在计算工资之前无法计算佣金。
此外,由于方法正在更新类中的值,我已将其返回类型更改为void
。我创建了两个getter方法getSalary()
&amp; getCommission()
- 您的应用程序将调用这些值来返回值。请参阅以下实施:
public class SalesRepresentativeAPP {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String ID;
String name;
int yearServiced;
double salesVolume;
System.out.println("Please enter your Year of Service : ");
yearServiced = sc.nextInt();
System.out.println("Please enter your sales volume : ");
salesVolume = sc.nextDouble();
SalesRepresentative s1 = new SalesRepresentative("id", "name", yearServiced, salesVolume);
System.out.println("Your total salary is : " + s1.getCommision() + "\nBasic Salary is : " + s1.getSalary());
}
}