2016-12-01 149 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