在不同类中比较同一类整数的Java代码问题求助
Let's break down and fix the two errors you're facing, plus correct the logic of the addResult method to align with your overall program structure:
1. Error: "result cannot be resolved to a variable"
In your Event.addResult method, you're referencing a variable result that hasn't been declared or initialized. Looking at your Result class, each Result object has a double[] result array to store attempt results. You need to first retrieve the existing Result instance from your resultList, then access its result array directly.
2. Error: "The method maxAttempts() is undefined for the type Event"
Your Event class doesn't have a maxAttempts() method, but the maximum allowed attempts for an event is stored in the numberOfTries field of Event. Alternatively, each Result object also has a maxAttempts field that matches the event's limit. You should use either of these values instead of trying to call an undefined method.
Corrected Event Class Code
Here's the fixed version of the Event class with clear explanations of key changes:
import java.util.ArrayList; public class Event { private String name; private int numberOfTries; private ArrayList<Result> resultList = new ArrayList<Result>(); public Event(String name, int numberOfTries) { this.name = name; this.numberOfTries = numberOfTries; } public String getName() { return name; } public int getNumberOfTries() { return numberOfTries; } public String toString() { return name + ", " + numberOfTries; } // Updated method to accept Participant and new result value (matches your SportsCompetition flow) public void addResult(Participant participant, double newResult) { int participantIndex = -1; // Find if the participant already has a Result entry in this event for (int i = 0; i < resultList.size(); i++) { if (resultList.get(i).getParticipant().getStartNumber() == participant.getStartNumber()) { participantIndex = i; break; } } if (participantIndex != -1) { // Participant already has a Result: add the new attempt if they haven't used all tries Result existingResult = resultList.get(participantIndex); if (existingResult.getAttemptsUsed() < existingResult.getMaxAttempts()) { // Add the new result to the next available slot in the result array existingResult.result[existingResult.getAttemptsUsed()] = newResult; // Increment attempts used after adding the result existingResult.attemptsUsed++; } else { System.out.println("Error: Participant has used all allowed attempts for this event."); } } else { // No existing Result: create a new entry and add to the list Result newResultEntry = new Result(newResult, this.numberOfTries, participant); resultList.add(newResultEntry); } } }
Additional Bug Fix in Result Class
You also have a small oversight in the Result class: the attemptsUsed field isn't initialized to 0, so the attemptsUsed++ in the constructor won't work correctly. Update the Result class to fix this:
public class Result { public double[] result; public Participant participant; public int maxAttempts; // Initialize attemptsUsed to 0 to ensure correct incrementing public int attemptsUsed = 0; public Result(double result, int noOfAttempts, Participant p) { this.result = new double[noOfAttempts]; this.result[0] = result; this.participant = p; this.maxAttempts = noOfAttempts; // Now correctly sets to 1 after adding the first result attemptsUsed++; } // Existing getter methods remain unchanged public double[] getResult() { return result; } public Participant getParticipant() { return participant; } public int getAttemptsUsed() { return attemptsUsed; } public int getMaxAttempts() { return maxAttempts; } }
Final Adjustment in SportsCompetition
To tie it all together, update the addResult method in SportsCompetition to call our fixed Event.addResult method:
// ... existing code in SportsCompetition.addResult ... System.out.println("Results for " + partResult.getFirstName() + " " + partResult.getLastName() + " from " + partResult.getTeam() + " in " + eventResult.getName() + ": "); double result = scan.nextDouble(); // Use double to match Result's result array type scan.nextLine(); // Call the corrected addResult method eventResult.addResult(partResult, result); System.out.println("Result added successfully!");
内容的提问来源于stack exchange,提问作者Zakan

