Results 1 to 2 of 2
- 05-10-2010, 04:42 AM #1
Member
- Join Date
- May 2010
- Posts
- 1
- Rep Power
- 0
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
- 05-10-2010, 06:34 AM #2
Senior Member
- Join Date
- Feb 2010
- Location
- Ljubljana, Slovenia
- Posts
- 470
- Rep Power
- 4
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
-
Help with plotting x-y coordinates
By Fidelcashflow in forum New To JavaReplies: 3Last Post: 12-02-2009, 11:17 AM -
Getting mouse coordinates
By nishant.4545 in forum Advanced JavaReplies: 3Last Post: 07-20-2009, 11:28 PM -
Resizing Coordinates In Applets
By lolmasterzz in forum Java AppletsReplies: 4Last Post: 07-12-2009, 04:17 AM -
button coordinates
By jacline in forum AWT / SwingReplies: 2Last Post: 04-05-2009, 10:58 PM -
Arc2D.Double coordinates
By alley in forum Java 2DReplies: 2Last Post: 11-07-2007, 10:27 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks