programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Jeanne Boyarsky
• Ron McLeod
• Paul Clapham
• Liutauras Vilda
Sheriffs:
• paul wheaton
• Rob Spoor
• Devaka Cooray
Saloon Keepers:
• Stephan van Hulst
• Tim Holloway
• Carey Brown
• Frits Walraven
• Tim Moores
Bartenders:
• Mikalai Zaikin

# Array Problem, Diving Scores

Greenhorn
Posts: 17
• Number of slices to send:
Optional 'thank-you' note:
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 =(

Bartender
Posts: 6109
6
• Number of slices to send:
Optional 'thank-you' note:
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.

Bartender
Posts: 563
• Number of slices to send:
Optional 'thank-you' note:

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
• Number of slices to send:
Optional 'thank-you' note:
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...

Greenhorn
Posts: 24
• Number of slices to send:
Optional 'thank-you' note:
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
/*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
• Number of slices to send:
Optional 'thank-you' note:
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 +
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
/*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(" ");
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.");
}

}

}

 Not so fast naughty spawn! I want you to know about a bit of art, as a gift, the permaculture playing cards https://gardener-gift.com