Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Help with embedded loops and if statements. Please!

  1. #1
    Junior Member
    Join Date
    Oct 2013
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Help with embedded loops and if statements. Please!

    Hello, I am trying to code a basic battleship game and need help with nested loops. I am trying to use stored values in first loop in the second loop to put an * instead of an X for the last row and column number.


    This is my code so far...

    import java.util.Random;
    public class createBoard {
     
    	public static void main(String args[])
    	{
    		int squareRow = 0; // declares variable squareRow, location of row coordinate
    		int squareColumn = 0; // declares variable squareColumn, location of column coordinate
    		int direction = 0; // declares variable direction, direction of ship 1=north, 2=east, 3=south, 4=west
    		int length = 0; // declares variable length, length of each ship
    		String directionName = ""; // declares string variable directionName
    		Random generator =  new Random(); // declares new random variable generator
     
    			for(int i = 0; i < 5; i++)  // for loop generates random number for variables: loops 5 times
    			{
    				squareRow = generator.nextInt(10) + 1;  // generates random number between 1 and 10 for row
    				squareColumn = generator.nextInt(10) + 1; // generates random number between 1 and 10 for column
    				direction = generator.nextInt(4) + 1; // generates random number between 1 and 4 for direction
    				length = generator.nextInt(5) + 1; // generates random number between 1 and 5 for length
    				System.out.println("Row: " + squareRow + ", Column: " + squareColumn + ", Direction: " + directionName + ", Length: " + length);
     
    				if(direction == 1)
    					directionName = "North";  // sets direction if 1 to north
    				if(direction == 2)
    					directionName = "East"; // sets direction if 2 to east
    				if(direction == 3)
    					directionName = "South"; // sets direction if 3 to south
    				if(direction == 4)
    					directionName = "West"; // sets direction if 4 to west
    			}
    				System.out.println();
    				System.out.println("   ABCDEFGHIJ");
     
    			int a = 0; 
    			for(int x = 1; x <= 10; x++)
    			{
    				a++;
    				if(x == 1 || x == 2 || x == 3 || x == 4 || x == 5 || x == 6 || x == 7 || x == 8 || x == 8 || x == 9)
    					System.out.print(a + "  ");
    				if(x == 10)
    					System.out.print(a + " ");
     
    				for(int y = 1; y <= 10; y++)
    					{
    						System.out.print("X");
    					}
    				System.out.println();	
    			}	
    	}
    }


    This is the output....


    Row: 1, Column: 9, Direction: South, Length: 4
    Row: 10, Column: 5, Direction: West, Length: 4
    Row: 6, Column: 9, Direction: West, Length: 1
    Row: 1, Column: 1, Direction: West, Length: 3
    Row: 3, Column: 3, Direction: East, Length: 2

    ABCDEFGHIJ
    1 XXXXXXXXXX
    2 XXXXXXXXXX
    3 XXXXXXXXXX
    4 XXXXXXXXXX
    5 XXXXXXXXXX
    6 XXXXXXXXXX
    7 XXXXXXXXXX
    8 XXXXXXXXXX
    9 XXXXXXXXXX
    10 XXXXXXXXXX

    This is how the out put should look. There should be an * corresponding with the row and column numbers.



    Row: 1, Column: 9, Direction: South, Length: 4
    Row: 10, Column: 5, Direction: West, Length: 4
    Row: 6, Column: 9, Direction: West, Length: 1
    Row: 1, Column: 1, Direction: West, Length: 3
    Row: 3, Column: 3, Direction: East, Length: 2

    ABCDEFGHIJ
    1 *XXXXXXX*X
    2 XXXXXXXXXX
    3 XX*XXXXXXX
    4 XXXXXXXXXX
    5 XXXXXXXXXX
    6 XXXXXXXX*X
    7 XXXXXXXXXX
    8 XXXXXXXXXX
    9 XXXXXXXXXX
    10 XXXX*XXXXX


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,039
    My Mood
    Mellow
    Thanks
    184
    Thanked 647 Times in 634 Posts

    Default Re: Help with embedded loops and if statements. Please!

    Please read the Announcement topic at the top of the sub-forum to learn how to post code in code tags and other useful info for newcomers.

    What should the output look like? Copy the output in your first post into a text editor, edit it to be what it should be, and then copy/paste it into a new post or at the end of your original post after you add the code tags.

Similar Threads

  1. Replies: 1
    Last Post: September 15th, 2013, 10:01 PM
  2. Using loops and control statements to draw lines
    By john.adam in forum Object Oriented Programming
    Replies: 3
    Last Post: January 19th, 2012, 08:51 AM
  3. Strings, if statements, while loops
    By ikebaseball28 in forum Loops & Control Statements
    Replies: 2
    Last Post: October 26th, 2011, 04:45 PM
  4. Help with JavaDB embedded
    By jstn455 in forum JDBC & Databases
    Replies: 2
    Last Post: March 17th, 2011, 03:27 PM
  5. [SOLVED] Fixing of bug in number guessing game
    By big_c in forum Loops & Control Statements
    Replies: 6
    Last Post: April 16th, 2009, 02:15 AM