我對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
嘗試把一個封裝類身邊。然後他們可以分享。 –