2015-11-05 52 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);