2017-03-04 45 views
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); 

    } 

回答

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; 
    } 
-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

+1

用JavaScript回答到java問題不是一個好主意 – baao