| | |
Found Bug but can't understand it
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Feb 2008
Posts: 25
Reputation:
Solved Threads: 0
hi,
I have to do a program that generates 'n' different combinations of 7 different numbers between 1 and 49, so I did this:
When I run it:
This happens cecause, for int[][] apostas = new int[7][n];, apostas.lenght equals 7! how can I obtain 'n' with just the array and the length function?
thanks in advance!
I have to do a program that generates 'n' different combinations of 7 different numbers between 1 and 49, so I did this:
Java Syntax (Toggle Plain Text)
import java.util.*; public class Totoloto { public static void main(String[] args) { System.out.print("Quantas apostas?"); Scanner s = new Scanner(System.in); int n=s.nextInt(); int[][] apostas = geraApostas(n); for(int i=0;i<apostas.length;i++) { System.out.print("["); for(int a=0;a<7;a++) { System.out.print(apostas[i][a]); if(a!=6) System.out.print(", "); } System.out.println("]"); } } private static int[] geraAposta(){ int[] aposta = new int[7]; for(int i=0;i<7;i++) { int num; Boolean existe; do{ existe=false; num=(int) (Math.random()*49+1); for(int a=0;a<7;a++) if(aposta[a]==num) existe=true; }while(existe); aposta[i]=num; } return aposta; } private static int[][] geraApostas(int n){ int[][] apostas = new int[7][n]; System.out.println(n + "" + apostas.length); for(int i=0;i<n;i++) { int[] aposta; Boolean existe; do{ existe=false; aposta=geraAposta(); for(int a=0;a<n;a++) if(apostas[n]==aposta) existe=true; }while(existe); apostas[i]=aposta; } return apostas; } }
When I run it:
Java Syntax (Toggle Plain Text)
Quantas apostas?9 97 Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 9 at Totoloto.geraApostas(Totoloto.java:54) at Totoloto.main(Totoloto.java:12)
This happens cecause, for int[][] apostas = new int[7][n];, apostas.lenght equals 7! how can I obtain 'n' with just the array and the length function?
thanks in advance!
![]() |
Similar Threads
- Open In New Window Php (PHP)
- Whats the benefit from Open Source? (Getting Started and Choosing a Distro)
- This Should be Easy for You Guys! (Linux Servers and Apache)
- About blank and fake security centre - nothing I try works. (Viruses, Spyware and other Nasties)
- hmmm, expert CSS cheat ? (JavaScript / DHTML / AJAX)
- RedHat Network Server (*nix Hardware Configuration)
- Need help with DirectX code (C)
Other Threads in the Java Forum
- Previous Thread: Help in writing a litle program for a friend
- Next Thread: Java app to send an SMS via DKU-5 serial cable
| Thread Tools | Search this Thread |
-xlint android api applet application array arrays automation bi binary blackberry block bluetooth chat class classes client code compile compiler component database developmenthelp eclipse error event exception fractal freeze game gameprogramming givemetehcodez graphics gui health html ide image input integer j2me j2seprojects java javac javaprojects jetbrains jni jpanel jtable julia learningresources lego linux list login loop loops mac map method methods mobile netbeans newbie notdisplaying number online oracle page print problem program programming project qt recursion scanner screen server set singleton size sms sort sql string swing system template textfields threads time title tree tutorial-sample update variablebinding windows working xor






