• Post Reply Bookmark Topic Watch Topic
  • New Topic

Array Problem, Diving Scores  RSS feed

 
Collin Sampson
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a problem in my beginning java class..

There are seven judges scores on each dive and a difficulty for the dive ranging from 1.2 to 3.8. To determine the final score, the lowest score and highest score are eliminated, then multiplied by the difficulty, then multiplied by .6.

(5 scores) * (difficulty) * .6 = final score

Here's my code, I can't tell why its not working. First time using an array.



It is compiling and running, but the calculations are wrong. I ran the scores...
15, 1, 10, 10, 10, 10, 10
And a difficulty... 1.5
Which should be (50) * (1.5) * .6 = final score of 45

Instead it returns... final score of 67.5 =(
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Show us where you're eliminating the highest and lowest score.

Also, I suggest you print out scores[index] and highest and lowest right after this block in your loop:


And probably print out the whole scores array and highest and lowest right after the loop completes, so you can see where your code is not doing what you probably think it's doing.
 
Greg Brannon
Bartender
Posts: 563
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I added some test code,

System.out.println( "total - highest - lowest = " + (sum - highest - lowest ) );

that prints before your "Diver's final score is: " line, and I get the answer 36.

Does that give you a hint? This line of test code is an example of the kinds of things you can do to troubleshoot your code yourself. There are more (and less) elegant ways to do the same thing, but in a program this size, a print statement here and there can tell volumes.
 
Collin Sampson
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay I put in some test code and checked the highest and lowest values after each loop. It sets 15 to highest and lowest as it should but when I input 1 for the next value, it still has 15 as both the highest and lowest values. It is not executing the if statements within the loop for index = 1. I don't understand...
 
Sccot Smith
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
for (index =1; index < scores.length; index++)
{

scores[index] = keyboard.nextDouble( );

if (scores[index] >= 0)
{

//like follows
sum = sum + scores[index];
highest=highest<scores[index]?scores[index]:scores[0];
lowest=lowest>scores[index]?scores[index]:scores[0];
//Notice here
//your if statement is wrong!!!
/*if (scores[index] > highest)
{
scores[index] = highest;
}
if (scores[index] < lowest)
{
scores[index] = lowest;
} */
}
else
{
System.out.println("Error: negative number entered.");
System.out.println(" ");
}


}
 
Sccot Smith
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
package com.cn;
import java.util.*;

public class DiveScoringProgram
{
public static void main(String[] args)
{
int index = 0;
//double next;
double highest,lowest;
//double input;
double difficulty = 0;
double sum = 0;

double finalScore;

double[] scores = new double[7];
System.out.println("Enter the " + scores.length +
" judges' scores please.");
System.out.println(" ");

Scanner keyboard = new Scanner(System.in);

scores[0] = keyboard.nextDouble( );
sum = sum + scores[0];
highest = scores[0];
lowest = scores[0];
for (index =1; index < scores.length; index++)
{

scores[index] = keyboard.nextDouble( );

if (scores[index] >= 0)
{

//like follows
sum = sum + scores[index];
highest=highest<scores[index]?scores[index]:scores[0];
lowest=lowest>scores[index]?scores[index]:scores[0];
//Notice here
//your if statement is wrong!!!
/*if (scores[index] > highest)
{
scores[index] = highest;
}
if (scores[index] < lowest)
{
scores[index] = lowest;
} */
}
else
{
System.out.println("Error: negative number entered.");
System.out.println(" ");
}


}

System.out.println(" ");
System.out.println("Enter degree of difficulty please.");
difficulty = keyboard.nextDouble( );
System.out.println(" ");
if ((difficulty >= 1.2) && (difficulty <= 3.8))
{
finalScore = ((sum - highest - lowest) * difficulty * 6.0) / 10.0;
System.out.println("Diver's final score is: ");
System.out.println(finalScore);
}
else
{
System.out.println("Error: degree of difficulty must " +
"be between 1.2 and 3.8.");
}

}

}
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!