Web Analytics Made Easy -
StatCounter Problem in a loop? - CodingForum

Announcement

Collapse
No announcement yet.

Problem in a loop?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Problem in a loop?

    I'm having trouble with this code:

    Code:
     package nim2;
    
       import java.util.Random;
       import java.util.Scanner;
       import java.io.*; 
    	
        public class NimBoard {
        
          Row [] rows;
          private static final int MAX = 10;
          private Random random = new Random();
          
           public NimBoard() {
             //this part of the code is supposed to call Row() a random number of times between 3 and 10.
             //Row() is supposed to generate ramdom numbers from 0 to 21.
             int size = 3 + random.nextInt(MAX-3);
             rows = new Row [size];
             for (int i = 0; i< size; ++i)
                rows[i] = new Row ();
          }
        
          public String toString() {
       	//I suspect my problem is going to be here.
             StringBuffer buf = new StringBuffer ();
             int size = rows.length;
             for (int i =0; i< size; ++i) {
                buf.append ("Row ");
                buf.append (i);
                buf.append (":   ");
            		for (int j = 0; j< rows[i].GetHowmany(); ++j)
                buf.append ("*");
                buf.append ("\n");
             }
             return buf.toString();
          }
        
           public static void main (String [] args) {
             NimBoard board;
             //this is where I test results.
             board = new NimBoard();
             System.out.println(board);
          }
       }
    Code:
    package nim2;
       import java.util.Random;
       
    	public class Row {
          private static final int MAX = 21;
          private static int howmany;
          
           public Row() {
             howmany = 1 + (new Random().nextInt(MAX));
          }
        
           public int GetHowmany(){
             return howmany;	 
          }
            
           public void SetHowmany(int n){
             howmany = n;
          }
       
           public static void main(String[] args){
          
             Row T = new Row();
             System.out.println(T.GetHowmany());
             
          }
       }
    Basically, I want my output to be
    *******
    ********
    ****
    *****
    , where the number of stars on each row should be given by a new instance of Row(), but rigth now it looks like
    *******
    *******
    *******
    *******
    where the number of stars is the last random value returned from Row()
    I thought this loop woudl create a new instance of Row() everytime, and print out a number of stars, but it seems it doesn't:
    Code:
    for (int j = 0; j< rows[i].GetHowmany(); ++j)
                buf.append ("*");
                buf.append ("\n");
    Any ideas?

  • #2
    OMG, I can't belive I was actually able to find the error, it was something I could have never imagined, but I got tired and started messing with the Row class. After I wrote something on the setter method, I noted a change, so I went on:
    Code:
    public void setHowmany(int n) {
           if (n <0 ) howmany = 0;
            else if (n > MAX) howmany = MAX;
            else howmany = n;
    It was this code instead of the setter I had before. Now it runs smoothly.

    Comment

    Working...
    X