Jump to content

Nested For Loops Trouble

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
4 replies to this topic

#1
hmmmmm

hmmmmm

    Newbie

  • Members
  • Pip
  • 1 posts
-----1
----21
---321
--4321
-54321
654321

123456
-12345
--1234
---123
----12
-----1

_______

I cannot seem to create this. Any help is appreciated.

what i've got so far:

public class HW4 {

    public static void main(String[] args) {
        int count = 1, count2 = 1;
        String lineNum = "1";
        
        for(count = 1; count>=6; count++){
            for (count2 = 6; count2>=count; count2--){
                if (count2==count){
                    System.out.print(lineNum);
                }
                System.out.print(" ");
                
            }
            System.out.println();
        }
    }
}
i thought this would print the first line but it doesn't print anything.

Edited by ZekeDragon, 20 September 2010 - 08:33 PM.
Please use [code] tags when posting code.


#2
Sinipull

Sinipull

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 386 posts
aaaaand? what have you got so far? What can you create?

#3
Generic

Generic

    Newbie

  • Members
  • PipPip
  • 26 posts
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package winsat.info.hdd;

import java.util.Arrays;

/**
 *
 * @author Jay
 */
public class Main2 {

    public static void main(String[] args){
        String str = "";
        for (char c = '1'; c <= '6'; c++) {
            str = c + str;
            System.out.println(addLeftPad(str, '-', 6 - str.length()));
        }
        
        for(char c = '6'; c >= '1'; c--) {
            str = trim(str, 1, str.length());
            System.out.println(addLeftPad(str, '-', 6 - str.length()));
        }
    }
    
    public static String trim(String str, int start, int end) {
        return new String(Arrays.copyOfRange(str.toCharArray(), start, end));
    }

    /**
     * @param str
     * @param pad
     * @param newSize
     */
    private static String addLeftPad(String str, char pad, int padCount) {
        char[] arr = str.toCharArray();
        char[] newStr = new char[arr.length + padCount];
        int i;
        for (i = 0; i < padCount; i++) {
            newStr[i] = pad;
        }
        for (int n = 0; n < arr.length; n++) {
            newStr[i+n] = arr[n];
        }

        return new String(newStr);
    }
}

I did the first half right, didn't notice how you wanted second one. That should set you to the right path though.

#4
Sinipull

Sinipull

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 386 posts
ouch, i'll better show something:

public class Test
{
    public static String[] fill = {"", " ", "  ", "   ", "    ", "     ", "      ", "       ","        ","         "};
    public static final int NUMBER_COUNT = 7;
    
    public static void main(String[] args){
                
        for(int i = 0; i < NUMBER_COUNT; i++){
            System.out.print((fill[NUMBER_COUNT-i-1]));
            for(int j = i; j> 0; j--){
                System.out.print(j);
            }            
            System.out.println();
        }        
    }
}
Modify this to get the second one.

#5
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
There's a tutorial about it. Also check the following posts on the tutorial -->http://forum.codecal...p-examples.html