2016-12-01 145 views
-2

我希望此代码将值assignId和assignSales递增1和5000每个循环,但从111和25000开始。我不能只是更改值,但我可以吗?这里是代码:For循环中递增数组值

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); 
    } 
} 
+4

当你使用循环,如果语句等多行代码你需要使用大括号 – shash678

回答

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()); 
    } 
} 
-1

只是将其更改为assignSales + = 5000和assignId + = 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); 
} 

除非你有括号,编译器会认为只有在第二天的语句(直到下一个;)是循环体,而之后的其他一切是而不是循环的一部分。

因此,在您提供的示例中,代码将在每次迭代时打印一些内容,但不会在值退出循环之前递增值。

0

当在for循环中封闭多行时,您需要使用括号,if语句等。您还需要增加值本身,而不是简单地添加到您拥有的基本值。 assignId + = 1与assignId = assignId + 1相同。如果您没有对您的情况执行+ =,那么它总是会给销售人员对象分配相同的编号,分配标识为112,assignSales为3000。由于您使用相同的销售人员对象,因此总是打印出相同的值,您应该使用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 
0

如果我明白你正在尝试做的,那么你的意思是填充salesperson(阵列);我会重命名它salespeople。接下来,我会计算id(id = assignId + i)和sales(sales = assignSales + 5000i)。另外,我更喜欢格式化的IO。喜欢的东西,

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