2014-01-18 239 views
-2

我有一个功课,经过很多努力后,我没有成功解决它,所以我问它在这里,问题是:我们有一个学生名单和他们的成绩在一个字符串中。学号,名字,姓氏,课程名称,年级,然后用“;”分隔然后另一个学生的记录,所以字符串是这样的:需要关于java编程的帮助

75414133,Mehdi,Javan,Math,16; 88887777,Shadi,Khani,英语,18; 75414133,Mehdi,Javan,英语,12; 88887777,Shadi,Khani,Physiques,16; 512345678,Mohamad,Sayadi,Chemistry,15; 88887777,Shadi,Khani,Math,15;

我们需要一个java程序来计算等级的平均值,并且用他们的成绩记录类的名字(等级必须是升序)。我们必须使用List和map来实现这个代码。失认沽应该是这样的:

75414133,迈赫迪,爪哇:

Average: 14 

English: 12 

Math: 16 

88887777,沙迪,哈尼:

Average: 16.5 

Math: 15 

Physiques: 16.5 

English: 18 

对不起家伙,我尽我所能。但我没有成功解决它。任何帮助表示赞赏。

编辑:这里是未完成的代码,我写了,这是错误的,但因为我是问到这里的一些用户,我写我错了代码写在这里:

public class Average 
{ 
    public static void main(String args[]){ 
     String str ="75414133,Mehdi,Javan,Math,16;88887777,Shadi,Khani,English,18;12345678,Mohamad,Sayadi,Chemistry,15;75414133,Mehdi,Javan,English,12;88887777,Shadi,Khani,Math,15;88887777,Shadi,Khani,Physiques,16.5;12345678,Mohamad,Sayadi,Sport,17.25;75414133,Mehdi,Javan,English,12;12345678,Mohamad,Sayadi,Islamic Revolution,8"; 
     String[] strRecords = str.split(";"); 

     for(int i=0;i<strRecords.length;i++){ 
      String[] strColon= strRecords[i].split(","); 

      for (int j=1;j<strRecords.length;j++){ 
       if(Integer.valueOf(strRecords[i].substring(0,8)) ==Integer.valueOf(strRecords[j].substring(0,8))){ 
        String strName = strColon[0] + "," + strColon[1]+"," +strColon[2]; 
        System.out.println(strName); 
       } 
      } 
     } 
    } 
} 
+3

您必须出示在奥德代码r我们帮助你,否则我们会失明。你到目前为止尝试了什么? – zero298

+0

好的,我会编辑第一篇文章,并写我尝试的代码(尽管)它没有写得很好,是错误的,但我会。 –

+0

请正确缩进您的代码。另外,你的代码有什么问题?它以什么方式不做你想要的? – Keppil

回答

4

这里是你想要

public class StudentMain 
{ 
    public static void main(String args[]) throws JAXBException 
    { 
     String input = "75414133,Mehdi,Javan,Math,16; 88887777,Shadi,Khani,English,18; 75414133,Mehdi,Javan,English,12; 88887777,Shadi,Khani,Physiques,16; 512345678,Mohamad,Sayadi,Chemistry,15; 88887777,Shadi,Khani,Math,15;"; 

     String[] studentRecords = input.split(";"); 

     List<Student> students = new ArrayList<Student>(); 

     for (String studentRecord : studentRecords) 
     { 
      String[] studentData = studentRecord.split(","); 

      Student student = new Student(studentData[0], studentData[1], studentData[2]); 

      if (students.contains(student)) 
      { 
       student = students.get(students.indexOf(student)); 

       student.addScore(studentData[3], Integer.valueOf(studentData[4])); 
      } 
      else 
      { 
       student.addScore(studentData[3], Integer.valueOf(studentData[4]));     
       students.add(student); 
      } 
     } 

     for (Student student : students) 
     { 
      student.printScoreCard(); 
     } 
    } 
} 

class Student 
{ 
    private String number; 
    private String firstName; 
    private String lastName; 

    private Map<String, Integer> scores = new HashMap<String, Integer>(); 

    public Student(String number, String firstName, String lastName) 
    { 
     this.number = number.trim(); 
     this.firstName = firstName.trim(); 
     this.lastName = lastName.trim(); 
    } 

    public void addScore(String subject, Integer score) 
    { 
     this.scores.put(subject, score); 
    } 

    public void printScoreCard() 
    { 
     System.out.println(); 
     System.out.println(number + "," + firstName + "," + lastName + ":"); 
     System.out.println("Average: " + getAverageScore()); 

     for (Entry<String, Integer> score : scores.entrySet()) 
     { 
      System.out.println(score.getKey() + ": " + score.getValue()); 
     } 

     System.out.println(); 
    } 

    private double getAverageScore() 
    { 
     double average = 0.0; 

     for (Entry<String, Integer> score : scores.entrySet()) 
     { 
      average = average + score.getValue(); 
     } 

     average = average/scores.size(); 
     return average; 
    } 

    @Override 
    public boolean equals(Object o) 
    { 
     if (o == null || !(o instanceof Student)) 
     { 
      return false; 
     } 

     Student compareTo = (Student) o; 

     return number.equals(compareTo.number); 
    } 
} 
+0

非常感谢你的帮助 –

0

可以使用字符串解析你的字符串.split()方法。像 yourString.split(":")将所有学生分开。然后你可以做string.split(“,”)来提取每个学生的所有属性。你可以制作一个学生课程并制作具有基本属性的对象。

+0

感谢您的回复,但代码应该按“;”分隔不是“:”你提到。请你用编程语言编写你的意思,我需要帮助实现地图和列表有问题的地方,谢谢 –

0

虽然SANKET的答案是安静好了,我想发布另一个答案具有更高的性能是什么。一些显着的点是:

  • 该算法的顺序必须为O(n),从而代替使用ArrayList存储学生使用HashMap由于ArrayListcontains方法使用循环来找到对象及其顺序是O(n),而通过其在HashMap中的键找到条目的顺序是O(1)。
  • 收集容量应该初始化。处理大量对象时会影响性能。
  • 为了解决这个问题,没有必要使用Map来存储分数的数据,而简单的List就足够了。

下面是答案:

主要类:

import java.util.HashMap; 
import java.util.Map; 

public class Main { 
    public static void main(String[] args) { 
     String input = "75414133,Mehdi,Javan,Math,16;88887777,Shadi,Khani,English,18;12345678,Mohamad,Sayadi,Chemistry,15;75414133,Mehdi,Javan,English,12; 88887777,Shadi,Khani,Math,15;88887777,Shadi,Khani,Physiques,16.5;12345678,Mohamad,Sayadi,Sport,17.25;12345678,Mohamad,Sayadi,Islamic Revolution,8"; 

     String[] studentRecords = input.split(";"); 
     Map<Long, Student> studentMap = new HashMap<>(studentRecords.length); 
     for (String record : studentRecords) { 
      String[] fields = record.split(","); 
      Long id = Long.valueOf(fields[0].trim()); 
      String firstName = fields[1]; 
      String lastName = fields[2]; 

      Student student = studentMap.get(id); 
      if (student == null) { 
       student = new Student(id, firstName, lastName); 
       studentMap.put(id, student); 
      } 

      String courseTitle = fields[3]; 
      Float score = Float.valueOf(fields[4]); 
      student.getCourseList().add(new Course(courseTitle, score)); 
     } 

     for (Student student : studentMap.values()) 
      student.printInfo(); 
    } 
} 

Student类:

import java.util.ArrayList; 
import java.util.List; 
import java.util.Objects; 

public class Student { 
    private Long id; 
    private String firstName; 
    private String lastName; 
    private List<Course> courseList = new ArrayList<>(); 

    public Student(Long id, String firstName, String lastName) { 
     this.id = id; 
     this.firstName = firstName; 
     this.lastName = lastName; 
    } 

    public String getFirstName() { 
     return firstName; 
    } 

    public void setFirstName(String firstName) { 
     this.firstName = firstName; 
    } 

    public String getLastName() { 
     return lastName; 
    } 

    public void setLastName(String lastName) { 
     this.lastName = lastName; 
    } 

    public List<Course> getCourseList() { 
     return courseList; 
    } 

    public void setCourseList(List<Course> courseList) { 
     this.courseList = courseList; 
    } 

    public void printInfo() { 
     System.out.println(id + "," + firstName + "," + lastName); 
     System.out.println(String.format("Average: %.2f", calculateAverage())); 

     for (Course course : courseList) 
      System.out.println(String.format("%s: %.2f", course.getTitle(), course.getScore())); 
    } 

    private float calculateAverage() { 
     float sum = 0; 

     for (Course course : courseList) 
      sum += course.getScore(); 

     float average = sum/(float) courseList.size(); 

     return average; 
    } 
} 

课程类:

public class Course { 
    private String title; 
    private Float score; 

    public Course(String title, Float score) { 
     this.title = title; 
     this.score = score; 
    } 

    public String getTitle() { 
     return title; 
    } 

    public void setTitle(String title) { 
     this.title = title; 
    } 

    public Float getScore() { 
     return score; 
    } 

    public void setScore(Float score) { 
     this.score = score; 
    } 
}