英文:
It is a question from a book that I cannot understand but somehow I wanted to know if I have coded it right
问题
使用单下标数组来解决以下问题。公司按佣金比例支付销售人员的工资。销售人员每周收入固定的$200,再加上当周总销售额的9%。例如,一个人在一周内总销售额为$5000,收入为$200加上$5000的9%,总计为$650。编写一个小程序(使用计数器数组),确定在以下每个范围内有多少销售人员的工资处于:
- $200-$299
- $300-$399
- $400-$499
- $500-$599
- $600-$699
- $700-$799
- $800-$899
- $900-$999
- $1000及以上
该小程序应使用第6章介绍的GUI技术。在JTextArea中显示结果。使用JTextArea的setText方法来更新用户输入的每个值。
这是问题,现在我想知道我做得对不对,因为书中没有人会纠正我。
public class ArrayExercise {
static final int salaryPerWeek = 200;
static final double commission = 0.09;
static int[] grossIncome = { 200, 300, 400, 500, 600, 700, 800, 900, 1000 };
public static void main(String[] args) {
JFrame frame = new JFrame();
JTextArea outputArea = new JTextArea();
outputArea.setText("上周销售人员收入:\n");
frame.add(outputArea);
for (int counter = 0; counter < grossIncome.length; counter++) {
outputArea.append("\n销售人员[" + counter + "] = " + calculate(grossIncome[counter]));
}
frame.setLayout(new FlowLayout());
frame.setVisible(true);
frame.setSize(300, 300);
}
public static double calculate(int salary) {
double total = 0;
total = salary * commission + salaryPerWeek;
return total;
}
}
输出结果如下:
销售人员[0] = 218.0
销售人员[1] = 227.0
销售人员[2] = 236.0
销售人员[3] = 245.0
销售人员[4] = 254.0
销售人员[5] = 263.0
销售人员[6] = 272.0
销售人员[7] = 281.0
销售人员[8] = 290.0
抱歉,我是新手,我真的想学习Java,请多多包涵。
英文:
Use a single-subscripted array to solve the following problem. A company pays its salesperson on a commission basis. The salespeople receive $200 per week plus 9% of their gross sales for that week. For example, a person grosses $5000 in sales in a week receives $200 plus 9% of $5000 or a total of $650. Write an applet (using an array of counters) that determines how many of the salespeople earned salaries in each of the following ranges ( assume that each salesperson's salary is truncated to an integer amount):
- $200-$299
- $300-$399
- $400-$499
- $500-$599
- $600-$699
- $700-$799
- $800-$899
- $900-$999
- $1000 and over
The applet should use the GUI techniques introduced in Chapter 6. Display the results in JTextArea. Use JTextArea method setText to update the results each value input by the user.
This is the problem, now I wanted to know if I did it right since no one would correct me from the book.
public class ArrayExercise {
static final int salaryPerWeek = 200;
static final double commission = 0.09;
static int[] grossIncome= { 200, 300, 400, 500, 600, 700, 800, 900, 1000 };
public static void main(String[] args) {
JFrame frame = new JFrame();
JTextArea outputArea = new JTextArea();
outputArea.setText("Salesperson income for the past week :\n");
frame.add(outputArea);
for ( int counter = 0; counter < grossIncome.length; counter++) {
outputArea.append("\nSalesperson[" + counter + "] = " + calculate( grossIncome[ counter ]));
}
frame.setLayout(new FlowLayout());
frame.setVisible(true);
frame.setSize(300, 300);
}
public static double calculate( int salary ) {
double total = 0;
total = salary * commission + salaryPerWeek;
return total;
}
}
The output goes like this :
Salesperson[0] = 218.0
Salesperson[1] = 227.0
Salesperson[2] = 236.0
Salesperson[3] = 245.0
Salesperson[4] = 254.0
Salesperson[5] = 263.0
Salesperson[6] = 272.0
Salesperson[7] = 281.0
Salesperson[8] = 290.0
Sorry I am new here and I really wanted to learn Java. Please bear with me.
答案1
得分: 1
问题描述为您提供了一个提示:
>(使用计数器数组)
您的数组大小是正确的,但它不应该包含货币金额,而应该包含每个工资范围内的人数。
private int[] countsForEachSalaryRange = new int[9];
- 该数组的第一个元素是收到200美元至299美元的销售人员人数。
- 该数组的第二个元素是收到300美元至399美元的销售人员人数。
- 该数组的第三个元素是收到400美元至499美元的销售人员人数。
- 依此类推。
当没有进行显式初始化时,新的 int[]
将自动填充为零值,这在这种情况下是您想要的。
您的程序应该为用户提供一种输入多个货币金额的方法。每个金额将是销售人员的总销售额。在问题描述的示例中,5000将是一个这样的总销售额。
一种做法是向您的 JFrame 添加一个 JTextField,用户可以在其中输入总销售额。向该 JTextField 添加一个 ActionListener,用于解析字段的文本为整数。
如果解析成功,您将希望分析整数值,即总销售额。
分析总销售额的一种简单方法是从总销售额计算工资,然后编写一系列 if/else 语句,检查工资是否在每个可能范围内。每个 if 语句的主体应该递增 countsForEachSalaryRange
的不同元素。
每次完成分析后,清除 outputArea,并使用当前计数数组设置其文本。
英文:
The problem description gives you a hint:
> (using an array of counters)
Your array is the right size, but it shouldn’t contain money amounts, it should contain counts of people in each salary range.
private int[] countsForEachSalaryRange = new int[9];
- The first element of that array is the number of salespeople who received $200–$299.
- The second element of that array is the number of salespeople who received $300–$399.
- The third element of that array is the number of salespeople who received $400–$499.
- And so on.
A new int[]
is automatically filled with zero values when no explicit initialization is done, which is what you want in this case.
Your program should provide a means for the user to enter multiple money amounts. Each amount will be the amount of gross sales made by a salesperson. In the example described by the problem, 5000 would be one such gross sales amount.
One way to do this is to add a JTextField to your JFrame, into which the user can type a gross sales amount. Add an ActionListener to that JTextField which parses the field’s text as an integer.
If the parsing succeeds, you’ll want to analyze the integer value, that is, the gross sales amount.
An easy way to analyze a gross sales amount is simply to calculate the salary from the gross sales, then write a series of if/else statements that check whether the salary is in each of the possible ranges. Each if-statement’s body should increment a different element of countsForEachSalaryRange
.
Each time that analysis is done, clear your outputArea and set its text using the current array of counts.
答案2
得分: 0
我认为你没有正确理解这个问题。你缺少一个额外的输入:每个销售人员的销售数据列表。你需要一个额外的输入列表,其中包含销售人员的销售额,对于每个收入,计算工资,并在Salesperson列表中对应的元素加1。
输入: 5000
工资: 650
销售人员[4] = +1
销售人员[4]代表工资在600到699之间的范围。因此,你需要将每个范围内的工资数量相加,而不是工资本身。
顺便说一下:用旧版本来学习Java基础没有问题。如今,很多开发人员尝试使用lambda和流来编写所有内容,但这并不总是有助于代码的可读性或可维护性。如果你从for循环和if语句开始,你会更好地理解这门语言。祝你好运。
英文:
I think you have not correctly understood the question. You are missing an extra input: the list of the sales figures per salesperson. You need an extra input list with salespersons sales and for each revenue calculate the salary and add 1 to the corresponding element in the Salesperson list.
input : 5000
salary: 650
Salesperson[4]= +1
Salesperson[4] represents salaries between 600 and 699. SO you have to add up the number of salaries in each range, not the salary itself.
By the way: there is no problem in learning the java basics with an older version. Nowadays a lot of developers try to write everything with lambda's and streams but it doesn't always help a lot to the readability or maintainability of the code. You will understand the language better if you start with for-loops and if-blocks. Good luck.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论