根据用户输入的最高分数按顺序排序消息

时间:2014-11-29 15:46:12

标签: java arrays sorting java.util.scanner

我打印了这4个句子,基本上希望用户输入10个句子中的每个句子的分数,然后将它们存储在数组中,然后按照给出的分数顺序对句子进行排序并打印它们按从高到低的顺序

我是java的新手,我正在努力解决这个问题,我设法在下面写了一些代码,这是我最远的。我不知道如何将这些打印的消息存储在数组中。而且我不确定如何将分数与打印的消息相关联。有人可以帮忙,:/

public static void arrays()
    {

        String []noun = {"face", "eyes", "nose", "lips", "ears", "cheeks"}; //stores all the arrays for noun words
        Random random = new Random();  //to work out the new random
        int rand1 = random.nextInt(noun.length); 


        String []verb = {"enchant", "dazzle", "captivate" , "lure", "desire", "entice" }; // stores all the arrays for verb words
        Random random2 = new Random();
        int rand2 = random2.nextInt(verb.length);


        String []adjective = { "alluring", "angelic", "adoring", "appealing", "attractive", "beautiful"}; // stores all the arrays for adjectives
        Random random3 = new Random();
        int rand3 = random3.nextInt(adjective.length);

        printmessage (noun[rand1], verb[rand2], adjective[rand3]);  //to print the message that will use the arguments from the random selected words.

        getscore ();

    }

     public static void printmessage(String noun, String verb, String adjective)  //calls the arguments defined above, which will be used below, when executing the messages.
    {

        System.out.println("line 1: I would love to " + verb + " your " + adjective + " " + noun + "\n");

        System.out.println("line 2: Your are my " + adjective+ " " + noun + " .You " + verb +  " me" + "\n");

        System.out.println("line 3: you always look great in that " + adjective + " dress" + " and you " + verb +  " me with your " + noun + "\n");

        System.out.println("line 4: I get butterflies when I see your " + noun + " , you make me want to " + verb + " , in your " + adjective + " world" + "\n");
    }

    public static void getscore()
    {   
        Scanner input = new Scanner(System.in);

        for (int i=0; i<=3; i++)
        {
        JOptionPane.showInputDialog("Enter the scores for line: " + i);
        int result = input.nextInt();
        int[] scores = new int[result];
        scores[i] = input.nextInt();
        } 

    }

1 个答案:

答案 0 :(得分:0)

在打印邮件之前,在需要得分时反转getScore和print语句的调用。所以你可以将带有getScore的int数组返回给main方法,如:

public static int[] getscore() {
    int[] scores = new int[3];
    //your logic to populate scores 
    return scores;
}

然后在你的主要使用:

int count = 0;
int scores [] = getscore ();
Arrays.sort(scores);
printmessage (noun[rand1], verb[rand2], adjective[rand3], score[count++]);  //to print the message that will use the arguments from the random selected words.

在您的打印信息中,您可以使用如下所示:

switch(score) {
    case 1://blah blah 
    break;
    //and so on
}