-
1st Java program
I am taking my 1st Java course this semester, and an assignment I have been given is giving me fits. Here is what I am trying to do, and what I have so far. I don't expect a full solution to the entire problem, but I am stuck part way through it, and an explanation/solution to what I am currently doing wrong would be appreciated.
I am creating a lottery program that will compare 2 arrays of 6 numbers each, 1 inputted from the user, and another randomly generated. Numbers should be 1-40. I have to provide ways to prompt for invalid inputs, but I'm not there yet...
I was able to create both arrays, and get them to successfully output to the screen. Now I have to display the arrays in sorted order, and that is where i am stuck.
Below is my program so far... I apologize if this displays strangely, this is my first post on here, and I don't know if it will auto format to display the code correctly.
Code:
import java.util.*;
public class InstantLottery
{
static Scanner console = new Scanner(System.in);
public static void main(String[] args)
{
int sortedOrder; //trying to initialize something to call from a method
int[] playerNumbers = new int[6]; //creates the player array of 6 integers
for (int i=0; i < playerNumbers.length; i++)
{
System.out.print("Enter a number from 1-40 >");
playerNumbers[i] = console.nextInt();
}
System.out.println("the numbers you entered are");//displays array
sortedOrder = LotteryMethods.selectionSort(playerNumbers);//attempt to call the sort method...
for (int i=0; i<playerNumbers.length; i++)//displays array
System.out.print(playerNumbers[i] +" ");//displays array
Random randNumGenerator = new Random();
int[] computerNumbers = new int[6]; //creates the computer array of 6 integers
for (int i=0; i < computerNumbers.length; i++)
{
computerNumbers[i] = (randNumGenerator.nextInt(40)+1);
}
System.out.println("the numbers the computer picked are");//displays computer array
for (int i=0; i<computerNumbers.length; i++)//displays array
System.out.print(computerNumbers[i] +" ");//displays array
}
//lottery methods
public class LotteryMethods
{
public static void selectionSort(int[] list, int listLength)
{
int index;
int smallestIndex;
int minIndex;
int temp;
for (index=0; index<listLength-1; index++)
{
smallestIndex=index;
for(minIndex=index+1; minIndex<listLength; minIndex++)
if (list[minIndex]<list[smallestIndex]) smallestIndex=minIndex;
temp=list[smallestIndex];
list[smallestIndex]=list[index];
list[index]=temp;
}
}
}
}
-
You have a problem here:
Code:
if (list[minIndex]<list[smallestIndex]) smallestIndex=minIndex;
That should be
Code:
if (list[minIndex]<list[smallestIndex])
{
smallestIndex=minIndex;
And that will make the rest of your } braces come out right. Also, you don't need the listLength parameter in your selectionSort method, as list will happily tell you what its length is.
Beyond that, you should write a method to list an array of numbers, as you need to do that multiple times.
Code:
public void listNumbers(int[] list) {
// TODO write this part
}
-Gary-
-
thank you
thank you, that was a big help. the brackets were causing the problem. I changed it and it works now. off to finish the rest of it. Thanks again.