Sailboat Race Ranking
Programming Challenge 7 in Chapter 3 asked yo uto create an application that tracks the performance of sailboats in five races. The version of the application only calculated the total points for each boat. In this new version, you are asked to rank the boats, assigning them first, second, and third place. Also, you will need to perform the following input validations:
1. All input value must be valid integers.
2. In any column (a single race), the three integers must add up to 6 (1+2+3, in any order). We assume there are no tie scores.
When one of these input validations fails, display an appropriate message in the status bar. You do not need to customize the message for each input field, but you must explain the nauture of the error.
Comments and output attached
import java.util.Scanner;
public class InpuRace {
public static void main(String args[]){
//scanner object to read user input
Scanner sc = new Scanner(System.in);
// declaring array
int[][] scores = new int[5][3];
//reading inputs from user
for(int i=0;i<5;i++){ //for five races
System.out.println("Enter ranks for "+(i+1) +" race: ");
for(int j=0;j<3;j++) {
System.out.println("Enter rank for "+(j+1) +" boat: ");
int boatScore = sc.nextInt();
//validating score
if(boatScore <= 0 || boatScore > 3) {
System.out.println("You entered wrong score, please enter correct
data");
j--;
}
else{
scores[i][j]= boatScore;
}
}
//validating race details
if((scores[i][0] + scores[i][1] + scores[i][2]) != 6){
System.out.println("You entered wrong score for this race, please
enter correct data again");
i--;
}
}
}
}
OUTPUT
Get Answers For Free
Most questions answered within 1 hours.