2014-12-03 66 views
0

我設法編寫下面的代碼,但是在打印時,它只按順序打印評分,但不打印句子。我如何按照給定的評分排列句子?如何按照降序排列和打印短語

public static void loopswithinloops() 
{ 

    String[] sentences = {"I am blessed to have you in my life. You are the one thing in my life that is true and real", 
        "I am honoured to have you by my side to love and to cherish each day of our lives.", 
        "More precious than any other thing in my life is to see your face each and every day", 
        "To wake up beside you is a treasure that I have found in you and that I am thankful for.", 
        "Your beautiful eyes dance bright and clear and I can see forever in your eyes." }; 

    for (int i=0; i<=1; i++)  
    { 
    ratemessage(sentences); //this will loop the whole rating message two times from 0 to 1. 
    }  

} 

public static void ratemessage (String[] sentences) // this will receive the argument from the method defined above and then be printed below as shown. 

{ 
    int[] result = new int[sentences.length];//you have to give the array a size 
    String inputStr; 

    for (int i = 0; i < sentences.length; i++) 
    { 
     JOptionPane.showMessageDialog(null, sentences[i]);//the sentences one at at time 
     inputStr = JOptionPane.showInputDialog("what do you rate this sentence out of 10?"); 
     result[i] = (int)Float.parseFloat(inputStr);//put the input into the array - it comes as a float so you'll have to cast it to int 
    } 

    sort (result, sentences); 
    printsort (result, sentences); 

} 

public static void sort(int [] result , String [] sentences) 
{ 
    int temp; 

    for(int i = 0; i < result.length; i++) 
    { 
     for(int j = 1; j < (result.length -i); j++) 
     { 
      //if numbers[j-1] > numbers[j], swap the elements 
      if(result[j-1] > result[j]) 
      { 
       temp = result[j-1]; 
       result[j-1]=result[j]; 
       result[j]=temp; 
      } 
     } 
    } 
} 

public static void printsort (int [] result , String [] sentences) 
{ 
System.out.println("The ratings go in ascending order: "); 
    for(int i = 0; i < result.length; i++) 
    { 
     System.out.println("Your ascending rating is " + result[i]+ " " + sentences[i]); 
    } 

} 
+0

您可以按照您所排序結果同時句子。問自己爲什麼排序方法有兩個參數,以及爲什麼第二個從未被使用。 – Mooolo 2014-12-03 00:24:53

+0

是的,但我不知道在排序方法中包括第二個,以及如何去解決。 @Mooolo – Arpit 2014-12-03 00:49:21

回答

0

與其僅僅提供代碼來解決這個特定問題,我會給出一個關於如何重構它以使其更加直觀的指針。我正在使用Java 8功能,但如果需要的話,很容易轉換回來。

創建一個既包含消息又包含評級的類。

public class RatedMessage { 
    private final String message; 
    private final int rating; 

    public RatedMessage(String message, int rating) { 
     this.message = message; 
     this.rating = rating; 
    } 

    public String toString() { 
     return rating + ": " + message; 
    } 

    public static int compareRating(RatedMessage rm1, RatedMessage rm2) { 
     return rm1.rating - rm2.rating; 
    } 
} 

然後創建RatedMessage對象的列表

List<RatedMessage> ratedMessages = new ArrayList<>(); 

改變你的消息計費方法來爲每個評級消息的RatedMessage並把它添加到列表中。

ratedMessages.add(new RatedMessage(message, rating)); 

然後使用得分排序列表:

Collections.sort(ratedMessages, RatedMessage::compareRatings); 

然後打印出排序列表:

ratedMessages.stream().map(RatedMessage::toString).forEach(System.out::println);