英文:
Setting negative marks in an string to zero (java)
问题
public void setNegativeMarksToZero()
{
    //TODO 3
    double negativeMark = marks.get(1).doubleValue();
    for (int i = 0; i < marks.size(); i++) {
        double val = marks.get(i).doubleValue();
        if (val < 0.0) {
            marks.set(i, 0.0);
            System.out.println(marks.get(i).doubleValue());
        }
        else {
            normalizeMarks();
        }
    }
}
Note: In the original code, there were several issues related to incorrect syntax and logic. I've made corrections to the code to ensure it functions as intended.
英文:
Help, i cant figure this out, im new to java and never coded before this class im taking. the assignment is a code that deals with voting, basically the votes are given and i need to formalise them and make sure there are no errors such as negative votes. i dont thing i am suppose to change any of the code that was given including the constructor and the initialiser for each part. i need help with the part that says //TODO 3
import java.util.*;
public class VotingPaper {
    // the numbers marked on the paper
    private ArrayList<Double> marks;
    // precision required for matching doubles
    private double epsilon = 10e-4;
    /**
     * Constructor for objects of class VotingPaper.
     * s will be a sequence of doubles, separated by commas.
     * e.g. if s is "1,22,-13,456", marks is set to <1,22,-13,456>.
     * s can also be a sigle value i.e. with no comma
     */
    public VotingPaper(String s) {
        marks = new ArrayList<>();
        for (String x : s.split(","))
            marks.add(Double.parseDouble(x));
    }
    /**
     * Returns the contents of the paper.
     */
    public ArrayList<Double> getMarks() {
        //TODO 1
        return marks;
    }
    /**
     * Checks if the paper has the correct number of marks i.e. one for each candidate.
     * If YES, ensures that the negative marks have been set to zero and
     * that marks are normalized to sum up to 1.0 and then returns true
     * If NO, it returns false
     */
    public boolean isFormal(int noOfCandidates) {
        // TODO 2
        if (noOfCandidates == marks.size()) {
            setNegativeMarksToZero();
        } else {
            return false;
        }
        return true;
    }
    /**
     * Sets all negative marks to zero
     */
 public void setNegativeMarksToZero()
{
   //TODO 3
   double negativeMark = marks.get(1).doubleValue();
   for (Double val : marks) {
       System.out.println(val.doubleValue());
       if (val.doubleValue() < 0.0) {
           val = Double((double)0.0);
           System.out.println(val.doubleValue());
       }
       else { 
           normalizeMarks();
       }
   }
}
    private void normalizeMarks() {
    }
}
This is the code. this is for a project and im stuck, i cant get the string to replace the negative numbers in the string with 0.0 but i can get it to set the negative value as val.doubleValue() and set that to 0.0. thanks in advance for any help.
答案1
得分: 0
我不确定我理解你的问题,但如果你只想让 setNegativeMarksToZero 将 marks 列表中的负值替换为0,你可以这样做:
public void setNegativeMarksToZero() {
    for (int i = 0; i < marks.size(); i++) {
        if (marks.get(i) < 0.0)
            marks.set(i, 0.0);
    }
}
英文:
I'm not sure that I know what you're asking, but if you just want setNegativeMarksToZero  to replace negative values in the marks list with 0 values, you can do this:
public void setNegativeMarksToZero() {
    for (int i = 0; i < marks.size() ; i++) {
        if (marks.get(i) < 0.0)
            marks.set(i, 0.0);
    }
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论