【问题标题】:ClassRoll and Constructor methodsClassRoll 和 Constructor 方法
【发布时间】:2015-12-04 04:04:48
【问题描述】:
public class Exams {

private int score1 = 0;

private int score2 = 0;

private int score3 = 0;

 public void setScore1(int sc){
    score1 = sc;
}

public void setScore2(int sc){
    score2 = sc;
}

public void setScore3(int sc){
    score3 = sc;
}
public int getScore1(){
    return score1;
}

public int getScore2(){
    return score2;
}

public int getScore3(){
    return score3;
}

public String toString(){
    return String.format("%-10s %-10s %4.2f\n", score1, score2, score3);

}

}

public class Student {

private String fName;

private String lName;

private Exams scores;

Student(String fn, String ln) {
    fName = fn;
    lName = ln;
    scores = new scores();

}

public void setScore1(int sc) {
    scores.setScore1(sc);

}

public void setScore2(int sc) {
     scores.setScore2(sc);

}

public void setScore3(int sc) {
     scores.setScore3(sc);

}


public String toSring(){
    return String.format("%-10s %-10s %4.2f\n", fName, lName, scores);
}

public double getAverage(){

}

public int compareTo(Student s){
    String name1 = lName + "  " + fName;
    String name2 = s.lName + "  " + s.fName;

    return ((lName + "  " + fName).compareTo(s.lName + "  " + s.fName));
}

}

public class ClassRoll {

private ArrayList<Student> students = new ArrayList<Student>();
private String title;
private String filename = "data.txt";

ClassRoll(String f) {
    Scanner kb = new Scanner(System.in);
    String inpFileName = kb.next();
    File inpFile = new File(inpFileName);
    Student s = new Student(fName, lName);

}

void Remove() {
    Scanner kb = new Scanner(System.in);

    System.out.println("What is the Student's first name?");
    String fName = kb.next();

    System.out.println("What is the Student's last name?");
    String lName = kb.next();

    Student s = new Student(fName, lName);

    for (int i = 0; i < students.size(); i++) {
        if (s.compareTo(students.get(i)) == 0) {
            students.remove(i);

        } else {
            System.out.println("Error: Student is not in Class");
        }

    }

}

void Display() {

}

void Add() {
    Scanner kb = new Scanner(System.in);

    System.out.println("What is the Student's first name?");
    String fName = kb.next();

    System.out.println("What is the Student's last name?");
    String lName = kb.next();

    System.out.println("What is the Student's first score?");
    int score1 = kb.nextInt();

    System.out.println("What is the Student's second score?");
    int score2 = kb.nextInt();

    System.out.println("What is the Student's third score?");
    int score3 = kb.nextInt();

    Student s = new Student(fName, lName);

    for (int i = 0; i < students.size(); i++) {
        if (s.compareTo(students.get(i)) == 0) {
            System.out.println("Student already in class");
        } else {
            students.add(s);
        }
    }
}

void changeScore1() {
    Scanner kb = new Scanner(System.in);

    System.out.println("What is the Student's first name?");
    String fName = kb.next();

    System.out.println("What is the Student's last name?");
    String lName = kb.next();

    System.out.println("What is the Student's first score?");
    int score1 = kb.nextInt();

    Student s = new Student(fName, lName);

    for (int i = 0; i < students.size(); i++) {
        if (s.compareTo(students.get(i)) == 0) {
            s.setScore1(i);
        } else {
            System.out.println("Error: Student is not in Class");
        }

    }

}

void changeScore2() {
    Scanner kb = new Scanner(System.in);

    System.out.println("What is the Student's first name?");
    String fName = kb.next();

    System.out.println("What is the Student's last name?");
    String lName = kb.next();

    System.out.println("What is the Student's first score?");
    int score1 = kb.nextInt();

    Student s = new Student(fName, lName);

    for (int i = 0; i < students.size(); i++) {
        if (s.compareTo(students.get(i)) == 0) {
            s.setScore2(i);
        } else {
            System.out.println("Error: Student is not in Class");
        }

    }

}

void changeScore3() {
    Scanner kb = new Scanner(System.in);

    System.out.println("What is the Student's first name?");
    String fName = kb.next();

    System.out.println("What is the Student's last name?");
    String lName = kb.next();

    System.out.println("What is the Student's first score?");
    int score1 = kb.nextInt();

    Student s = new Student(fName, lName);

    for (int i = 0; i < students.size(); i++) {
        if (s.compareTo(students.get(i)) == 0) {
            s.setScore3(i);
        } else {
            System.out.println("Error: Student is not in Class");
        }

    }

}

public void sortAverage() {
    for (int i = 0; i < students.size() - 1; i++) {
        for (int j = i + 1; j < students.size(); j++) {
            Student s1 = (Student) students.get(i);
            Student s2 = (Student) students.get(j);
            if (s1.getAverage() < s2.getAverage()) {
                students.set(i, s2);
                students.set(j, s1);
            }
        }
    }
}

public void sortNames() {
    for (int i = 0; i < students.size() - 1; i++) {
        for (int j = i + 1; j < students.size(); j++) {
            Student s1 = (Student) students.get(i);
            Student s2 = (Student) students.get(j);
            if (s1.compareTo(s2) > 0) {
                students.set(i, s2);
                students.set(j, s1);
            }
        }
    }
}

public void save(){

}

}

我有一个包含几个不同课程的程序。在声明私有变量之后的 classroll 类中,我必须创建一个 classroll 构造函数“ClassRoll(String f)”,假设为.....

从输入文件 f 中读取班级卷数据,为每个学生创建学生对象并将它们添加到学生的 ArrayList 中。输入文件的第一行包含课程名称。每个学生的数据显示在一个单独的行中,由名字、姓氏、score1、score 2 和 score3 组成,至少用一个空格分隔。

我尽我最大的努力开始它,但我很困惑,不知道正确的制作方法。有人可以帮忙吗

谢谢

【问题讨论】:

  • 向我们展示您的努力程度。让汗水滴落!展示你的尝试(即使它不起作用)。
  • 嘿,如果你的学生设置分数方法使用考试中的设置分数方法来实际更改分数会发生什么?深思熟虑。可能与您的实际问题无关。
  • @JaskaranbirSingh ' 扫描仪 kb = new Scanner(System.in);字符串 inpFileName = kb.next();文件 inpFile = new File(inpFileName);学生 s = 新学生;'
  • @JaskaranbirSingh 在被卡住之前我做了我能想到的最好的事情
  • 你写了sc = score1;——当然应该是score1 = sc;; score2 和 score3 相同。

标签: java class methods constructor


【解决方案1】:

从输入文件f中读取班级卷数据,创建Student 每个学生的对象并将它们添加到 ArrayList 学生。

好的,所以基本上,我们必须从某个文件中获取学生的信息并将其添加到某个 ArrayList 中。好吧,让我们先创建一个 ArrayList 来存储东西。

ArrayList<Student> studentInfo = new ArrayList<Student>();

你已经有这个代码要从文件中读取:

Scanner kb = new Scanner(System.in);
String inpFileName = kb.next();
File inpFile = new File(inpFileName);

让我们继续……

输入文件的第一行包含课程标题。

所以kb.nextLine() 中的第一个元素是课程名称。这意味着我们必须从第二行开始添加学生数据,也就是跳过第一行。好吧....

每个学生的数据显示在单独的一行中,包括 名字、姓氏、score1、score 2 和 score3 以 at 分隔 至少一个空格

所以基本上,如果我们设法从space 字符中分割行,我们就会得到数据。现在来实现这个:

让我们创建一个boolean 变量来检查它的第一行是否:

boolean firstLine = true; // True - because it starts from first line

是时候开始读行了......

String courseName = ""; //I'll just store in a string, use as you wish

while(kb.hasNextLine()){
    if(firstLine){
        courseName = kb.nextLine(); //Set courseName if its first line
        firstLine = false; // We have moved past first line
    }
    else{ // Otherwise get student data
        // Lets store the student data in a string. This is just one line.
        // Something like: "FirstName LastName score1 score2 score3"
        String studentData = kb.nextLine();

        // Now time to split up data (so we get names/scores separately)
        String[] stData = studentData.split("\\s"); //Remember they have spaces between them. \\s --> A pattern that matches one or more spaces
        //So we can use those to separate data

        // This is how String[] stData contains the student information:
        // stData[0] = first name of student
        // stData[1] = last name of student
        // stData[2] = score1
        // stData[3] = score2
        // stData[4] = score3
        // Use this data however you like

        //Now that we have separated data, lets add the student object to ArrayList since we got all details.
        studentInfo.add(new Student(stData[0], stData[1]);
        // This last line of code is probably not going to do the trick.
        // You may need to make changes to your code to do stuff
    }
}

无论如何,我已经告诉过你如何阅读问题以及如何继续。你有所有的字符串,名字,姓氏和分数。现在由您决定如何使用它们。虽然最后一行代码可能不适合你,但它提示继续。

休息一下,你应该先弄清楚自己,因为如果我做所有的功课就没有乐趣;)

【讨论】:

    【解决方案2】:

    您已将 SE 替换为 scire1,2,3

    当您在 setter 方法中获得一个 var 时,需要将它放在您的类的属性中。

    只需替换为以下代码:

    public void setScore1(int sc){
           score1=sc;
        }
    
        public void setScore2(int sc){
            score2=sc;
        }
    
        public void setScore3(int sc){
            score3=sc;
        }
    

    【讨论】:

    • 你能告诉你为什么投反对票吗?作为 stackoverflow 成员,您应该知道您不会在没有解释的情况下投反对票。
    • 当我的回答正确时,我不喜欢被否决请你评论问题是什么(谁投了反对票)
    • 投反对票的人绝对没有义务解释他们的投票。搜索 Meta Stack OverflowMeta Stack Exchange 以获取有关此主题的 许多 个讨论。
    猜你喜欢
    • 2018-08-05
    • 2019-08-20
    • 2016-01-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-13
    • 2013-05-23
    相关资源
    最近更新 更多