-4

学生の平均を保存するには、double ベクトルを使用する必要があります。

読み取り元のファイルは次のように設定されています。

2  //num of students
60   //total possible score
John   //name
4 16 9 7 10  //scores

したがって、文字列を double に変換し、行からすべての int を追加し、平均に分割してから、平均を double ベクトルに格納する必要があります。

私がこれまでに持っているコードは次のとおりです。

public static String line;
public static Scanner in = new Scanner(System.in);
public static void main(String[] args) {


    System.out.println("enter the name of your file");
    String filename = in.next();
    FileIn file = new FileIn(filename);

    String firstLine; // String to hold first line which is number of students total in file.
    String secondLine; //String to hold second line which is number of points available 


    ArrayList<String> students = new ArrayList<String>();  // holds the students names

    //reads first line of the file. sets that number as the number of students
    firstLine = file.read();
    int numStu = Integer.parseInt(firstLine);
    // Just to test that number is being read correctly.
    System.out.println(numStu + " Number of students");

    //reads the second line of the file. sets that number as the total possible points in a semester
    secondLine = file.read();
    int totalPoints = Integer.parseInt(secondLine);
    // Just to test that number is being read correctly.
    System.out.println(totalPoints + " Total possible points");


    double avg = 0;
    double[]vector = new double [numStu]; 

    readFile(students,numStu,file,vector, avg);

    System.out.println(students);
    System.out.println(vector);
}

//puts the names into an arraylist and scores into a double vector
public static void readFile(ArrayList<String> students,int numStu, FileIn file, double[]vector, double avg)
{
    for(int k=0; k<(numStu*2); k++)
    {
                    //odd numbers are the students
        if (k % 2 == 0)
            students.add(file.read());

        else
        {
            //code to read and add the numbers from one line together, and storing the added and averaged score
        }
    }
}

}

ご覧のとおり、ファイル要素を二重ベクトルに分散している下部がありません

私の FileIn クラスは次のようになります。

private String myFileName;
private BufferedReader myFile;
public FileIn(String filename)
{
    myFileName = filename;
    try
    {
        myFile = new BufferedReader(new FileReader(myFileName));
    }
    catch(Exception e)
    {
        e.printStackTrace();
    }
    finally{}
}

public String read()
{
    String myLine = new String();
    try
    {
        myLine = myFile.readLine();
    }
    catch(Exception e)
    {
        e.printStackTrace();
    }
    finally{}
    return myLine;
}

}

4

1 に答える 1

0

まず!あなたの質問の下で私のコメントを読んでください。よりスマートな方法で質問する方法を学び、問題を分割統治する方法を学びます。

プログラマーはそれを自分で解決し、問題で使用する方法を学ぶことができるはずなので、これはあなたの質問に対する直接的な答えではありません。

次のコードを完成させると、完成した自分の質問に対する答えがわかります。

public class Foo {
  public static int main(String[] args) {
    String input = "10 15 20.5 70 40";

    // write code to convert the input string to doubles and sum them up
    double sum = ......;

    // answer should equals to 155.5
  }
}

ヒント:String.split()Double.parseDouble()、およびforループを利用する

于 2013-01-14T02:10:45.423 に答える