我对Java很新。对于班级任务,我需要输入销售人员姓名和年销售额,然后根据他们的固定年薪5万美元显示其总工资。它必须包括2个班级。我的salesperson
类正常工作。该Annualwages
类运行,但显示“hello null”和“你的总年薪5万美元”(这只是没有增加销售额的固定年薪)。将信息从一个班级拉到另一个班级
我不知道如何在运行Annualwages
时从salesperson
提取信息。
package annualwages;
import java.util.Scanner;
/** @author Rachael */
class salesperson { //begins salesperson class
int annualSales;
String name;
public static void main(String[] args) {
int annualSales;
String name;
// Create a Scanner object to read input.
Scanner keyboard = new Scanner(System. in);
//Get the user's name.
System.out.print("What is your name? ");
name = keyboard.nextLine();
//Get the amount of annual Sales
System.out.print("How much in sales did you make in the last year? ");
annualSales = keyboard.nextInt();
}
} //ends salesperson class
package annualwages;
/**
*
* @author Laptop
*/
public class Annualwages {
/**
* @param args the command line arguments
*/
public static void main(String[] args) { //begins the main method
/**Declaration Statements */
final double COMMISSION = 0.15;
//Sets a fixed variable for commission earned
final double SALARY = 50000;
//Sets a fixed variable for Salary earned
final double SALESTARGET = 120000;
//Sets a fixed variable for the sales target
double totalSales, totalWages, actualCommission, accelFactor = 1.25;
/**
* initializes annual Sales, total Sales, Total Wages,
* actual commission and acceleration factor as a double.
* Sets the acceleration factor to increase by 1.25. */
salesperson sp = new salesperson();
//Sales incentive begins at a minimum at $96,000 in sales.
//if less than, then no commission is earned
if (sp.annualSales <= 96000) {
actualCommission = 0;
}
// Sales incentive with $96,000 or more earns 15% commission
else if ((sp.annualSales > 96000) && (sp.annualSales < SALESTARGET)) {
actualCommission = COMMISSION;
}
//Sales incentive increases if the sales person earns more than $120,000
//in sales with the acceleration factor of 1.25
else {
actualCommission = COMMISSION * accelFactor;
}
//Calculates total sales by multiplying sales and commission
totalSales = sp.annualSales * actualCommission;
//Calculates total wages by adding salary to total sales
totalWages = SALARY + totalSales;
//Display the resulting information.
System.out.println("Hello " + sp.name);
System.out.println("Your total annual compensation is $" + totalWages);
} // ends main method
} // ends annual wages class
尝试把一个封装类身边。然后他们可以分享。 –