English 中文(简体)
将“AA”级改为“AA”级的方案,其标记为90多个,采用Java 8流概念和tern操作者[封闭]
原标题:Program to set the grade to "AA" whose marks are more than 90 by using Java 8 streams concept and ternary operator [closed]
Closed. This question needs debugging details. It is not currently accepting answers.

下面是我的节目,我需要将年级定在“A”级,其分数在60至90之间,应为“B”,其余学生的年级为“C”。 如何在java8溪流中利用私人经营者实施这一方案。 我试图过滤有90个以上标志的学生,并试图使用固定校对方法确定年级。 但我正发现错误。 请帮助我解决这一问题。 ......

import java.util.ArrayList; 
import java.util.List; 
import java.util.stream.Collectors; 
import java.util.stream.Stream;


class Student { String name; String subject; Double marks; String grade;    
           Student(String name, String subject, Double marks){
        this.name=name;
        this.subject=subject;
        this.marks=marks;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Double getMarks() {
        return marks;
    }
    public void setMarks(Double marks) {
        this.marks = marks;
    }
    public String getSubject() {
        return subject;
    }
    public void setSubject(String subject) {
        this.subject = subject;
    }
    public String getGrade() {
        return grade;
    }
    public void setGrade(String grade) {
        this.grade = grade;
    }
    @Override
    public String toString() {
        return "Student [name=" + name + ", subject=" + subject + ", marks=" + marks + ", grade=" + grade + "]";
    }
    
}

public class MyProject{ public static void main(String[] args) {    
List <Student> studentList = new ArrayList < > ();
    studentList.add(new Student("Ram", "maths",85.5));
    studentList.add(new Student("Pranav", "maths",90.5));
    studentList.add(new Student("Vibin", "maths",92.0));
    studentList.add(new Student("Nikhil", "maths",60.0));
    studentList.add(new Student("Sushil", "science",60.0));
List<Student> mystudents= studentList.stream().filter(i->i.getMarks()>=90) .map(i>i.setGrade("A")) .toList(); System.out.println(mystudents);
}};
问题回答

为了直接根据学生的分数绘制学生分级图,而不首先进行过滤,你可以在地图功能中使用一个实习生,如:

List<Student> students = studentList.stream()
.map(student -> {
    student.setGrade(student.getMarks() >= 90 ? "A" : student.getMarks() >= 60? "B" : "C");
    return student;
})
.collect(Collectors.toList());

地图在各行中发挥作用的论点必须是一种功能。 你写道,i > i.set Grade(A)不是正确的形式。 相反,你应当使用:

List<Student> mystudents = studentList.stream()
.filter(i -> i.getMarks() >= 90)
.map(i -> {
    i.setGrade("A");
    return i;
})
.collect(Collectors.toList());




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签