6 changed files with 46 additions and 0 deletions
@ -0,0 +1,15 @@ |
|||||
|
public class ScoreCalculateorBefore { |
||||
|
public static void main(String[] args) { |
||||
|
int sum = 0; |
||||
|
int count = 0; |
||||
|
BufferedReader br = new BufferedReader(new FileReader("scores.txt")); |
||||
|
String line; |
||||
|
while ((line = br.readLine()) != null) { |
||||
|
sum += Integer.parseInt(line); |
||||
|
count++; |
||||
|
} |
||||
|
br.close(); |
||||
|
double average = (double) sum / count; |
||||
|
System.out.println("Average score: " + average); |
||||
|
} |
||||
|
} |
||||
Binary file not shown.
@ -0,0 +1,31 @@ |
|||||
|
import java.io.BufferedReader; |
||||
|
import java.io.FileReader; |
||||
|
import java.io.IOException; |
||||
|
|
||||
|
public class ScoreCalculator { |
||||
|
public static void main(String[] args) { |
||||
|
int sum = 0; |
||||
|
int count = 0; |
||||
|
|
||||
|
try (BufferedReader br = new BufferedReader(new FileReader("scores.txt"))) { |
||||
|
String line; |
||||
|
while ((line = br.readLine()) != null) { |
||||
|
try { |
||||
|
sum += Integer.parseInt(line); |
||||
|
count++; |
||||
|
} catch (NumberFormatException e) { |
||||
|
System.err.println("Invalid number format: " + line); |
||||
|
} |
||||
|
} |
||||
|
} catch (IOException e) { |
||||
|
System.err.println("Error reading file: " + e.getMessage()); |
||||
|
} |
||||
|
|
||||
|
if (count > 0) { |
||||
|
double average = (double) sum / count; |
||||
|
System.out.println("Average score: " + average); |
||||
|
} else { |
||||
|
System.out.println("No valid scores found."); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
Binary file not shown.
|
After Width: | Height: | Size: 74 KiB |
|
After Width: | Height: | Size: 85 KiB |
Loading…
Reference in new issue