| | |
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 |
Tag cloud for Java
addressbook android api apple applet application arguments array arrays automation binary bluetooth button calculator chat class classes client code columns component converter database draw eclipse error errors event exception file fractal ftp game givemetehcodez graphics gridlayout gui helpwithhomework html ide image inetaddress input integer j2me japplet java javaprojects jme jmf jni jpanel julia link linux list loop map method methods midlethttpconnection mobile netbeans newbie number objects openjavafx oracle php print problem program programming project projects recursion rim scanner screen server set signing size smart sms socket sort sql storm string support swing test threads time tree unlimited variablebinding webservices windows






