Мне нужно выяснить, сколько раз число 50 встречается в этом массиве, который был создан из чисел в текстовом файле, а затем вывести, сколько учеников набрало 50 баллов, но я, кажется, застрял в бесконечном l oop в конце? Я должен использовать массивы, а также методы. Любая помощь будет оценена, спасибо !!
import javax.swing.JOptionPane;
import java.util.Scanner;
import java.io.*;
public class TestScores
{
public static void main(String[] args) throws IOException
{
int count=0;
int number;
//Open the file
File file=new File("scores.txt");
Scanner inputFile=new Scanner(file);
//Count the number of elements in the file
while(inputFile.hasNext())
{
number=inputFile.nextInt();
count++;
}
//Close the file
inputFile.close();
//Create an array
int[] numbers=new int[count];
//Display the number of elements in the array
JOptionPane.showMessageDialog(null, count);
//Pass the array to the loadData method
loadData(numbers);
//Pass the array to the perfectScore method
perfectScore(numbers);
}
/**
The loadData method loads the data from the file into the array
@param array A reference to the array
*/
public static void loadData(int[] array) throws IOException
{
int index=0;
//Open the file
File file=new File("scores.txt");
Scanner inputFile=new Scanner(file);
//load the data from the file into the array
while(inputFile.hasNext() && index<array.length)
{
array[index]=inputFile.nextInt();
index++;
}
}
/**
The perfectScore method determines how many students
got a 50 on the exam.
@param array A reference to the array
*/
public static void perfectScore(int[] array)
{
for(int index=0; index<array.length; index++)
{
if(array[index]==50)
{
index++;
int perfect=array[index];
JOptionPane.showMessageDialog(null, "The number of students " +
"who got a perfect 50 score is: "+ perfect + " students.");
}
}
}
}