vendredi 8 octobre 2021

This code is printing the coordinates twice. Can someone help me with how I can improve it, or what is the correct solution?

public class RandomWalker {
    public static void main(String[] args) {
        int y=0;
        int x=0;
        int n=Integer.parseInt(args[0]);
        for(int i=0; i<n; i++) {
            System.out.println("("+x+","+y+")");
            double wandom=Math.random();
            if(wandom<0.25) {
                y++;
            } else if(wandom<0.5) {
                x++;
            } else if(wandom<0.75) {
                y--;
            } else if(wandom<1) {
                x--;
            }
            System.out.println("("+x+","+y+")");
        }
        double ans=x*x+y*y;
        System.out.println(ans);
    }
}

This code is printing the coordinates twice. Can someone help me with how I can improve it, or what is the correct solution?




Aucun commentaire:

Enregistrer un commentaire