|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
Lucky Dip Lotto Ticket code!!!!
Please can someone help with code for a little programme Iam trying to write in Java.
Iam having trouble with the sort feature, iam using a bubble sort. code as follows:- // Method : Sort 6 numbers in ascending order public void sortNumbers() { for(int next = 0; next < lottoNumber.length-1; next++){ for (int other = next +1; other < lottoNumber.length; other++){ if(lottoNumber [next] > lottoNumber [other]) int temp = lottoNumber [other]; lottoNumber [other] = lottoNumber [next]; lottoNumber [next] = temp; next = -1; } } [img]images/buttons/edit.gif[/img] |
|
#2
|
||||
|
||||
|
moved to Java Development
|
|
#3
|
||||
|
||||
|
Code:
public void sortNumbers() {
for(int next = 0; next < lottoNumber.length-1; next++) {
for (int other = next +1; other < lottoNumber.length; other++) {
if(lottoNumber [next] > lottoNumber [other])
int temp = lottoNumber [other];
lottoNumber [other] = lottoNumber [next];
lottoNumber [next] = temp;
next = -1;
}
}
After properly indenting your code, I noticed you're missing a closing bracket. Also, you don't have brackets around your IF statement, was that intended? In doing this, only the first statement will execute when the IF statement is true. I haven't analyzed your logic, just your syntax. Hopefully that's a start. Note: The code in my post is the exact same as yours... if you copy&paste this code, you'll need to add that final closing bracket. |
|
#4
|
|||
|
|||
|
Print Method for Lotto
thank you for your help, the bracket was in the wrong place and should have enclosed the if statement, changed and there is no syntax errors now
))I now need to add a print method // Method : Format and display 6 numbers to the screen. I belive i use the original for loop add a print method, am i correct, perhaps you can give me some guidance. No ticket is generated yet from the code i have input, i take it this will happen when i add a print method?? Regards Handy |
|
#5
|
||||
|
||||
|
I've based this on your earlier example...
Try something like this: Code:
public void printNumbers() {
for(int next = 0; next < lottoNumber.length-1; next++) {
System.out.println("Number " + next + ": " + lottoNumber [next]);
}
}
|
|
#6
|
|||
|
|||
|
Reply to print method
Quote:
do i not have to use a forloop? for the print method |
|
#7
|
|||
|
|||
|
Lotto program help required
My sort method is not working, code as follows. I also have a problem with the
amount of numbers being generated, I require 6 random numbers, but I am getting about 30 and some repeats too. Does anyone have any ideas please. // Method : Sort 6 numbers in ascending order public void sortNumbers() { for(int next = 0; next < lottoNumber.length-1; next++) for (int other = next +1; other < lottoNumber.length; other++) if(lottoNumber [next] > lottoNumber [other]){ int temp = lottoNumber [other]; lottoNumber [other] = lottoNumber [next]; lottoNumber [next] = temp; } |
![]() |
| Viewing: Dev Articles Community Forums > Programming > Java Development > Lucky Dip Lotto Ticket code!!!! |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|