2015-10-16 98 views
1

我對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 
+0

嘗試把一個封裝類身邊。然後他們可以分享。 –

回答

1

在你salesperson類,我會在你的main功能將所有的代碼,構造函數。就像我在下面做的一樣。這樣做可讓您設置非靜態字段,以便您可以正確設置nameannualSales

import java.util.Scanner; 

public class salesperson { 

    int annualSales; 
    String name; 

    public salesperson(){ 
     // 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(); 
    } 
} 

注意,我聲明除去從方法nameannualSales。 在此之後,您的Annualwages類中的其餘代碼應該正常工作。

1

兩個領域

int annualSales; 
String name; 

應取得 「公共靜態」。你也不應該在主要方法中再次定義它們。

最後它似乎並沒有使用IDE。下載用於在Java中編寫代碼的「Eclipse IDE」。它會讓你的生活變得更輕鬆

+0

此外,您的課程也存在一些編譯問題。請在運行代碼之前更正 – sparkDabbler

+0

根據課程說明,我們必須使用NetBeans。這就是我用來寫這段代碼的地方。感謝您的幫助。 – Rachael

1

你要做的那樣:

import java.util.Scanner; 
class salesperson { 
protected int annualSales; 
protected String name; 

public void saleInformation() { 
    Scanner keyboard = new Scanner(System.in); 
    System.out.print("What is your name? "); 
    name = keyboard.nextLine(); 
    System.out.print("How much in sales did you make in the last year? "); 
    annualSales = keyboard.nextInt(); 
} 
} 

public class Annualwages extends salesperson { 

public static void main(String[] args) { 
    salesperson s = new salesperson(); 
    s.saleInformation(); 

    final double COMMISSION = 0.15; 
    final double SALARY = 50000; 
    final double SALESTARGET = 120000; 
    double totalSales, totalWages, actualCommission, accelFactor = 1.25; 
    if (s.annualSales <= 96000) { 
     actualCommission = 0; 
    } else if ((s.annualSales > 96000) && (s.annualSales < SALESTARGET)) { 
     actualCommission = COMMISSION; 
    } else { 
     actualCommission = COMMISSION * accelFactor; 
    } 
    totalSales = s.annualSales * actualCommission; 
    totalWages = SALARY + totalSales; 
    System.out.println("Hello " + s.name); 
    System.out.println("Your total annual compensation is $" + totalWages); 
} 
} 
+0

非常感謝。這很有幫助! – Rachael

相關問題