2014-12-03 65 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);