简体   繁体   English

Java。 数组,保存以前的值

[英]Java. Array, save previous values

Cant figure out how to do this thing. 无法弄清楚该怎么做。 I need to store and count number of "jumps" and direction of "jump" in array. 我需要存储并计算数组中“跳转”的数量和“跳转”的方向。 Console has to display something like this: 控制台必须显示以下内容:

: Jump 1, direction Left :跳1,向左方向

: Jump 2, direction Right :跳2,向右方向

In my code below, when I print array elements, int jump always equals to 0; 在下面的代码中,当我打印数组元素时, int jump始终等于0;

I tried many variations, sometimes I got (for example): 我尝试了许多变体,有时我得到了(例如):

jump();

jump();

jump();

jump();

And it prints: {0,0,0,4} , instead of {1,2,3,4}; 并输出: {0,0,0,4} ,而不是{1,2,3,4};

public class Jump {
int jump;
String direction;
Jump[] jumpArray = new Jump[100];
int storeJump;
String storeDirection;

public void jump() {
    jump++;
    if (jump > 50) {
        System.out.println("need a rest");
        return;
    } else {

        for (int i = 0; i < jumpArray.length; i++) {
            jumpArray[i] = new Jump();
        }
    }
    jumpArray[jump].storeJump = jump;
    jumpArray[jump].storeDirection = direction;

}

public static void main(String[] args) {
    Jump jumper = new Jump();
    jumper.jump();
    jumper.jump();

    for (int i = 0; i < jumper.jump; i++) {
        System.out.println(jumper.jumpArray[i].jump);

    }

}

}

I think it would make more sense to move the jumpArray into main() : 我认为将jumpArray移到main()会更有意义:

public class Jump {
    String direction;
    //int storeJump;   Not sure what this is for
    //String storeDirection;    Or this

    @Override
    public String toString() {
        return direction;
    }

    // Static function that creates an initializes a Jump object
    public static Jump jump(String direction) {
        Jump jump = new Jump();
        jump.direction = direction;
        return jump;
    }

    public static void main (String[] args)
    {
        Jump[] jumpArray = new Jump[100];
        int numJumps = 0;

        // A list of directions for testing
        String [] path = new String[]{"north", "south", "east", "west"};

        // Create a bunch of jumps
        for (String dir : path) {
            if (numJumps > 50) {
                System.out.println("need a rest");
                break;
            }
            // Create a new jump and add to the array
            jumpArray[numJumps++] = Jump.jump(dir);
        }

        // Print the jumps
        for (int i = 0; i < numJumps; i++) {
            System.out.println(i + " " + jumpArray[i]);
        }
    }
}

I think the code below will solve your problem, but I think You should improve your code. 我认为下面的代码可以解决您的问题,但是我认为您应该改进代码。

int jump;
String direction;
Jump[] jumpArray = new Jump[100];
int storeJump;
String storeDirection;

public void jumping() {
    jump++;
    if (jump > 50) {
        System.out.println("need a rest");
        return;
    } else {
        jumpArray[jump] = new Jump();
    }
    jumpArray[jump].storeJump = jump;
    jumpArray[jump].storeDirection = direction;

}

public static void main(String[] args) {
    Jump jumper = new Jump();
    jumper.jumping();
    jumper.jumping();

    Set<Jump> mySet = new HashSet<Jump>(Arrays.asList(jumper.jumpArray));

    for (int i = 1; i < mySet.size(); i++) {
        System.out.println(jumper.jumpArray[i].storeJump);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM