0

OK so what would I need to change to the following code so it would produce the following:

xxx
xoo
xoo

As at the moment it is producing:

xxxooo
xxxooo
xxxooo

My code that I have used can be seen below:

import javax.swing.*;
public class test {
    public static void main(String[] args){

        int row; 

        String num = JOptionPane.showInputDialog("Input?");

        row = Integer.parseInt(num);
        for(int i = 0; i < row; i++){  
            for(int j = 0; j < row; j++){
                System.out.print("X");
            }
            for (int z = 0; z < row; z++){
                System.out.print("O");
        }
        System.out.println("");
    }
}
}
3
Contributors
2
Replies
3
Views
5 Years
Discussion Span
Last Post by wen_cai
0

What are the rules for what is printed on each line?

The simplest way:
System.out.println("xxx\nxoo\nxoo");

0

If you have to keep the for loop. Consider the code below.

for(int i = 0; i < row; i++){  
    for(int j = i; j < row; j++){
        System.out.print("X");
    }
    for (int z = 0; z < i; z++){
        System.out.print("O");
    }
    System.out.println();
}
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.