Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
数据不存储在JAVA中的文件中_Java - Fatal编程技术网

数据不存储在JAVA中的文件中

数据不存储在JAVA中的文件中,java,Java,我试图将学生的姓名和CGPA存储在一个文件中,但它只存储姓名,有人能帮我吗? 这是我的密码 class Marks { public static void main(String[] args) { 以下是计算CGPA的主要方法 ArrayList arrayList = new ArrayList(); Scanner ed = new Scanner(System.in); Scanner sc=new Scanner(System.in); Syste

我试图将学生的姓名和CGPA存储在一个文件中,但它只存储姓名,有人能帮我吗? 这是我的密码

class Marks {
public static void main(String[] args) {

以下是计算CGPA的主要方法

    ArrayList arrayList = new ArrayList();
    Scanner ed = new Scanner(System.in);
    Scanner sc=new Scanner(System.in);
    System.out.println("Enter the name of Students");
    arrayList.add(sc.next());
    System.out.println("Enter number of subjects");
    int n=sc.nextInt();
    double[] marks=new double[n];
    System.out.println("Enter marks of Sub1\t sub2\t sub3\t sub4\t");
    for(int i=0;i<n;i++)
    {
        marks[i]=sc.nextInt();
    }
    double grade[]=new double[n];
    double cgpa,sum=0;
    for(int i=0;i<n;i++)
    {
        grade[i]=(marks[i]/10) ;
    }
    for(int i=0;i<n;i++)
    {
        sum+=grade[i];
    }
    cgpa=sum/n;
    System.out.println("cgpa="+cgpa);
    System.out.println("percentage from cgpa="+cgpa*9.5);

}

我认为您在计算后没有在arraylist中添加CGPA。因此,当您在文件中写入时,只会写入名称。

您需要捕获计算出的CGPA值。考虑使用学生姓名作为关键的地图。

 Map<String,Double> cgpaMap = new HashMap<>();
 List<String> students = new ArrayList<>();
 ... other setup ...

 student = .. get student from input ..
 students.add(student);
 ... other input ...

 cgpa = .. calculate the CGPA ..
 cgpaMap.put(student, cgpa);

您只需调用
arrayList.add
一次即可添加名称。因此,当您保存arraylist时,它只包含名称,这对您来说并不奇怪。只需查看您自己的代码,并计算您看到
arrayList的次数。添加
您不应使用原始arrayList。此外:
 Map<String,Double> cgpaMap = new HashMap<>();
 List<String> students = new ArrayList<>();
 ... other setup ...

 student = .. get student from input ..
 students.add(student);
 ... other input ...

 cgpa = .. calculate the CGPA ..
 cgpaMap.put(student, cgpa);
 for (Student student : students) {
   fileWriter.write(String.format("%s %f%n", student, cgpaMap.get(student)));
 }