Welcome to Dream.In.Code
Become a Java Expert!

Join 150,201 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 2,012 people online right now. Registration is fast and FREE... Join Now!




looping problem

 
Reply to this topicStart new topic

looping problem

pangjd
24 Sep, 2008 - 10:27 PM
Post #1

New D.I.C Head
*

Joined: 24 Sep, 2008
Posts: 4

hi guys.. i am supposed to print a pattern which goes like this:
AA
BBAA
AABBAA
BBAABBAA
AABBAABBAA

however, what i am getting now is
AA
BBAA
AAAABB
BBBBAAAA
AAAAAABBBB

this is my program code:
java

import java.util.Scanner;
public class P4
{
public static void main(String[] args)
{
int a, b, lines, height; //enter variables
Scanner sc=new Scanner(System.in);

System.out.print("Enter height of the pattern: ");
height = sc.nextInt();

for (lines=1; lines<=height; lines++)
{
if (lines%2==1){ //odd lines
for (a=1;a<=lines;a+=2)
System.out.print("AA");
{
for (b=1;b<=lines-1;b+=2)
System.out.print("BB");
{

}
System.out.println();}}
if (lines%2==0){ //even lines
for (b=1;b<=lines;b+=2)
System.out.print("BB");
{
for (a=1;a<=lines;a+=2)
System.out.print("AA");}
System.out.println();}
}
}
}


i cant figure out the patter to make it AABBAABB...pls help me.

Thanks

EIDT: W_W code.gif

This post has been edited by William_Wilson: 25 Sep, 2008 - 04:52 AM
User is offlineProfile CardPM
+Quote Post

BetaWar
RE: Looping Problem
25 Sep, 2008 - 04:44 AM
Post #2

#include <soul.h>
Group Icon

Joined: 7 Sep, 2006
Posts: 2,304



Thanked: 101 times
Dream Kudos: 1275
My Contributions
You just have some basic looping issues here is some code that I think does what you are looking for:

CODE
import java.util.Scanner;

public class Runner
{
    public static void print(int height){
        int a = height;
        for (int lines = 0; lines < a; lines++, height--)
        {
            if(height%2 == 0){
                System.out.print("BB");
            }
            else if(height%2 == 1){
                System.out.print("AA");
            }
        }
    }
    public static void main(String[] args)
    {
        int a, b, lines, height; // enter variables
        Scanner sc = new Scanner(System.in);

        System.out.print("Enter height of the pattern: ");
        a = height = sc.nextInt();

        for(int i=0; i<height; i++){
            print(i+1);
            System.out.println();
        }
        
        
    }
}

User is offlineProfile CardPM
+Quote Post

pangjd
RE: Looping Problem
25 Sep, 2008 - 06:59 AM
Post #3

New D.I.C Head
*

Joined: 24 Sep, 2008
Posts: 4

QUOTE(BetaWar @ 25 Sep, 2008 - 05:44 AM) *

You just have some basic looping issues here is some code that I think does what you are looking for:

CODE
import java.util.Scanner;

public class Runner
{
    public static void print(int height){
        int a = height;
        for (int lines = 0; lines < a; lines++, height--)
        {
            if(height%2 == 0){
                System.out.print("BB");
            }
            else if(height%2 == 1){
                System.out.print("AA");
            }
        }
    }
    public static void main(String[] args)
    {
        int a, b, lines, height; // enter variables
        Scanner sc = new Scanner(System.in);

        System.out.print("Enter height of the pattern: ");
        a = height = sc.nextInt();

        for(int i=0; i<height; i++){
            print(i+1);
            System.out.println();
        }
        
        
    }
}


hi betawar... thanks for your code... however i still do not understand the code. i really apologise cause i'm new and is trying to figure out the codes.

could you explain? thanks a lot!
User is offlineProfile CardPM
+Quote Post

johnmalloy
RE: Looping Problem
25 Sep, 2008 - 07:42 AM
Post #4

New D.I.C Head
*

Joined: 24 Sep, 2008
Posts: 8



Thanked: 2 times
My Contributions
I hope this helps:

CODE

import java.util.Scanner;


//Declare your class here
public class Runner {

    //Create the class to create each iteration of height
    public static void print(int height){
        //Set the INTEGER "a" set to the value of "height".
        int a = height;
        /************************************
        **Start LOOP
        **Start of by setting "lines" = 0
        **As log as lines is smaller then a keep looping
        **Once the loop is started, increase the value of "lines" for each iteration
        **Also you need to decrese the value of "height", meaning the 1st level is
        **done so when it starts again it will start at the next level
        **This is where you get the AABBAA, which is the "HORIZIONAL" height.
        **Meaning level 1 is AA, 2 is AABB, 3 is AABBAA, and so on.
        **Once "lines" is equal to "a", stop looping
        ************************************/
        for (int lines = 0; lines < a; lines++, height--){
            //If the instance is on an even, then print out "BB"
            if(height%2 == 0){
                System.out.print("BB");
            }
            //If the instance is on an odd, then print out "AA"
            else if(height%2 == 1){
                System.out.print("AA");
            }
        }
    }
    //main class, a MUST to run a JAVA App
    public static void main(String[] args)
    {
        //Declare local variables
        int a, b, lines, height;
        //Set up main to recieve the users inputed data
        Scanner sc = new Scanner(System.in);
        //Message printed out to user giving directions on what to do
        System.out.print("Enter height of the pattern: ");
        //assigns the int a to height and then points to the value recieved from the user
        a = height = sc.nextInt();
        /*****************************************
        **loop for each level
        **This is where the "VERTICAL" height is applied.
        **Example
        **AA
        **AABB
        **AABBAA
        **And so on
        ******************************************/
        for(int i=0; i<height; i++){
            print(i+1);
            System.out.println();
        }
        
        
    }
}

User is offlineProfile CardPM
+Quote Post

pangjd
RE: Looping Problem
25 Sep, 2008 - 08:57 AM
Post #5

New D.I.C Head
*

Joined: 24 Sep, 2008
Posts: 4

QUOTE(johnmalloy @ 25 Sep, 2008 - 08:42 AM) *

I hope this helps:

CODE

import java.util.Scanner;


//Declare your class here
public class Runner {

    //Create the class to create each iteration of height
    public static void print(int height){
        //Set the INTEGER "a" set to the value of "height".
        int a = height;
        /************************************
        **Start LOOP
        **Start of by setting "lines" = 0
        **As log as lines is smaller then a keep looping
        **Once the loop is started, increase the value of "lines" for each iteration
        **Also you need to decrese the value of "height", meaning the 1st level is
        **done so when it starts again it will start at the next level
        **This is where you get the AABBAA, which is the "HORIZIONAL" height.
        **Meaning level 1 is AA, 2 is AABB, 3 is AABBAA, and so on.
        **Once "lines" is equal to "a", stop looping
        ************************************/
        for (int lines = 0; lines < a; lines++, height--){
            //If the instance is on an even, then print out "BB"
            if(height%2 == 0){
                System.out.print("BB");
            }
            //If the instance is on an odd, then print out "AA"
            else if(height%2 == 1){
                System.out.print("AA");
            }
        }
    }
    //main class, a MUST to run a JAVA App
    public static void main(String[] args)
    {
        //Declare local variables
        int a, b, lines, height;
        //Set up main to recieve the users inputed data
        Scanner sc = new Scanner(System.in);
        //Message printed out to user giving directions on what to do
        System.out.print("Enter height of the pattern: ");
        //assigns the int a to height and then points to the value recieved from the user
        a = height = sc.nextInt();
        /*****************************************
        **loop for each level
        **This is where the "VERTICAL" height is applied.
        **Example
        **AA
        **AABB
        **AABBAA
        **And so on
        ******************************************/
        for(int i=0; i<height; i++){
            print(i+1);
            System.out.println();
        }
        
        
    }
}


i sort of get the idea...thanks for the help guys

User is offlineProfile CardPM
+Quote Post

pbl
RE: Looping Problem
25 Sep, 2008 - 02:34 PM
Post #6

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 3,587



Thanked: 233 times
Dream Kudos: 75
My Contributions
Or more simply, just prefix a String withh AA or BB

CODE

    String str = "";
        
    for(int i = 0; i < height; i++) {
        if(i % 2 == 0)
            str = "AA" + str;
        else
            str = "BB" + str;
        System.out.println(str);
    }

User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 05:01AM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month