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]);
}
}
您可以按照您所排序結果同時句子。問自己爲什麼排序方法有兩個參數,以及爲什麼第二個從未被使用。 – Mooolo 2014-12-03 00:24:53
是的,但我不知道在排序方法中包括第二個,以及如何去解決。 @Mooolo – Arpit 2014-12-03 00:49:21