存储每个给定短语的用户输入评级,并按给定的评级顺序打印

时间:2014-11-30 17:41:03

标签: java sorting java.util.scanner

我基本上有这5个随机句子存储在数组中我基本上希望用户输入10个中每个句子的分数并将其存储在数组中,然后包括按顺序排序句子的方法给予他们的分数并按顺序打印 即从最高到最低。

我是java的新手,如果有人可以帮我解决这个问题我会非常感激,显然我必须使用比较,但我该如何使用?还有其他办法吗?

    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." };
    Random random = new Random();
    int rand = random.nextInt(sentences.length);

    for (int i=0; i<=4; i++)    
    {
    ratemessage(sentences[rand]);  

    }       


    public static void ratemessage (String sentences) 

    {   
        JOptionPane.showMessageDialog( sentences ); 


        JOptionPane.showInputDialog("what do you rate this sentence out of 10?");
        Scanner input = new Scanner(System.in);
        int result = input.nextInt();
    }
        Arrays.sort(result);// this is the Array.sort()method

        for (int i=0; i<=4; i++)
        {
        System.out.println("you given rating is " + result + "for " + sentences);
        }
}
// END loopswithinloops

1 个答案:

答案 0 :(得分:0)

你只需要随机化你的句子(你说5个随机句子)。 当您使用JOptionPane时,您不需要扫描仪。

所以这就是你可以做到的 - 我已经添加了一些评论:

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

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
} 
Arrays.sort(result);
for (int i = 0; i < sentences.length; i++) {
    System.out.println("you given rating is " + result[i] + " for " + sentences[i]);
}