Java问题,无法打印这两种方法

时间:2014-10-28 23:43:13

标签: java arraylist

     package messagelist;
     import java.util.ArrayList;
     import java.util.List;
     import java.util.Scanner;
     import java.util.Random;



    public class Messagelist {

     public static void main(String[] args) {

           //create an ArrayList object

           List list = new ArrayList();

         //Add elements to Arraylist

         list.add(0, "I love Java Programming."); 
         list.add(1, "Java programming is so much fun."); 
         list.add(2, "Who needs C ++, when you have Java."); 
         list.add(3, "Java can be hard.");
         list.add(4, "I would be happy when I get an A."); 
         list.add(5, "I love Jave the coffee also."); 
         list.add(6, "Who wants to Learn Java."); 
         list.add(7, "I'm taking a vacation after this class.");
         list.add(8, "Only two more questions to go."); 
         list.add(9, "What course should I take next?");

       System.out.println("Retrieving stored messages from Arraylist");
       shoutOutCannedMessage(list);

         }


          //This method retrieves values from ArrayList using get method


        public static void shoutOutCannedMessage(List list)
        {


              //Retrieving values from list

        int size = list.size();
        for(int i=0;i<size;i++)

        {

            System.out.println(list.get(i));
            System.out.println("**********************\n\n");
        }


          int userIndex;
          Scanner scanner = new Scanner(System.in);
          System.out.println("Please enter the index number you wish to search");
          userIndex = scanner.nextInt();
          System.out.println(list.get(userIndex));
        }

上述方法按预期打印,但下面的代码没有。对于这个项目,我需要shoutOutCannedMethodshoutOutRandomMethod,但只有固定方法有效。

        public static void ShoutOutRandomMessage() {

        int i;

      //holds the words to be generated.

      String[] subject= {" He's", " I'm", " She"};
      String[] verb= {" eating", " catching", " studying", " caughing"};
      String[] adjective= {" funny", " hard", " good", " polite"};
      String[] object= {" course", " homework", " books", " dog"};
      String[] adverb= {" quickly. ", " everywhere. ", " accordingly. ", " awfully. "};


       Random r = new Random(); //intialize a Random
       int selectedElement = r.nextInt(subject.length);

      //randomly create sentence.

      for (i=1; i<=1; i++)

     {

     String randomSentence=subject[selectedElement] 
      + verb[selectedElement]       
      + adjective[selectedElement] 
      + object[selectedElement]
      + adverb[selectedElement];

     System.out.println("ShoutOut: " + randomSentence );

      }
      }

2 个答案:

答案 0 :(得分:0)

您的随机数仅生成一次。动词,形容词等使用相同的数字。因此,如果随机生成器返回0,那么你就会快速地吃掉有趣的课程。&#34;我猜你的for循环会为每个数组生成一个随机数,但是你得到了偏离。您获得的输出是什么(您只是说它没有按预期打印出来)?

答案 1 :(得分:0)

你没有调用你的方法shoutOutRandomMessage,所以永远不会执行它。

添加

ShoutOutRandomMessage();

在下面的主要方法中

shoutOutCannedMessage(list);

并在输入第一个方法所需的索引后打印出随机消息。

这样做我得到了这个结果:

Please enter the index number you wish to search
3
Java can be hard.
ShoutOut:  I'm catching hard homework everywhere.