2015-11-05 72 views
-3

我必須將-1替換爲將評估爲新購物者ID號的適當整數值的值。如何在方法內創建一個自動遞增的ID

import java.util.Scanner; 

public class MethodHolder { 

    private static int nextShopperID = 100; 
    private static int nextProductID = 100; 

    protected static Shopper generateShopper(){ 

     Scanner scan = new Scanner(System.in); 
     String name = null; 
     double balance = 0.0; 
     int productLimit = 0; 

     System.out.println("Please enter the shoppers name: "); 
     name = scan.next(); 
     System.out.println("Please enter the shoppers balance: "); 
     balance = scan.nextDouble(); 
     System.out.println("Please enter the shoppers product limit: "); 
     productLimit = scan.nextInt(); 


     // replace -1 with the appropriate increment expression 
     return new Shopper(-1, name, balance, productLimit); 
    } 
+1

這是什麼問題? – ergonaut

+0

你知道'if'語句是什麼嗎?順便說一句:[爲我的Java類生成唯一的int ID](http://stackoverflow.com/q/32639692) – Tom

+0

這是什麼'nextShopperID'打算是什麼?這可能與你的問題有關嗎? – zapl

回答

2

嘗試在nextShopperID中傳遞並增加它。

return new Shopper(nextShopperID++, name, balance, productLimit);