将字符串中的负数替换为零(Java)

huangapple go评论114阅读模式
英文:

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&lt;Double&gt; 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 &quot;1,22,-13,456&quot;, marks is set to &lt;1,22,-13,456&gt;.
     * s can also be a sigle value i.e. with no comma
     */
    public VotingPaper(String s) {
        marks = new ArrayList&lt;&gt;();
        for (String x : s.split(&quot;,&quot;))
            marks.add(Double.parseDouble(x));
    }

    /**
     * Returns the contents of the paper.
     */
    public ArrayList&lt;Double&gt; 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() &lt; 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

我不确定我理解你的问题,但如果你只想让 setNegativeMarksToZeromarks 列表中的负值替换为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 &lt; marks.size() ; i++) {
        if (marks.get(i) &lt; 0.0)
            marks.set(i, 0.0);
    }
}

huangapple
  • 本文由 发表于 2020年9月7日 14:52:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/63772659.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定