Java 将值从一个方法传递到另一个方法
为什么薪资中的值没有从薪资方法解析为佣金方法Java 将值从一个方法传递到另一个方法,java,Java,为什么薪资中的值没有从薪资方法解析为佣金方法 package salesrepresentativeapp; import java.util.Scanner; /** * * @author Kirin */ public class SalesRepresentativeAPP { /** * @param args the command line arguments */ public static void main(String[] a
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());
}
}
当我没有把工资放在等式中时,这个方法很有效。但是当我把工资放在公式中时,方法返回值0.0,你首先调用佣金方法,然后在调用Salaray后编辑该值。 将您的系统更改为此
System.out.println("Basic Salary is : " + s1.Salary() + "\nYour total salary is : " + s1.Commision());
除此之外,方法名称应以小写字符开头
编辑:我希望我已经正确理解了您的问题。查看您的代码,您应该做很多更改以改进。请参阅编辑的副本,我将解释
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 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 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());
}
}