跟随B的坐标100,-100

时间:2018-11-05 14:33:54

标签: java arrays

//intelligent next direction
//0 -> up, 1 -> right, 2 -> down, 3 -> left
static int nextIntelligent(int p1, int p2, int q1, int q2)

如何使用Java中的IFWHILE表单执行此操作,结果将在最后显示

 Intelligente Verfolgung
 Position A: (21, -2)
 Position B: (21, -2)
 Anzahl der Schritte: 24

我希望你能理解我的意思

1 个答案:

答案 0 :(得分:0)

public class Bsp04 {

    public static void main(String[] args) {

        int a1, a2, b1 = 0, b2 = 0, xA, yA, xB, yB;
        int pos, count = 0;

        //////// TEIL I

        System.out.print("1. Koordinate von A: ");
        a1 = SavitchIn.readLineInt();    
        System.out.print("2. Koordinate von A: ");
        a2 = SavitchIn.readLineInt();

        if (a1 > 100) {
            a1 = 100;
        }
        else if (a1 < -100) {
            a1 = -100;
        }

        if (a2 > 100) {
            a2 = 100;
        }

        else if (a2 < -100) {
            a2 = -100;
        }

        xA = a1;
        yA = a2;
        xB = b1;
        yB = b2;

        while(isSamePosition(a1, a2, b1, b2) == false){
            pos = nextDirection();

            if (pos == 0 && a2 < 100) {
                a2++;
                count++;

            }

            else if (pos == 1 && a1 < 100) {
                a1++;
                count++;

            }

            else if (pos == 2 && a2 > -100) {
                a2--;
                count++;
            }

            else if (pos == 3 && a1 > -100) {
                a1--;
                count++;
            }

            if (isSamePosition(a1, a2, b1, b2) == false) {
                pos = nextDirection();

                if (pos == 0 && b2 < 100) {
                    b2++;
                }

                else if (pos == 1 && b1 < 100) {
                    b1++;
                }
                else if (pos == 2 && b2 > -100) {
                    b2--;
                }

                else if (pos == 3 && b1 > -100) {
                    b1--;

                }

            }
        }


        System.out.println("Alles Zufall");
        System.out.println(" Position A: (" + a1 +", " + a2 +")");
        System.out.println(" Position B: (" + b1 +", " + b1 +")");
        System.out.println(" Anzahl der Schritte: " + count);
        }

        static int nextDirection(){
            int random = PRNG.randomInt(4);
            return random;
            }

        static boolean isSamePosition (int p1, int p2, int q1, int q2){
            boolean same = false;

            if (p1 == q1 && p2 == q2) {
                same = true;
            }

            return same;
        }


        static int nextIntelligent(int xA, int yA, int xB, int yB){

            return -1;
        }
    }