Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/366.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/198.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 静态计数(Android)_Java_Android_Count - Fatal编程技术网

Java 静态计数(Android)

Java 静态计数(Android),java,android,count,Java,Android,Count,在方法bounce()中第一次增量中的count值不会向前移到第二次增量中..我的意思是suposecount=0。然后第一次增量count时,它的值变为第1秒,值也变为1..这可能是因为两个计数存储在不同的内存位置…如果我使count为静态,则每次运行应用程序它保留上次存储在其中的值,这是我不想要的..我想要第一次增量中的count值应向前移到第二次增量中..而且我不能将count放在if loop insidebounce()方法之外,因为它将给出扭曲的输出..我该怎么办 public cl

在方法
bounce()中
第一次增量中的count值不会向前移到第二次增量中..我的意思是supose
count=0
。然后第一次增量count时,它的值变为第1秒,值也变为1..这可能是因为两个计数存储在不同的内存位置…如果我使count为静态,则每次运行应用程序它保留上次存储在其中的值,这是我不想要的..我想要第一次增量中的count值应向前移到第二次增量中..而且我不能将count放在if loop inside
bounce()
方法之外,因为它将给出扭曲的输出..我该怎么办

public class Ball {

private Point p;               //Represents the x and y position of the Ball
private int c;                 //Represents the color of the Ball
private int r;                 //Represents the Radius of the Ball.
private int dx;                   //Represents the change in x position of the Ball. (Horizontal Speed)
private int dy;               //Represents the change in y position of the Ball. (Vertical Speed)
private Paint paint;           //Android Object holding the color for drawing on the canvas
public int count = 0;
public Ball(int x,int y,int col,int radius)
{

    p=new Point(x,y);
    c=col;
    r=radius;
    paint=new Paint();
    dx=0;
    dy=0;
}
public int getCount()
{return count;
}
public int getX()                       //Returns the x position of the ball as an integer
{return p.x;
}
public int getY()                      //Returns the y position of the ball as an integer
{
    return p.y;
}

public int getRadius()               //Returns the Radius of the ball as an integer
{return r;
}
public Paint getPaint()            //Returns the Paint of the Ball as a Paint Object
{return paint;
}
public void setColor(int col)      //Sets the Color of the Ball
{c=col;
if (paint == null) 
    paint = new Paint();
 paint.setColor(col);
}
public void goTo(int x,int y)       //Sets the x and y positions of the Ball
{p.x=x;
p.y=y;
}
public void setDX(int speed)      //Sets the Horizontal Speed of the Ball
{dx=speed;
}
public void setDY(int speed)       //Sets the Vertical Speed of the Ball
{
    dy=speed;
}
public void move()                 //Changes the x and y position by the dx and dy values
{
    p.x=p.x+dx;
    p.y=p.y+dy;
}
public void setX(int x)
{
    p.x = x;
}
public void setY(int y)
{
    p.y = y;
}
public int getDX()
{
    return p.x;
}
public int getDY()
{
    return p.y;
}
public void bounce(Canvas canvas)       //Bounces off the Sides of the Canvas
{ 
    move();
    if(p.x>canvas.getWidth()|| p.x<0)
    {
        count++;
        dx=dx * -1;
    }
    if(p.y>canvas.getWidth()|| p.y<0)
    {
        count++;
        dy=dy * -1;
    }


    MainActivity.tv.setText(String.valueOf(getCount()));
}
}
公开课舞会{
私有点p;//表示球的x和y位置
private int c;//表示球的颜色
private int r;//表示球的半径。
private int dx;//表示球的x位置的变化。(水平速度)
private int dy;//表示球的y位置的变化。(垂直速度)
private Paint;//保存画布上绘制颜色的Android对象
公共整数计数=0;
公共球(整数x,整数y,整数列,整数半径)
{
p=新点(x,y);
c=col;
r=半径;
油漆=新油漆();
dx=0;
dy=0;
}
public int getCount()
{返回计数;
}
public int getX()//以整数形式返回球的x位置
{返回p.x;
}
public int getY()//以整数形式返回球的y位置
{
年回报率;
}
public int getRadius()//以整数形式返回球的半径
{返回r;
}
public Paint getPaint()//将球的绘制作为绘制对象返回
{返回油漆;
}
public void setColor(int col)//设置球的颜色
{c=col;
if(paint==null)
油漆=新油漆();
油漆。设置颜色(col);
}
public void goTo(int x,int y)//设置球的x和y位置
{p.x=x;
p、 y=y;
}
public void setDX(int speed)//设置球的水平速度
{dx=速度;
}
public void setDY(int speed)//设置球的垂直速度
{
dy=速度;
}
public void move()//通过dx和dy值更改x和y位置
{
p、 x=p.x+dx;
p、 y=p.y+dy;
}
公共无效集合x(整数x)
{
p、 x=x;
}
公共空间设置(整数y)
{
p、 y=y;
}
公共int getDX()
{
返回p.x;
}
公共int getDY()
{
年回报率;
}
public void bounce(画布)//从画布的侧面反弹
{ 
move();

如果(p.x>canvas.getWidth()| | p.xcanvas.getWidth())||p.y您应该使用singleton类来保持计数增量,并且每当应用程序启动时都将其设置为零。使用setter和getter方法来代替静态设置singleton类并在该类中保持计数是非常容易的。

只是认为我应该添加注释作为答案。您应该只创建一个
Ball
类并使用它。每次创建
Ball
的新实例时,设置
count=0

您需要显示如何实现您的类。如果每次使用
Ball
类时创建一个新实例,则每次都将设置
count=0
。应该完全没有必要在这里使用单例。午餐盒对该问题的评论是即时的-这里必须创建多个
Ball
实例。我将count声明为static,并在其内部将其初始化为0constructor@user3503165:不,不要将其设置为静态-作为实例变量完全可以,只要避免创建多个实例即可