将信息从一个班级拉到另一个班级

时间:2015-10-16 15:39:44

标签: java class

我是Java的新手。对于课堂作业,我需要输入销售人员的姓名和年度销售额,然后根据固定年薪50,000美元显示他们的总工资。它必须包括2个类。

我让salesperson班级正常工作。 Annualwages类运行,但显示“hello null”和“您的年度总薪酬为50,000美元”(这只是没有增加销售额的固定年薪)。

在运行salesperson时,我无法弄清楚如何从Annualwages提取信息。

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

3 个答案:

答案 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 :(得分:1)

两个字段

 int annualSales;
 String name;

应该制作&#34; public static&#34;。您也不应该在main方法中再次定义它们。

最后,您似乎没有使用IDE。下载&#34; Eclipse IDE&#34;用Java编写代码。它会让你的生活更轻松

答案 2 :(得分:1)

你必须这样做:

<div class="flexContainer">
    <div class="img">fixed size img here</div>
    <div class="flexItem">This flexes</div>
    <div class="wrapItem">This wraps</div>
</div>