size = 10
for i in range(size) :
    for j in range(size) :
        if(i == j or size - j == i + 1) :
            print("* ", end = "")
        elif(i == size/2 - 1 or j == size/2 -1 or i == size/2 or j == size/2):
            print("  ", end = "")
        elif(i == 0 or j == 0 or i == size - 1 or j == size -1):
            print("* ", end = "")
        else :
            print("  ", end = "")
    print("")

i dont know python at all ...it would be a great help if i get the code in java ...beacuse i really dont know python...its related to print pattern using for loops

Recommended Answers

All 3 Replies

Even if you don't know Python, why can't you convert it line by line. For example, is line 1 a problem?

commented: public class Output2 { public static void main(String[] args) { int n = 10; for(int i=0;i<n;i++) { for(int j=0;j<n;j++) +0

If you don't mind me asking, why did you need to translate this particular piece of code? Keep in mind that, if this is an assignment, we can only give advice, not do the problem for you.

Just to make sure we're all on the same page, the code section in question prints a set of four arrows pointed towards the corners. Having seen this, the logic of the code should be a bit clearer, even if you don't know Python. The equivalent Java code is fairly straightforward; the logic is the same, just in a slightly different syntax (and I do mean 'slightly' - if you treat the Python code as if it were pseudocode, you should be able to translate it directly into a Java method).

If you replace the or operators with || operators, use else if instead of elif, and re-write the for: conditions like so:

 for (int i=0; i <= size; i++) {
     for (int j=0; j <= size; j++) {
         // body goes here
     }
 }

The rest is mostly just using System.out.print() for the printout (with the last one a System.out.println() instead).

Oh, and you'd have to make a class for it to be in, of course.

public class Output2 {

    public static void main(String[] args) {
        int n = 10;
          for(int i=0;i<n;i++)
          {
              for(int j=0;j<n;j++)
              {

                  if(i == j || n - j == i + 1 )

//                        
//              
                  {
                  System.out.print("* ");
                  }
                  else if
                  (i == n/2 - 1 || j == n/2 -1 || i == n/2 ||j == n/2)
                  System.out.print("* ");
                  else
                      System.out.print(" ");

                  if
                  (i == 0 || j == 0 || i == n- 1 || j == n -1)
                  System.out.print("  ");
                  else 
                      System.out.print(" ");
                          }
                  System.out.println();
              }
      }

      }

//this is expected output[Click Here

arrow_square.png

](null)

commented: What is i, j, n? Always use descriptive variable names. +16
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.