java 如何增加int变量的值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30094818/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 16:27:43  来源:igfitidea点击:

How to increment the value of an int variable?

javaandroidincrement

提问by Sun

I would like to increment the value of an int variable whenever a user taps on a button, but right now the value gets incremented only once.

我想在用户点击按钮时增加 int 变量的值,但现在该值只会增加一次。

This is what I use to increment the value of the variable p.

这就是我用来增加变量值的方法p

@Override
public void onClick(View v) {
    int p = 1;

    if (p == 9) {
        Toast.makeText(context, "You have reached to maximum number", Toast.LENGTH_LONG).show();
    } else {
        p = p + 1;                      
        holder.textViewQuantity.setText("" + p);                        
    }
}

回答by weston

When pis inside the method it is a local variableand is a unique value for each method invocation, initialized as 1every time.

p在方法内部时,它是一个局部变量,并且是每个方法调用的唯一值,每次都初始化1

Moving it to the class it becomes an instance variable(AKA field) and maintains its latest value for the length of the life of the class instance.

将它移动到类中,它成为一个实例变量(AKA 字段)并在类实例的生命周期内保持其最新值。

Read this doc on the 4 types of variablesfor more infomation.

阅读有关 4 种类型变量的此文档以获取更多信息。

private int p = 1; //moved 

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub

    if(p == 9) {
        Toast.makeText(context, "You have reached to maximum number", Toast.LENGTH_LONG).show();
        return ;
    }
    else {                              
        p = p+1;                        
        holder.textViewQuantity.setText(""+p);                      
    }

回答by flyingAssistant

Variable pshould be a class member:

变量p应该是类成员:

int p = 1;  

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub


    if(p == 9) {
        Toast.makeText(context, "You have reached to maximum number", Toast.LENGTH_LONG).show();
        return ;
    }
    else {                              
        p = p+1;                        
        holder.textViewQuantity.setText(""+p);                      
    }

回答by Danyal Sandeelo

Declare it as global variable. Make it visible to the whole class (I assume, you are not declaring the object again and again). At the moment the scope of the variable is limited to the function and this variable gets the value of 1 every time the event occurs.

将其声明为全局变量。使其对整个班级可见(我假设,您不会一次又一次地声明该对象)。目前,变量的范围仅限于函数,每次事件发生时,该变量的值都为 1。

whenever this event is called, your value p is assigned value 1.

每当调用此事件时,您的值 p 都会被赋值为 1。

class YourClass{

     int p;  /// creating p 
     YourClass(){
      p = 1;     /// initializing the value in constructor;
     } 


 // your onClick Event Code;
 @Override
 public void onClick(View v) {
 if(p == 9) {
 Toast.makeText(context, "You have reached to maximum number", Toast.LENGTH_LONG).show();
   return ;
 }
 else {                              
   p = p+1;                        
    holder.textViewQuantity.setText(""+p);                      
 }
 }  

回答by Arthur

move the int p = 1 out of the function, you're resetting p to 1 onclick.

将 int p = 1 移出函数,您将 p 重置为 1 onclick。

回答by Bhavin Patel

Your code should be like below :

您的代码应如下所示:

private int p = 1;

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
    if(p == 9) {
        Toast.makeText(context, "You have reached to maximum number", Toast.LENGTH_LONG).show();
        return ;
    }
    else {                              
        p = p+1;                        
        holder.textViewQuantity.setText(""+p);                      
    }
}

int p should be a class member.

int p 应该是一个类成员。

回答by m4tt

You can make your variable static, then it will count all button taps for given class:

您可以将变量设为静态,然后它会计算给定类的所有按钮点击次数:

class Button {
   public static int p = 0;

   @Override
   public void onClick(View v) {
   // TODO Auto-generated method stub     

   if(p == 9) {
       Toast.makeText(context, "You have reached to maximum number", Toast.LENGTH_LONG).show();
       return ;
    }
    else {                              
       p = p+1;                        
       holder.textViewQuantity.setText(""+p);                      
    }
}

回答by manojprasad

private int p = 0;

私人整数 p = 0;

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
     p+=1;
   if(p<9)
     holder.textViewQuantity.setText(String.valueof(p)); 
   else
    Toast.makeText(context, "You have reached to maximum number", Toast.LENGTH_LONG).show();
}