gpt4 book ai didi

java - 将信息从一个类(class)拉到另一个类(class)

转载 作者:行者123 更新时间:2023-11-29 04:57:41 27 4
gpt4 key购买 nike

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

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

我不知道如何在运行 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

最佳答案

两个字段

 int annualSales;
String name;

应设为“公共(public)静态”。此外,您不应在 main 方法中再次定义它们。

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

关于java - 将信息从一个类(class)拉到另一个类(class),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33174445/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com