Benzin
Member
+576|6244
OK, I have this example to program:

Code:

...#...
..###..
...#...
I know I need to use a set of for-loops, but I just cannot get the algorithm down that I need. This is supposed to be horribly simple, but it is driving me out of my mind.

Code:

public class Carpet
{

public static void main (String args[])
{

    int breite = 7;
    int hoehe = 3;

    for (int h = 1 ; h <= hoehe ; h++)
    {
    
    for (int i = 1 ; i <= breite-1 ; i++)
    {
        System.out.print(".");

        if (i == 3)
            System.out.print("#");

    }//end for i
    
    
    
    System.out.println();
    
    }//end for h

}//end method main

}//end class Carpet
That is what I have come up with so far...
Benzin
Member
+576|6244
This is my output with the current code, btw:

Code:

...#...
...#...
...#...
Surgeons
U shud proabbly f off u fat prik
+3,097|6735|Gogledd Cymru

Can you not do something using the "modulus operator %" ?
Benzin
Member
+576|6244
I can use anything.
Benzin
Member
+576|6244
can anyone here help?
jsnipy
...
+3,277|6768|...

I don't want to rob you of thinking ...

First you don't need an inner loop, you can line break based on what Sheriff said ...

Second, look at the the desired output of characters not in terms of rows, rather as if the characters were in line. You should see a pattern. Think of how a carpet is made ...

...#.....###.....#...
Benzin
Member
+576|6244

jsnipy wrote:

I don't want to rob you of thinking ...

First you don't need an inner loop, you can line break based on what Sheriff said ...

Second, look at the the desired output of characters not in terms of rows, rather as if the characters were in line. You should see a pattern. Think of how a carpet is made ...

...#.....###.....#...
I had honestly NOT thought of being able to print it like that and just put the necessary line breaks ... lemme go start working on it and I'll let you know how I do.

Thanks!
Benzin
Member
+576|6244
Here is what I've done ... it works, but I was kinda hoping to accomplish this more with loops (our professor LOVES using loops):

Code:

public class Carpet
{

public static void main (String args[])
{

    int breite = 7;
    int hoehe = 3;

    for (int h = 1 ; h <= hoehe ; h++)
    {
    
    if (h == 1 || h == 3)
            System.out.print("...#...");
    else
            System.out.print("..###..");
    
    System.out.println();
    
    }//end for h

}//end method main

}//end class Carpet
How might you have done it? Again, mine works, but it's damn near cheating.
Benzin
Member
+576|6244
OK, I managed to get help on another forum:

Code:

public class Carpet1
{

    public void printCarpet(int size, char a)
    {
        for(int i =0 ; i < size ; i++)
        {
            for(int j=0 ; j < size ; j++)
            {
                if( (j < Math.abs((size-1)/2-i)) || (size-j) <= Math.abs((size-1)/2-i) )
                {
                // zeichne einen Punkt
                    System.out.print(".");
                }
                else
                // zeichne das übergebene Zeichen 
                System.out.print(a);
            }
            System.out.println();
        }
        
    }//end method printCarpet

}//end class Carpet1
It works now. Just need to put in the necessary parameter and character in the main method of another class. Someone gave me the answer for the math, I just need to wrap my head around it and figure out how said person came to the answer. Ugh. I hate algorithms, lol. If I didn't have to take this class for my degree, I wouldn't. Ah well. Could be worse.

Board footer

Privacy Policy - © 2024 Jeff Minard