Java 我做错了什么?

Java 我做错了什么?,java,Java,texfile的初始输出如下所示不允许使用数组列表或比较器: Steve Jobs 9 f 91 Bill Gates 6 m 90 James Gosling 3 m 100 James Gosling 3 f 100 Dennis Ritchie 5 m 94 Steve Jobs 9 m 95 Dennis Ritchie 5 f 100 Jeff Dean 7 m 100 Bill Gates 6 f 96 Jeff Dean 7 f 100 Sergey Brin 27 f 97 Se

texfile的初始输出如下所示不允许使用数组列表或比较器

Steve Jobs 9 f 91
Bill Gates 6 m 90
James Gosling 3 m 100
James Gosling 3 f 100
Dennis Ritchie 5 m 94
Steve Jobs 9 m 95
Dennis Ritchie 5 f 100
Jeff Dean 7 m 100
Bill Gates 6 f 96
Jeff Dean 7 f 100
Sergey Brin 27 f 97
Sergey Brin 22 m 98
r[2*position[exams[i].getID()]+1] = new Exam(r[i].getFirstName(), r[i].getLastName(), r[i].getID(), r[i].getExamType(), r[i].getScore());

import java.io.*;
import java.util.*;

class P2 {

    public static void main(String [] args) throws FileNotFoundException
    {

        Scanner data = new Scanner(new File("Exam.txt"));
        Exam[] readObjects = readAllExams(data);
        Exam[] collateObjects = collateExams(readObjects);

        System.out.println("1) Initially the list of exams of students is: ");
        System.out.println();
        for(int i = 0; i < readObjects.length; i++)
        {
            System.out.println(readObjects[i]);
        }
        System.out.println();
        System.out.println("Sorted list: ");
        for (int i = 0; i < collateObjects.length; i++)
        {
            System.out.println(collateObjects[i]);
        }
    }



    public static Exam[] readAllExams(Scanner s) throws ArrayIndexOutOfBoundsException
    {

        String firstName = "";
        String lastName = "";
        int ID = 0;
        String examType = "";
        char examTypeCasted;
        int score = 0;

        int index = 0;

        Exam[] object = new Exam[s.nextInt()];

        while(s.hasNext())
        {
            //Returns firtsName and lastName 
            firstName = s.next();
            lastName = s.next();

            //Returns ID number
            if(s.hasNextInt())
            {
                ID = s.nextInt();
            }
            else 
                s.next();

            //Returns examType which is 'M' or 'F'
            examType = s.next();
            examTypeCasted = examType.charAt(0);

            if(s.hasNextInt())
            {
                score = s.nextInt();
            }
            //Exam[] object = new Exam[s.nextInt()];

            object[index] = new Exam(firstName, lastName, ID, examTypeCasted, score);
            //System.out.println();
            index++;
        }
        readExam(s);
        return object;


    }

    public static Exam readExam(Scanner s)
    {
        String firstName = "";
        String lastName = "";
        int ID = 0;
        String examType = "";
        char examTypeCasted = 0;
        int score = 0;

        while (s.hasNext())
        {
            //Returns firtsName and lastName 
            firstName = s.next();
            lastName = s.next();

            //Returns ID number
            if(s.hasNextInt())
            {
                ID = s.nextInt();
            }
            //Returns examType which is 'M' or 'F'
            examType = s.next();
            examTypeCasted = examType.charAt(0);

            if(s.hasNextInt())
            {
                score = s.nextInt();
            }

        }
        Exam temp = new Exam(firstName, lastName, ID, examTypeCasted, score);
        return temp;
    }

    public static Exam[] collateExams(Exam[] exams)
    {

        Exam[] r = new Exam[exams.length]; 
        int [] position = new int[exams.length];
        int index = 0;

        for (int i = 0;  (i < exams.length) && (i < position.length); i++)
        {
            position[i] = -1;
            if(exams[i].getExamType()=='m')
               {
                   if(position[exams[i].getID()]==-1)
                   {
                       r[index*2] = new Exam(r[i].getFirstName(), r[i].getLastName(), r[i].getID(), r[i].getExamType(), r[i].getScore());

                       position[exams[i].getID()] = 2*index;

                   }
                   else
                       r[2*position[exams[i].getID()] - 1] = new Exam(r[i].getFirstName(), r[i].getLastName(), r[i].getID(), r[i].getExamType(), r[i].getScore());
               }
            else
               {
                   if(position[exams[i].getID()]==-1)
                   {
                       r[index*2+1] = new Exam(r[i].getFirstName(), r[i].getLastName(), r[i].getID(), r[i].getExamType(), r[i].getScore());
                       position[exams[i].getID()] = 2*index+1;
                   }

                  else    
                      r[2*position[exams[i].getID()]+1] = new Exam(r[i].getFirstName(), r[i].getLastName(), r[i].getID(), r[i].getExamType(), r[i].getScore());
               }
               index++;

            }

            return r;
    }
}
collateexamples方法对考试对象进行整理/排序,从第一个对象的第一个“m”(期中)开始,紧接着是同一个人的“f”(期末)。仅允许单循环构造。CollateTests()的输出应该是下面的输出,但我的代码不工作,即CollateTests方法不工作。smb能帮我吗?collateExames()的输出应为

Bill Gates 6 m 90
Bill Gates 6 f 96
James Gosling 3 m 100
James Gosling 3 f 100
Dennis Ritchie 5 m 94
Dennis Ritchie 5 f 100
Steve Jobs 9 m 95
Steve Jobs 9 f 91
Jeff Dean 7 m 100
Jeff Dean 7 f 100
Sergey Brin 22 m 98
Sergey Brin 27 f 97 
r[2*position[exams[i].getID()]+1] = new Exam(r[i].getFirstName(), r[i].getLastName(), r[i].getID(), r[i].getExamType(), r[i].getScore());

import java.io.*;
import java.util.*;

class P2 {

    public static void main(String [] args) throws FileNotFoundException
    {

        Scanner data = new Scanner(new File("Exam.txt"));
        Exam[] readObjects = readAllExams(data);
        Exam[] collateObjects = collateExams(readObjects);

        System.out.println("1) Initially the list of exams of students is: ");
        System.out.println();
        for(int i = 0; i < readObjects.length; i++)
        {
            System.out.println(readObjects[i]);
        }
        System.out.println();
        System.out.println("Sorted list: ");
        for (int i = 0; i < collateObjects.length; i++)
        {
            System.out.println(collateObjects[i]);
        }
    }



    public static Exam[] readAllExams(Scanner s) throws ArrayIndexOutOfBoundsException
    {

        String firstName = "";
        String lastName = "";
        int ID = 0;
        String examType = "";
        char examTypeCasted;
        int score = 0;

        int index = 0;

        Exam[] object = new Exam[s.nextInt()];

        while(s.hasNext())
        {
            //Returns firtsName and lastName 
            firstName = s.next();
            lastName = s.next();

            //Returns ID number
            if(s.hasNextInt())
            {
                ID = s.nextInt();
            }
            else 
                s.next();

            //Returns examType which is 'M' or 'F'
            examType = s.next();
            examTypeCasted = examType.charAt(0);

            if(s.hasNextInt())
            {
                score = s.nextInt();
            }
            //Exam[] object = new Exam[s.nextInt()];

            object[index] = new Exam(firstName, lastName, ID, examTypeCasted, score);
            //System.out.println();
            index++;
        }
        readExam(s);
        return object;


    }

    public static Exam readExam(Scanner s)
    {
        String firstName = "";
        String lastName = "";
        int ID = 0;
        String examType = "";
        char examTypeCasted = 0;
        int score = 0;

        while (s.hasNext())
        {
            //Returns firtsName and lastName 
            firstName = s.next();
            lastName = s.next();

            //Returns ID number
            if(s.hasNextInt())
            {
                ID = s.nextInt();
            }
            //Returns examType which is 'M' or 'F'
            examType = s.next();
            examTypeCasted = examType.charAt(0);

            if(s.hasNextInt())
            {
                score = s.nextInt();
            }

        }
        Exam temp = new Exam(firstName, lastName, ID, examTypeCasted, score);
        return temp;
    }

    public static Exam[] collateExams(Exam[] exams)
    {

        Exam[] r = new Exam[exams.length]; 
        int [] position = new int[exams.length];
        int index = 0;

        for (int i = 0;  (i < exams.length) && (i < position.length); i++)
        {
            position[i] = -1;
            if(exams[i].getExamType()=='m')
               {
                   if(position[exams[i].getID()]==-1)
                   {
                       r[index*2] = new Exam(r[i].getFirstName(), r[i].getLastName(), r[i].getID(), r[i].getExamType(), r[i].getScore());

                       position[exams[i].getID()] = 2*index;

                   }
                   else
                       r[2*position[exams[i].getID()] - 1] = new Exam(r[i].getFirstName(), r[i].getLastName(), r[i].getID(), r[i].getExamType(), r[i].getScore());
               }
            else
               {
                   if(position[exams[i].getID()]==-1)
                   {
                       r[index*2+1] = new Exam(r[i].getFirstName(), r[i].getLastName(), r[i].getID(), r[i].getExamType(), r[i].getScore());
                       position[exams[i].getID()] = 2*index+1;
                   }

                  else    
                      r[2*position[exams[i].getID()]+1] = new Exam(r[i].getFirstName(), r[i].getLastName(), r[i].getID(), r[i].getExamType(), r[i].getScore());
               }
               index++;

            }

            return r;
    }
}
我在

r[2*position[exams[i].getID()]+1] = new Exam(r[i].getFirstName(), r[i].getLastName(), r[i].getID(), r[i].getExamType(), r[i].getScore());

import java.io.*;
import java.util.*;

class P2 {

    public static void main(String [] args) throws FileNotFoundException
    {

        Scanner data = new Scanner(new File("Exam.txt"));
        Exam[] readObjects = readAllExams(data);
        Exam[] collateObjects = collateExams(readObjects);

        System.out.println("1) Initially the list of exams of students is: ");
        System.out.println();
        for(int i = 0; i < readObjects.length; i++)
        {
            System.out.println(readObjects[i]);
        }
        System.out.println();
        System.out.println("Sorted list: ");
        for (int i = 0; i < collateObjects.length; i++)
        {
            System.out.println(collateObjects[i]);
        }
    }



    public static Exam[] readAllExams(Scanner s) throws ArrayIndexOutOfBoundsException
    {

        String firstName = "";
        String lastName = "";
        int ID = 0;
        String examType = "";
        char examTypeCasted;
        int score = 0;

        int index = 0;

        Exam[] object = new Exam[s.nextInt()];

        while(s.hasNext())
        {
            //Returns firtsName and lastName 
            firstName = s.next();
            lastName = s.next();

            //Returns ID number
            if(s.hasNextInt())
            {
                ID = s.nextInt();
            }
            else 
                s.next();

            //Returns examType which is 'M' or 'F'
            examType = s.next();
            examTypeCasted = examType.charAt(0);

            if(s.hasNextInt())
            {
                score = s.nextInt();
            }
            //Exam[] object = new Exam[s.nextInt()];

            object[index] = new Exam(firstName, lastName, ID, examTypeCasted, score);
            //System.out.println();
            index++;
        }
        readExam(s);
        return object;


    }

    public static Exam readExam(Scanner s)
    {
        String firstName = "";
        String lastName = "";
        int ID = 0;
        String examType = "";
        char examTypeCasted = 0;
        int score = 0;

        while (s.hasNext())
        {
            //Returns firtsName and lastName 
            firstName = s.next();
            lastName = s.next();

            //Returns ID number
            if(s.hasNextInt())
            {
                ID = s.nextInt();
            }
            //Returns examType which is 'M' or 'F'
            examType = s.next();
            examTypeCasted = examType.charAt(0);

            if(s.hasNextInt())
            {
                score = s.nextInt();
            }

        }
        Exam temp = new Exam(firstName, lastName, ID, examTypeCasted, score);
        return temp;
    }

    public static Exam[] collateExams(Exam[] exams)
    {

        Exam[] r = new Exam[exams.length]; 
        int [] position = new int[exams.length];
        int index = 0;

        for (int i = 0;  (i < exams.length) && (i < position.length); i++)
        {
            position[i] = -1;
            if(exams[i].getExamType()=='m')
               {
                   if(position[exams[i].getID()]==-1)
                   {
                       r[index*2] = new Exam(r[i].getFirstName(), r[i].getLastName(), r[i].getID(), r[i].getExamType(), r[i].getScore());

                       position[exams[i].getID()] = 2*index;

                   }
                   else
                       r[2*position[exams[i].getID()] - 1] = new Exam(r[i].getFirstName(), r[i].getLastName(), r[i].getID(), r[i].getExamType(), r[i].getScore());
               }
            else
               {
                   if(position[exams[i].getID()]==-1)
                   {
                       r[index*2+1] = new Exam(r[i].getFirstName(), r[i].getLastName(), r[i].getID(), r[i].getExamType(), r[i].getScore());
                       position[exams[i].getID()] = 2*index+1;
                   }

                  else    
                      r[2*position[exams[i].getID()]+1] = new Exam(r[i].getFirstName(), r[i].getLastName(), r[i].getID(), r[i].getExamType(), r[i].getScore());
               }
               index++;

            }

            return r;
    }
}
r[2*位置[tests[i].getID()]+1]=新考试(r[i].getFirstName(),r[i].getLastName(),r[i].getID(),r[i].getExamType(),r[i].getScore();
导入java.io.*;
导入java.util.*;
P2类{
公共静态void main(字符串[]args)引发FileNotFoundException
{
扫描仪数据=新扫描仪(新文件(“Exam.txt”);
检查[]readObjects=readAllExams(数据);
检查[]collateObjects=检查(readObjects);
最初,学生的考试列表是:);
System.out.println();
for(int i=0;i
这是绝对和完整的kluge,但它符合您的标准。如果这对您不起作用,那么您必须考虑对象创建或其他适合您需要的东西

r[2*position[exams[i].getID()]+1] = new Exam(r[i].getFirstName(), r[i].getLastName(), r[i].getID(), r[i].getExamType(), r[i].getScore());

import java.io.*;
import java.util.*;

class P2 {

    public static void main(String [] args) throws FileNotFoundException
    {

        Scanner data = new Scanner(new File("Exam.txt"));
        Exam[] readObjects = readAllExams(data);
        Exam[] collateObjects = collateExams(readObjects);

        System.out.println("1) Initially the list of exams of students is: ");
        System.out.println();
        for(int i = 0; i < readObjects.length; i++)
        {
            System.out.println(readObjects[i]);
        }
        System.out.println();
        System.out.println("Sorted list: ");
        for (int i = 0; i < collateObjects.length; i++)
        {
            System.out.println(collateObjects[i]);
        }
    }



    public static Exam[] readAllExams(Scanner s) throws ArrayIndexOutOfBoundsException
    {

        String firstName = "";
        String lastName = "";
        int ID = 0;
        String examType = "";
        char examTypeCasted;
        int score = 0;

        int index = 0;

        Exam[] object = new Exam[s.nextInt()];

        while(s.hasNext())
        {
            //Returns firtsName and lastName 
            firstName = s.next();
            lastName = s.next();

            //Returns ID number
            if(s.hasNextInt())
            {
                ID = s.nextInt();
            }
            else 
                s.next();

            //Returns examType which is 'M' or 'F'
            examType = s.next();
            examTypeCasted = examType.charAt(0);

            if(s.hasNextInt())
            {
                score = s.nextInt();
            }
            //Exam[] object = new Exam[s.nextInt()];

            object[index] = new Exam(firstName, lastName, ID, examTypeCasted, score);
            //System.out.println();
            index++;
        }
        readExam(s);
        return object;


    }

    public static Exam readExam(Scanner s)
    {
        String firstName = "";
        String lastName = "";
        int ID = 0;
        String examType = "";
        char examTypeCasted = 0;
        int score = 0;

        while (s.hasNext())
        {
            //Returns firtsName and lastName 
            firstName = s.next();
            lastName = s.next();

            //Returns ID number
            if(s.hasNextInt())
            {
                ID = s.nextInt();
            }
            //Returns examType which is 'M' or 'F'
            examType = s.next();
            examTypeCasted = examType.charAt(0);

            if(s.hasNextInt())
            {
                score = s.nextInt();
            }

        }
        Exam temp = new Exam(firstName, lastName, ID, examTypeCasted, score);
        return temp;
    }

    public static Exam[] collateExams(Exam[] exams)
    {

        Exam[] r = new Exam[exams.length]; 
        int [] position = new int[exams.length];
        int index = 0;

        for (int i = 0;  (i < exams.length) && (i < position.length); i++)
        {
            position[i] = -1;
            if(exams[i].getExamType()=='m')
               {
                   if(position[exams[i].getID()]==-1)
                   {
                       r[index*2] = new Exam(r[i].getFirstName(), r[i].getLastName(), r[i].getID(), r[i].getExamType(), r[i].getScore());

                       position[exams[i].getID()] = 2*index;

                   }
                   else
                       r[2*position[exams[i].getID()] - 1] = new Exam(r[i].getFirstName(), r[i].getLastName(), r[i].getID(), r[i].getExamType(), r[i].getScore());
               }
            else
               {
                   if(position[exams[i].getID()]==-1)
                   {
                       r[index*2+1] = new Exam(r[i].getFirstName(), r[i].getLastName(), r[i].getID(), r[i].getExamType(), r[i].getScore());
                       position[exams[i].getID()] = 2*index+1;
                   }

                  else    
                      r[2*position[exams[i].getID()]+1] = new Exam(r[i].getFirstName(), r[i].getLastName(), r[i].getID(), r[i].getExamType(), r[i].getScore());
               }
               index++;

            }

            return r;
    }
}
static int maxId = 0;

public static void main(String [] args) throws FileNotFoundException
{

    Scanner data = new Scanner(Answer28522397.class.getClassLoader().getResourceAsStream("exams.txt"));
    Exam[] readObjects = readAllExams(data);

    System.out.println("1) Initially the list of exams of students is: ");
    System.out.println();
    for(int i = 0; i < readObjects.length; i++)
    {
        System.out.println(readObjects[i]);
    }

    Object[][] collatedObjects = collateExams(readObjects);

    System.out.println("");
    System.out.println("Sorted list: ");
    for (int i = 0; i < collatedObjects.length; i++)
    {
        if (collatedObjects[i][0] != null && collatedObjects[i][1] != null)
        {
            System.out.println(collatedObjects[i][0]);
            System.out.println(collatedObjects[i][1]);
        }
    }
}

public static Exam[] readAllExams(Scanner s) throws ArrayIndexOutOfBoundsException
{
    int index = 0;
    Exam[] object = new Exam[s.nextInt()];

    while(s.hasNext())
    {
        Exam e = readExam(s);
        maxId = e.getID() > maxId ? e.getID() : maxId;
        object[index] = e;
        index++;
    }

    return object;
}

public static Exam readExam(Scanner s)
{
    String firstName = "";
    String lastName = "";
    int ID = 0;
    String examType = "";
    char examTypeCasted = 0;
    int score = 0;

    //Returns firtsName and lastName 
    firstName = s.next();
    lastName = s.next();

    //Returns ID number
    if(s.hasNextInt())
    {
        ID = s.nextInt();
    }
    //Returns examType which is 'M' or 'F'
    examType = s.next();
    examTypeCasted = examType.charAt(0);

    if(s.hasNextInt())
    {
        score = s.nextInt();
    }

    return new Exam(firstName, lastName, ID, examTypeCasted, score);
}

public static Object[][] collateExams(Exam[] exams)
{
    Exam [] r = new Exam[exams.length];
    System.arraycopy(exams, 0, r, 0, exams.length); 


    Object[][] collation = new Object[maxId+1][2];

    for(Exam exam : exams)
    {
       if (exam.getExamType() == 'm')
       {
           collation[exam.getID()][0] = exam;
       }
       else
       {
           collation[exam.getID()][1] = exam;
       }           
    }

    return collation;
}
static int maxId=0;
公共静态void main(字符串[]args)引发FileNotFoundException
{
扫描仪数据=新扫描仪(answer2852397.class.getClassLoader().getResourceAsStream(“exams.txt”);
检查[]readObjects=readAllExams(数据);
最初,学生的考试列表是:);
System.out.println();
for(int i=0;i