增加一个整数变量?

时间:2017-11-27 12:51:21

标签: android global-variables

  

我有一个问题在a之后递增我的整数变量   Android编程符合一定条件。为什么不增加?   请帮忙......

下面的代码在ImageButton上实现,ImageButton是一个名为Product的类(Activity)。

if error = system_errors[response_code]
  error[:error_class].new(error[:error_message], technical_message, response_code)
else
  puts 'skipping test'
end

这是我的Singleton类,我在其中调用它的方法:

int count = 1;

List<Item> prod = ShoppingCart.getInstance().getProducts();

// if arraylist is null --> add a product
if(prod != null && prod.isEmpty()){
    ShoppingCart.getInstance().addItem(new Item(
            namePureString,
            manufacturePureString,
            pricePureString,
            "",
            count
    ));
    Toast.makeText(getApplicationContext(), namePureString + " Added To Cart", Toast.LENGTH_SHORT).show();

}else {

    for (Item products : ShoppingCart.getInstance().myProducts) {

        // here i am checking if the product exists, if it does --> count has to increment
        if (products.getManufacture().equals(manufacturePureString)) {

            count += count;
            Toast.makeText(getApplicationContext(), "You Added This Product\nQuantity Will Increase",
                    Toast.LENGTH_SHORT).show();

            ShoppingCart.getInstance().setItemExists(new Item(
                    namePureString,
                    manufacturePureString,
                    pricePureString,
                    "",
                    count
            ));

            } else {
            ShoppingCart.getInstance().addItem(new Item(
                    namePureString,
                    manufacturePureString,
                    pricePureString,
                    "",
                    count
            ));
        }
}

2 个答案:

答案 0 :(得分:3)

count ++与count = count + 1相同,而不是count + = count。 Count + = count会在count的值之上添加计数。

所以要增加添加:

count = count + 1; //or count++;

答案 1 :(得分:1)

尝试

int count = 0;

count = count + 1;

Log.d("Answer: ",count);