如何使用以下方法反转字符串数组?

时间:2017-03-04 01:27:16

标签: java arrays string reverse

我基本上试图显示高于平均水平,低于平均水平的成绩输入,并显示所有成绩。如下所示,我展示了所有成绩的输入。现在我的问题是如何扭转所有成绩?

if (grades.size() != 0) {
        double average = (double) total / grades.size();

        System.out.println("\nAverage of grades is " + average);
        for (int i = 0; i < grades.size(); i++) {
            grade = grades.get(i);
            count++;
            countStr += grade + ", "; //All grades
            if (grade > average) {
                above++;
                aboveStr += grade + ", ";
            } else {
                below++;
                belowStr += grade + ", ";
            }
        }

        System.out.println(above + " grades above average:");
        System.out.println(aboveStr);
        System.out.println(below + " grades below average:");
        System.out.println(belowStr);
        System.out.println("All grades: " + countStr);

    }

2 个答案:

答案 0 :(得分:0)

您可以这样做:

public class grade {

    public static void main(String[]args) throws FileNotFoundException, UnsupportedEncodingException
    {
        ArrayList<Double> grades=new ArrayList<Double>();
        double grade;
        double total=100;
        int count=0,above=0,below=0;
        String countStr="",aboveStr="",belowStr="";

        grades.add(25.0);
        grades.add(59.2);
        grades.add(75.0);
        grades.add(85.5);
        grades.add(95.0);

        if (grades.size() != 0) {
            double average = (double) total / grades.size();

            System.out.println("\nAverage of grades is " + average);
            for (int i = 0; i < grades.size(); i++) {
                grade = grades.get(i);
                count++;
                countStr += grade + ", "; //All grades
                if (grade > average) {
                    above++;
                    aboveStr += grade + ", ";
                } else {
                    below++;
                    belowStr += grade + ", ";
                }
            }

            System.out.println(above + " grades above average:");
            System.out.println(aboveStr);
            System.out.println(below + " grades below average:");
            System.out.println(belowStr);
            System.out.println("All grades: " + countStr);

            String reversedString=reverseString(countStr);
            System.out.println("Reversed string is "+ reversedString);

        }
    }

    private static String reverseString(String countStr) {
        String[] arr_grades=countStr.split(", ");
        String reversed="";
        for(int i=arr_grades.length-1;i>=0;i--)
        {
            reversed+=arr_grades[i]+", ";
        }
        return reversed;
    }

答案 1 :(得分:-2)

尝试将此作为一种功能

function reverseString(str) {
// Step 1. Use the split() method to return a new array
var splitString = str.split(""); // var splitString = "hello".split("");
// ["h", "e", "l", "l", "o"]

// Step 2. Use the reverse() method to reverse the new created array
var reverseArray = splitString.reverse(); // var reverseArray = ["h", "e", "l", "l", "o"].reverse();
// ["o", "l", "l", "e", "h"]

// Step 3. Use the join() method to join all elements of the array into a string
var joinArray = reverseArray.join(""); // var joinArray = ["o", "l", "l", "e", "h"].join("");
// "olleh"

//Step 4. Return the reversed string
return joinArray; // "olleh"
}

reverseString("hello");

我没有在https://medium.freecodecamp.com/how-to-reverse-a-string-in-javascript-in-3-different-ways-75e4763c68cb#.lcvytbsnn

上找到它