【问题标题】:How to code a function that randomly selects a quote from an array? - Android Java如何编写从数组中随机选择引号的函数? - 安卓Java
【发布时间】:2015-02-13 00:40:41
【问题描述】:

我正在关注一个 android 编码教程,并尝试随机选择报价。以下是未修改的代码。

       final ArrayList<Quote> quoteList = new ArrayList<Quote>();

    Quote quote1 = new Quote("Would I rather be feared or loved? Easy. Both. I want people to fear how much they love me.", "Michael Scott");
    quoteList.add(quote1);

    Quote quote2 = new Quote("I'm not superstitious. But I am a little stitious.", "Michael Scott");
    quoteList.add(quote2);

    Quote quote3 = new Quote("I like waking up to the smell of bacon.", "Michael Scott");
    quoteList.add(quote3);

    Quote quote4 = new Quote("Wikipedia is the best thing ever. Anyone in the world can write anything they want about any subject. So you know you are getting the best possible information.", "Michael Scott");
    quoteList.add(quote4);

    Quote quote5 = new Quote("Mo' money, mo' problems.", "Michael Scott");
    quoteList.add(quote5);

    Quote quote6 = new Quote("SWAG! Stuff We All Get.", "Michael Scott");
    quoteList.add(quote6);

    Quote quote7 = new Quote("You just gots to get your freak on.", "Michael Scott");
    quoteList.add(quote7);

    Quote quote8 = new Quote("We're all homos. Homo sapiens.", "Michael Scott");
    quoteList.add(quote8);

    Quote quote9 = new Quote("Hate to see you leave but love to watch you go. 'Cause of your butt.", "Michael Scott");
    quoteList.add(quote9);

    Quote quote10 = new Quote("If a baby were president, there would be no taxes, there would be no war.", "Michael Scott");
    quoteList.add(quote10);
    //Add more quotes here


    touch.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            if (count < quoteList.size()) {
                Quote q = quoteList.get(count);
                quoteText.setText(q.getQuote());
                personText.setText(q.getPerson());
                count = count + 1;
            } else{
                count = 0;
            }
        }
    });
}

如果有人能解释如何让应用程序在触摸屏幕时随机选择一个引号,而不是一个一个地浏览它们,我将不胜感激。

【问题讨论】:

  • 您的问题是什么?请具体说明您发布的代码有什么问题?
  • 从结构的角度来看,我会考虑创建一个扩展ArrayListQuoteArrayList 类,其构造函数采用一对字符串并为您生成引号并添加到列表中。然后实现一个返回随机引用的getRandomQuote() 方法(使用下面的任何解决方案)

标签: java android arrays random


【解决方案1】:

您必须生成一个介于 0 和列表长度之间的随机数

Random rand = new Random()
int  n = rand.nextInt(quoteList.size() - 1);

然后使用获取该索引处的报价

Quote random = quoteList.get(n);

【讨论】:

    【解决方案2】:
    Random random = new Random();    
    int randomNum = random.nextInt(quoteList.size() - 1);
    Quote randomQuote = quoteList.get(randomNum);
    

    【讨论】:

    • 不要减 1 - 查看 nextInt() 的文档。
    【解决方案3】:
    String yourString = quoteList[new Random().nextInt(quoteList.length)];
    

    【讨论】:

    • 您从包含Quote 的列表中获得String。此外,ArrayList 使用size() 而不是length,并且要从ArrayList 中获取一个元素,您需要使用.get(index) 而不是[index],最后您不会从发送给随机的int 中减去1,它返回元素的数量,而不是最后一个索引。
    【解决方案4】:

    随机选择一个报价:

    int randomNum = (int)(Math.random()*(quoteList.size() - 1));
    Quote randomQuote = quoteList.get(randomNum);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-01-23
      • 2011-03-29
      • 2014-04-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-24
      相关资源
      最近更新 更多