Reputation: 245
I am to make this following pattern on a for loop:
XXXXXXXXXX
XXXXXXXXXY
XXXXXXXXYY
XXXXXXXYYY
...
..and so on
public class ex{
public static void main(String[] args){
for(int i=0;i<=10;i++){
System.out.println();
for(int j=0;j<=10;j++){
if(i==0){
System.out.print("X");
}
if(i==1){
System.out.print("X");
if(j==9){
System.out.print("Y");
}
}
}
}
}
} ~
I am getting extra "X" at the end for my output that I don't want. I think there is a better way to do this but can't think of a way right now
Any help guys?
Upvotes: 2
Views: 406
Reputation: 141829
Try nesting two loops inside one loop. Count up to i
and then continue counting up to 10
on each iteration of the outer loop:
// 10 lines
for(int i = 10; i >= 0; i--){
int j = 0;
// Print 'X's (10 - i of them)
for(; j < i; j++)
System.out.print("X");
// Print 'Y's (i of them)
for(; j < 10; j++)
System.out.print("Y");
System.out.println();
}
Upvotes: 3
Reputation: 7957
The condition for the / diagonal of a matrix is i + j = n, so the left-upper part is i + j < n and i + j > n for the right-lower part.
public static void main(String... arg) {
int n = 10;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if ( i + j < n) {
System.out.print("X");
} else {
System.out.print("Y");
}
}
System.out.println();
}
}
If you want to separate on \ diagonal, the condition for the diagonal is i = j, for the upper part i > j and for the lower part i < j.
Upvotes: 0
Reputation: 3497
hint: each row is of the same length, there is just one less X and one more Y on each row down.
public class XY
{
public static void main(String[] args)
{
System.out.print(xy(10,10));
}
public static String xy(int rows,int origRows)
{
return X(rows,origRows-rows)+"\n"+((rows>0)?xy(rows-1,origRows):"");
}
public static String X(int x,int y)
{
return (x>0?"X":"")+((x>0||y>0)?X(x-1,y-1):"")+(y>0?"Y":"");
}
}
teehee.
Upvotes: 0
Reputation: 21220
Here is a recursive solution:
public class ex {
public static final String X = "X";
public static final String Y = "Y";
public static void main(String[] args){
printall(10, 0);
}
private static void printall(int length, int saturation){
if (saturation > length) {
return;
} else {
System.out.print(printRow(length, saturation, 0);
printall(length, saturation + 1);
}
}
private static String printrow(int length, int saturation, int position) {
if (position > length) {
return "";
} else {
return getChar(length, saturation, position) + printrow(length, saturation, position + 1);
}
}
private static String getChar(int length, int saturation, int position) {
if (length-saturation < position) {
return Y;
} else {
return X;
}
}
}
Upvotes: 0
Reputation: 17629
Probably not the most efficient version, but here it goes:
public static void main(String[] args) {
for (int i = 0; i <= 10; i++) {
System.out.println(repeat("X", 10 - i) + repeat("Y", i));
}
}
private static String repeat(String string, int times) {
return new String(new char[times]).replaceAll("\0", string);
}
Upvotes: 0
Reputation: 12202
The answer to your specific question is:
for(int i=0;i<=10;i++)
i<=10
should be i<10
, since from 0 to 10 (inclusive) there are eleven loops.
Upvotes: 0