Question about the problem of shooting stars using Java for statements

26 Views Asked by At

Solving the problem of taking stars in Java. The questions are as follows.

  1. Print out a square with a width and length of 10*10 using *.

  2. The columns that match the x and y coordinates in the following array list should be outputted 0 instead of *. The complete version and array list are as follows. For example, if an element in an array list is given,

     Character character1  = new Character(5,5);
     Character character2  = new Character(7,8);
     Character character3  = new Character(3,1);
    

The output shall be as follows like this:

* * O * * * * * * * 
* * * * * * * * * * 
* * * * * * * * * * 
* * * * * * * * * * 
* * * * O * * * * * 
* * * * * * * * * * 
* * * * * * * * * * 
* * * * * * O * * * 
* * * * * * * * * * 
* * * * * * * * * * 

This is the code I'm currently writing.

public class Character {
     int xLocation;
     int yLocation;
    public Character(int x, int y){
        this.xLocation = x;
        this.yLocation = y;
    }
    public int getXLocation(){return xLocation;}
    public int getYLocation(){
        return yLocation;
    }
}

import java.util.ArrayList;
public class Main {
    public static void main(String[] args)
    {
        int a = 10;
        
        ArrayList<Character> characters = new ArrayList<>();
        Character character1  = new Character(5,5);
        Character character2  = new Character(7,8);
        Character character3  = new Character(3,1);
        characters.add(character1);
        characters.add(character2);
        characters.add(character3);
        for (int i = 1; i<=a; i++)
        {
            for (int v = 1; v <= a; v++)
            {
                for (int j = 0; j < characters.size(); j++)
                {
                    if (v == characters.get(j).getXLocation() && i == characters.get(j).getYLocation())
                    {
                        System.out.print("O ");
                    }
                    else System.out.print("* ");
                }
                System.out.println();
            }
        }
    }
}

However, the code has the following incorrect output:

* * * 
* * * 
* * O 
* * * 
* * * 
* * * 
* * * 
* * * 
* * * 
* * * 
* * * 
* * * 
* * * 
* * * 
* * * 
* * * 
* * * 
* * * 
* * * 
* * * 
* * * 
* * * 
* * * 
* * * 
* * * 
* * * 
* * * 
* * * 
* * * 
* * * 
* * * 
* * * 
* * * 
* * * 
* * * 
* * * 
* * * 
* * * 
* * * 
* * * 
* * * 
* * * 
* * * 
* * * 
O * * 
* * * 
* * * 
* * * 
* * * 
* * * 
* * * 
* * * 
* * * 
* * * 
* * * 
* * * 
* * * 
* * * 
* * * 
* * * 
* * * 
* * * 
* * * 
* * * 
* * * 
* * * 
* * * 
* * * 
* * * 
* * * 
* * * 
* * * 
* * * 
* * * 
* * * 
* * * 
* O * 
* * * 
* * * 
* * * 
* * * 
* * * 
* * * 
* * * 
* * * 
* * * 
* * * 
* * * 
* * * 
* * * 
* * * 
* * * 
* * * 
* * * 
* * * 
* * * 
* * * 
* * * 
* * * 
* * * 
1

There are 1 best solutions below

0
MorganS42 On

The issue is you have the System.out.println() in the wrong place in your nested-loop. Your current solution starts a new line after iterating over all the characters, when really it should start a new line after iterating over all the x-values for a given row (in this case you've named the variable v). This just means moving the System.out.println() out one loop, such as in the following:

import java.util.ArrayList;
public class Main {
    public static void main(String[] args)
    {
        int a = 10;
        
        ArrayList<Character> characters = new ArrayList<>();
        Character character1  = new Character(5,5);
        Character character2  = new Character(7,8);
        Character character3  = new Character(3,1);
        characters.add(character1);
        characters.add(character2);
        characters.add(character3);
        for (int i = 1; i<=a; i++)
        {
            for (int v = 1; v <= a; v++)
            {
                for (int j = 0; j < characters.size(); j++)
                {
                    if (v == characters.get(j).getXLocation() && i == characters.get(j).getYLocation())
                    {
                        System.out.print("O ");
                    }
                    else System.out.print("* ");
                }
            }
            System.out.println(); // Only line changed
        }
    }
}