5

As i really like Programming and i like to program in my free time so i was trying to create a code in which the output would look like an x. Something like this.

x    x
 x  x
  x
 x  x
x    x

So i wanted the user to input the height of the "x". This is the code i have so far and i really don't know how to move on. I just need a hint or if anyone can tell me where i went wrong.

import java.util.Scanner;    
    public class x{
    public static void main(String[] args){
    Scanner kbd = new Scanner(System.in);
    int height;    
    System.out.print("Enter the height of the X:   " );             
    height = kbd.nextInt();
    for (int i = 1; i <= height; i++){                        
      for (int j = 1; j <= height; j++) {                            
        if(i ==j || j+i == height + 1)                               
            System.out.println("x");                            
        else                            
            System.out.print(" ");
      }
    }
  }
}
5
  • you want to print only 1 line of "x"-es? or some table? Commented Apr 26, 2013 at 17:19
  • 4
    Java is indexed at 0, not 1. Commented Apr 26, 2013 at 17:21
  • 1
    You don't always need new line after x in System.out.println("x"); correct this. Commented Apr 26, 2013 at 17:21
  • oh actually i think Grijesh deserves to be the answerer of this question. Commented Apr 26, 2013 at 17:23
  • @SlaterTyranus That is correct if you are iterating an array or a list. However, here it is just a counter in a for-loop, and the break condition is using <=, so for this particular use case it does not matter. Commented Apr 26, 2013 at 17:27

3 Answers 3

5

Two changes:

  • change System.out.println("x"); to System.out.print("x"); (remove ln after print)

  • after the two lines

        System.out.print(" ");
    }
    

    add

    System.out.println();
    
Sign up to request clarification or add additional context in comments.

2 Comments

"add System.out.println(); after the line System.out.print(" ");" surely not straight after, but after the }, or else each blank space will get a new line
that's right, Richard. thanks for the heads up! i'll edit answer.
0
 for (int i = 0; i < height; i++){                        
    for (int j = 0; j < height; j++) {                            
        if(i == j || j + i == height - 1)                               
            System.out.print("x");                            
        else                            
            System.out.print(" ");
    }
    System.out.println();
 }

Comments

0

This works for me for both odd and even height's of X:

import java.util.Scanner;  
public class x{
    public static void main(String[] args){
    Scanner kbd = new Scanner(System.in);
    int height;    
    System.out.print("Enter the height of the X:   " );             
    height = kbd.nextInt();
    for (int i = 0; i <= height; i++){                        
      for (int j = 0; j <= height; j++) {                            
        if( (i ==j && i!=0 ) || j+i == height + 1) //needed to check for i or j !=0
            System.out.print("x"); //this shouldn't be println             
        else                            
            System.out.print(" ");
      }
      System.out.println(); //you needed this
    }
  }
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.