import javax.swing.*;
public class Golf
{
public static void main(String args[]) throws Exception
{
// Declare variables.
String golferName; // Golfer's name.
String ageString; // String version of golfer's age.
int golferAge =0; // Golfer's age.
String scoreString; // String version of score.
int score; // Golfer's score.
int numRows = 5; // Number of rows in 2D array.
int numColumns = 9; // Number of columns in 2D array.
int numHoles = 9; // Number of holes.
int scores[][] = new int[numRows][numColumns]; // 2D array.
int golferScores[] = new int[numHoles]; // Store nine scores.
int i; // Array index for golferScores array.
int hole; // Hole number.
int row; // Row number in 2D array.
// Use assignment statements to populate the 2D array here.
scores[0][0] = 8;
scores[0][1] = 8;
scores[0][2] = 9;
scores[0][3] = 7;
scores[0][4] = 5;
scores[0][5] = 7;
scores[0][6] = 8;
scores[0][7] = 5;
scores[0][8] = 8;
scores[1][0] = 7;
scores[1][1] = 7;
scores[1][2] = 8;
scores[1][3] = 6;
scores[1][4] = 5;
scores[1][5] = 6;
scores[1][6] = 7;
scores[1][7] = 5;
scores[1][8] = 6;
scores[2][0] = 6;
scores[2][1] = 5;
scores[2][2] = 6;
scores[2][3] = 5;
scores[2][4] = 4;
scores[2][5] = 5;
scores[2][6] = 5;
scores[2][7] = 4;
scores[2][8] = 5;
scores[3][0] = 5;
scores[3][1] = 4;
scores[3][2] = 4;
scores[3][3] = 4;
scores[3][4] = 3;
scores[3][5] = 4;
scores[3][6] = 3;
scores[3][7] = 3;
scores[3][8] = 4;
scores[4][0] = 4;
scores[4][1] = 3;
scores[4][2] = 3;
scores[4][3] = 3;
scores[4][4] = 2;
scores[4][5] = 3;
scores[4][6] = 2;
scores[4][7] = 3;
scores[4][8] = 3;
// Get user input.
if((golferName = JOptionPane.showInputDialog("Enter golfer's name: ")) != null)
{
ageString = JOptionPane.showInputDialog("Enter golfer's age: ");
golferAge = Integer.parseInt(ageString);
for(i = 0; i < numHoles; i++)
{
hole = i+1;
scoreString = JOptionPane.showInputDialog("Enter score " + hole);
golferScores[i] = Integer.parseInt(scoreString);
}
}
// Print golfer's name and age here.
System.out.println("Golfer's Name: " + golferName);
System.out.println("Age: " + golferAge);
// Write for loop for each of 9 holes here.
hole = 0;
while(hole < 9)
{
// Calculate row in 2D array based on golfer's age.
if(golferAge <= 4)
numRows = 0;
else if(golferAge <= 7)
numRows = 1;
else if(golferAge <= 11)
numRows = 2;
else if(golferAge <= 15)
numRows = 3;
else
numRows = 4;
if(golferScores[numHoles]< scores[numRows][numColumns])
System.out.println("Hole" + hole + "Score" + golferScores[numHoles - 1] + "Under Par");
else if(golferScores[numHoles] == scores[numRows][numColumns])
System.out.println("Hole" + hole + "Score" + golferScores[numHoles - 1] + "Par");
else
System.out.println("Hole" + hole + "Score" + golferScores[numHoles - 1] + "Over Par");
hole++;
}
// Print score and decide which phrase to print for each of the nine holes here.
// Exit the program.
System.exit(0);
} // End of main() method.
} // End of Golf class.
It's suppose to get the scores from the user, then print out the golder name, age, scores and if it's under or over par depending on the hole par and golfer age. I've got the arrays set from the table in the book, using 1-1 as 0-0. However, it still kicks out the out of bounds error. I'm stumped here.
Thanks!

New Topic/Question
Reply




MultiQuote






|