Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: bingo ticket loop problem (doesn't compile like it should...)

  1. #1
    Junior Member
    Join Date
    Nov 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Lightbulb bingo ticket loop problem (doesn't compile like it should...)

     
    import java.util.Scanner;
    import java.util.Random;
     
    public class demo 
    {
     
    	public static void main(String[] args)
    	{
    		Random rand= new Random(); // being able to use random object
     
    		int[][] array = new int[3][9];
    		 // print array in rectangular form
    		for (int i=0; i<array.length; i++) 
    		 {
    		     for (int j=0; j<array[i].length; j++) 
    		     { 
     
    		    	 for(int x=0;x<5;x++)
    		    	 {
    		    		 int k = rand.nextInt(9);
    		    		 array[i][k] = k*10 +rand.nextInt(9)+1;
     
    		    	 }
     
    		    	 System.out.print("\t" + array[i][j]); 
    		     }
    		     System.out.println("");//
    		 }
     
    	}
    }

    I'm creating a bingo ticket. I need to respect these rules
    -
    The first column contains numbers from 1 to 9,
    - The second column numbers from 10 to 19,
    - The third 20 to 29 and so on up until the last column, which contains numbers from 80 to 90 (the 90 being
    placed in this column as well)
    - Each row contains five numbers and four blank spaces

    I'm having a problem with the 5 numbers max per row, i created the array[3][9], then i made a loop so it would put 5 numbers in a random place in the row but the output show me that there is a number in everywhere ...

    Did i made a mistake somewhere ?

    thank you


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: bingo ticket loop problem (doesn't compile like it should...)

    You say this doesn't compile, but you haven't provided a compiler error. What error are you getting?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Nov 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: bingo ticket loop problem (doesn't compile like it should...)

    oh sorry by doesnt compile; i wanted to say it doesnt give the same output when i tried the loop

    for(int x=0;x<5;x++)
    {
    int k = rand.nextInt(9);
    array[i][k] = k*10 +rand.nextInt(9)+1;

    }

    in a different class to test it out . It supposed to create 5 (x<5) numbers but it create 27 in total for all the arrays !

  4. #4
    Junior Member
    Join Date
    Nov 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: bingo ticket loop problem (doesn't compile like it should...)

    0 0 0 34 45 52 62 77 87
    0 0 24 32 43 58 66 73 87
    0 16 21 32 46 54 65 77 86

    it gave me this but it supposed to have 5 random number per row ...

  5. #5
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: bingo ticket loop problem (doesn't compile like it should...)

    I'm really not sure what you're asking. Your array is 3 X 9, which means it has 27 elements. That for loop doesn't really do much, as it's setting the same element each time, so only the last iteration matters.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  6. #6
    Junior Member
    Join Date
    Nov 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: bingo ticket loop problem (doesn't compile like it should...)

    if i understand correctly if i delete the loop for(int x=0;x<5;x++)... it will only create only 0 in every elements

    i thought that by making the last loop it will only create a random number in a random spot on the row until there is 5 number then start over on the next line and the others element will be 0, dont know if im making any sense...
    i'm doing my first java programming class so i'm still a beginner in java

  7. #7
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: bingo ticket loop problem (doesn't compile like it should...)

    Quote Originally Posted by mv740 View Post
    if i understand correctly if i delete the loop for(int x=0;x<5;x++)... it will only create only 0 in every elements
    What happened when you tested that theory?

    Quote Originally Posted by mv740 View Post
    i thought that by making the last loop it will only create a random number in a random spot on the row until there is 5 number then start over on the next line and the others element will be 0, dont know if im making any sense...
    i'm doing my first java programming class so i'm still a beginner in java
    Why do you want to randomize the spot? You can't guarantee you hit every spot- you might hit the same spot several times. Why not just iterate through each element and set it equal to whatever you want?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Bank Balance - while loop - wont compile
    By mwardjava92 in forum Loops & Control Statements
    Replies: 15
    Last Post: November 9th, 2011, 05:19 PM
  2. lotto ticket generator help
    By tlckl3m3elmo in forum What's Wrong With My Code?
    Replies: 19
    Last Post: July 15th, 2011, 12:39 AM
  3. Help outputing to txt file (BINGO GAME)
    By Chris in forum What's Wrong With My Code?
    Replies: 0
    Last Post: June 23rd, 2010, 09:10 PM
  4. [SOLVED] While Loop Problem :(
    By faisalnet5 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: May 10th, 2010, 11:23 AM
  5. Little Loop Problem
    By chronoz13 in forum Loops & Control Statements
    Replies: 1
    Last Post: October 17th, 2009, 04:40 AM