Results 1 to 2 of 2
  1. #1
    tyke is offline Member
    Join Date
    May 2010
    Posts
    1
    Rep Power
    0

    Default Help with printing out new coordinates

    Hi there im new to programming and have a problem with some code


    import java.util.Scanner;

    public class StageOne
    {
    public static void main(String[] args)
    {
    final int WIDTH = 100; // width of the sheet in pixels
    final int HEIGHT = 100; // height of the sheet in pixels

    int x; // the x-coordinate
    int y; // the y-coordinate
    String direction; // the direction chosen

    // Create a Scanner object for keyboard input.
    Scanner keyboard = new Scanner(System.in);

    // 1. Ask the user for the ant's x-coordinate
    System.out.print("Enter your ant's x-coordinate: ");
    x = keyboard.nextInt();

    // 2. If the x-coordinate is outside the allowed range,
    // print an error message and exit
    if(x < 0 || x > WIDTH-1)
    {
    System.out.println("The x-coordinate must be between 0 and " + (WIDTH-1));
    System.exit(-1);
    }


    // 3. Ask the user for the ant's y-coordinate
    System.out.print("Enter your ant's y-coordinate: ");
    y = keyboard.nextInt();



    // 4. If the integer is outside the allowed range,
    // print an error message and exit
    if (y < 0 || y > HEIGHT-1)
    {
    System.out.println("The y-coordinate must be between 0 and "+ (WIDTH-1));
    System.exit(-1);
    }

    // 5. Ask the user for the direction
    System.out.println("Which direction would you like to move? Possible Directions are U, UR, R, DR, D, DL, L, UL");
    direction = keyboard.next();

    // 6. Increase or reduce the x and y-coordinates depending on the direction
    if(direction.equals("U"))
    {
    y += 1;
    }
    // else if(direction.equals("UR")) ... etc

    else if (direction.equals("UR"))
    {
    y += 1;
    x += 1;
    }

    else if (direction.equals("R"))
    {
    x += 1;
    }

    else if (direction.equals("DR"))
    {
    y -= 1;
    x += 1;
    }

    else if (direction.equals("D"))
    {
    y -= 1;
    }

    else if (direction.equals("DL"))
    {
    y -= 1;
    x -= 1;
    }

    else if (direction.equals("L"))
    {
    x -= 1;
    }

    else if (direction.equals("UL"))
    {
    y += 1;
    x -= 1;
    }
    // 7. If the direction is not one of the allowed values,
    // print an error message and exit

    else System.out.println("The direction must be either U, UR, R, DR, D, DL, L or UL ");
    System.exit(-1);

    // 8. "Wrap" the x and y-coordinates if the ant has walked over the edge of the paper
    if(x < 0)
    {
    x += WIDTH;
    }
    // else if(x > WIDTH-1) ... etc

    else if (x > WIDTH-1)
    {
    x += WIDTH-99;
    }

    else if (y < 0)
    {
    y += HEIGHT;
    }

    else if (y > HEIGHT-1)
    {
    y += 1;
    }

    // 9. Print out the new x and y-coordinates

    System.out.println(" X coordinate is "+(int)x);
    System.out.println(" Y coordinate is "+(int)y);

    }
    }

    when i run the program it goes blank after asking for the coordinates and direction, can anybody see what it is that im doing wrong?
    thanks

  2. #2
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    Please use code tags and proper indentation when you post your code, as it's much easier to read for those that want to help you. Another suggestion is to break down your program into separate methods instead of putting everything in the main method, as it's a lot simpler to track down a problem if you have small pieces to look over instead of one big blob. My suggestion is to look over the logic of your if else statements, as that is the most likely culprit.
    Ever seen a dog chase its tail? Now that's an infinite loop.

Similar Threads

  1. Help with plotting x-y coordinates
    By Fidelcashflow in forum New To Java
    Replies: 3
    Last Post: 12-02-2009, 11:17 AM
  2. Getting mouse coordinates
    By nishant.4545 in forum Advanced Java
    Replies: 3
    Last Post: 07-20-2009, 11:28 PM
  3. Resizing Coordinates In Applets
    By lolmasterzz in forum Java Applets
    Replies: 4
    Last Post: 07-12-2009, 04:17 AM
  4. button coordinates
    By jacline in forum AWT / Swing
    Replies: 2
    Last Post: 04-05-2009, 10:58 PM
  5. Arc2D.Double coordinates
    By alley in forum Java 2D
    Replies: 2
    Last Post: 11-07-2007, 10:27 PM

Posting Permissions

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