我希望这段代码在每个循环中增加值assignId和assignSales 1和5000,但是从111和25000开始。我不能只改变值吗?这是代码:
package Salesperson;
public class DemoSalesperson2
{
public static void main(String[] args)
{
int assignId = 111;
double assignSales = 25000;
Salesperson salesperson1 = new Salesperson(assignId, assignSales);
Salesperson[] salesperson = new Salesperson[10];
for(int i = 0; i < salesperson.length; i++)
System.out.println("Salesperson " + i + " has ID #" + salesperson1.getId() + " and annual sales of $" + salesperson1.getSales());
Salesperson salesperson2 = new Salesperson(assignId + 1, assignSales + 5000);
}
}
答案 0 :(得分:1)
如果我理解你要做什么,那么你打算填充salesperson
(数组):
salespeople
。id
(id = assignId + i)和sales
(sales = assignSales + 5000i)。像
这样的东西int assignId = 111;
double assignSales = 25000;
Salesperson[] salespeople = new Salesperson[10];
for (int i = 0; i < salespeople.length; i++) {
salespeople[i] = new Salesperson(assignId + i, assignSales + (5000 * i));
System.out.printf("Salesperson %d has ID #%d and annual sales of $%.02f%n",
i, salespeople[i].getId(), salespeople[i].getSales());
}
哪个输出:
Salesperson 0 has ID #111 and annual sales of $25000.00
Salesperson 1 has ID #112 and annual sales of $30000.00
...
Salesperson 9 has ID #120 and annual sales of $70000.00
答案 1 :(得分:0)
你的代码现在可能无法正常工作,因为for
循环的主体周围没有大括号,正如@ElliottFrisch在上面的评论中指出的那样。
更改
for(int i = 0; i < salesperson.length; i++)
System.out.println("Salesperson " + i + " has ID #" + salesperson1.getId() + " and annual sales of $" + salesperson1.getSales());
Salesperson salesperson2 = new Salesperson(assignId + 1, assignSales + 5000);
到
for(int i = 0; i < salesperson.length; i++) {
System.out.println("Salesperson " + i + " has ID #" + salesperson1.getId() + " and annual sales of $" + salesperson1.getSales());
Salesperson salesperson2 = new Salesperson(assignId + 1, assignSales + 5000);
}
除非您包含大括号,否则编译器将假定只有下一个语句(直到下一个;
)是循环体,并且之后的所有其他语句都是而不是部分循环。
因此,在您提供的示例中,代码会在每次迭代时打印一些内容,但在退出循环之前不会递增值。
答案 2 :(得分:0)
在for循环,if语句等中包含多行时,需要使用括号。此外,您需要增加值本身,而不是简单地添加到您拥有它们的基值。 assignId + = 1与执行assignId = assignId + 1相同。如果在您的情况下不执行+ =,它将始终为Salesperson对象提供相同的编号,对于assignId为112,对于assignSales为3000。您也总是打印出相同的值,因为您使用相同的Salesperson对象,您应该使用salesperson2对象并在创建后打印出内容。
//for each iteration we need to increment assignId and assignSales
for(int i = 0; i < salesperson.length; i++)
{
//increment the variables themselves before passing to the objects
assignId += 1;
assignSales += 5000;
Salesperson salesperson2 = new Salesperson(assignId, assignSales);
System.out.println("Salesperson " + i + " has ID #" + salesperson2.getId() + " and annual sales of $" + salesperson2.getSales());
}//need second bracket to include more than one line in for loop
答案 3 :(得分:0)
我明白了。我需要在for循环中启动salesperson1并将值设置为i,然后它将在循环中添加值。
public static void main(String[] args)
{
final int SALES_INCREASE = 5000;
int assignId = 111;
double assignSales = 25000;
Salesperson[] salespeople = new Salesperson[10];
for(int i = 0; i < salespeople.length; i++)
{
salespeople[i] = new Salesperson(assignId, assignSales);
assignId += 1;
assignSales += SALES_INCREASE;
System.out.println("Salesperson " + i + " has ID #" + salespeople[i].getId() +
" and annual sales of $" + salespeople[i].getSales());
}
}
答案 4 :(得分:-1)
只需将其更改为assignSales + = 5000和assignId + = 1。