Results 1 to 6 of 6
Like Tree2Likes
  • 1 Post By JosAH
  • 1 Post By wsaryada

Thread: can't find the logic

  1. #1
    riris is offline Member
    Join Date
    Mar 2012
    Posts
    6
    Rep Power
    0

    Default can't find the logic

    hy all, i can't figure out the logic of this
    PHP Code:
    | | | | | | | | | | |
    | | | | | | | | | | |
    |A|*|*|*|*| | | | | |
    | | | | |*| | | | | | 
    | | | | |B| | | | | |
    | | | | | | | | | | |
    | | | | | | | | | | |
    | | | | | | | | | | |
    | | | | | | | | | | |
    | | | | | | | | | | |
    The position of A and B is random. And A and B is linked by *. What is the logic of this?? do i have to make an array 10 x 10, and access the index randomly to put A??? or what??

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,541
    Blog Entries
    7
    Rep Power
    20

    Default Re: can't find the logic

    You have coordinates (xa, ya) and (xb, yb) the step to take in the x direction is xd= (xb > xa)?1:-1 and similar for yd; now loop over x:

    Java Code:
    for(; xa != xb; xa+= xd)
       // do something with coordinate (xa, ya)
    and similar again with ya and yd.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    riris is offline Member
    Join Date
    Mar 2012
    Posts
    6
    Rep Power
    0

    Default Re: can't find the logic

    Thanks for the respon. but, i don't know what you mean

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,541
    Blog Entries
    7
    Rep Power
    20

    Default Re: can't find the logic

    Quote Originally Posted by riris View Post
    Thanks for the respon. but, i don't know what you mean
    Neither do I, so we're even.

    kind regards,

    Jos ;-)
    Sutured likes this.
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    riris is offline Member
    Join Date
    Mar 2012
    Posts
    6
    Rep Power
    0

    Default Re: can't find the logic

    i have found the way to place A and B randomly. Here is my scrip
    Java Code:
    import java.util.Random;
    
    public class OrderMap {
        public static void main(String args[]){
            
            int Y=10;
            int X=10;
            
            OrderMap baru = new OrderMap();
            
            int y = baru.ran();
            int x = baru.ran();
            for(int i=0; i<Y; i++){
                for(int j=0; j<=X; j++){
                    if(y==x){
                        x=x+1;
                    }
                    System.out.print("|");
                    if(i==y && j==x){
                        System.out.print("A");
                    }
                    else if(i==x && j==y){
                        System.out.print("B");
                    }
                    else if(y>x){
                        if(i==x && j==x){
                            System.out.print("*");
                        }
                        else{
                            System.out.print(" ");
                        }
                    }
                    else if(y<x){
                        if(i==x && j==x){
                            System.out.print("*");
                        }
                        else{
                            System.out.print(" ");
                        }
                    }
                }
                System.out.println("");
            } 
            System.out.println(y + " " + x);
        }
        
        private int rand;
        
        public int ran(){
            int nilai[]= new int[10];
            
            Random create = new Random();
            rand = create.nextInt(nilai.length);
            return rand;
        }
    }
    but still, i can conect A and B with *. Is there any suggestion???

  6. #6
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    760
    Rep Power
    8

    Default Re: can't find the logic

    You've been cross posting this question on can't find the logic (Beginning Java forum at JavaRanch). The solution given by tarun mall is your solution.
    DarrylBurke likes this.

Similar Threads

  1. Where's the logic?
    By diamonddragon in forum New To Java
    Replies: 11
    Last Post: 01-28-2012, 04:16 AM
  2. Logic Help
    By Barbados in forum New To Java
    Replies: 6
    Last Post: 01-22-2012, 11:24 PM
  3. Need help in logic
    By nn12 in forum New To Java
    Replies: 3
    Last Post: 03-23-2011, 06:44 PM
  4. Need help on logic
    By nn12 in forum New To Java
    Replies: 6
    Last Post: 03-10-2011, 11:06 AM
  5. Cant get the logic right
    By jermaindefoe in forum New To Java
    Replies: 4
    Last Post: 03-11-2008, 12:22 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •