| | |
Found Bug but can't understand it
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 |
2dgraphics account android api apple applet application array arrays automation banking bidirectional binary binarytree birt bluetooth chat chatprogramusingobjects class client code columns component database derby design eclipse encryption error errors expand fractal game givemetehcodez graphics gui guidancer homework html ide if_statement image inheritance integer intellij interface j2me java javadesktopapplications javaprojects jlabel jme jni jpanel jtextfield julia linux list map method methods midlethttpconnection mobile mobiledevelopmentcreatejar monitoring myaggfun netbeans newbie nullpointerexception open-source problem program programming project property recursion reference ria scanner search server set sms sort sourcelabs splash sql sqlite static stop string support swing testautomation threads tree ui unicode validation windows






