-1

我有一个我想使用的超过 100 个单词的列表,我已经在几个地方寻找解决方案,但是由于我对 xcode 的了解有限,我不知道我将如何实现它们,我举一个应用程序的例子我在我想要在 Xcode 中创建的 java 中创建(但有一个刷新按钮,我想我知道如何做这个按钮)

//import java libraries
import java.awt.*;
import javax.swing.*;
public class Emotion extends JFrame
{
    //set what you can use
    private JLabel label;
    private JLabel phrasem;
    public Emotion()
    {
    setLayout( new FlowLayout());

    //Wordlists
    String[] wordlist =
    {
            "Anger","Misery","Sadness","Happiness","Joy","Fear","Anticipation","Surprise","Shame","Envy","Indignation","Courage","Pride","Love","Confusion","Hope","Respect","Caution","Pain","Rage Melon"
    };

        //number of words the list
        int length = wordlist.length;

        //random number
        int rand = (int) (Math.random() * length);


    //building phrase
    String phrase = wordlist[rand];

    // printing phrase
    phrasem = new JLabel("Today your emotion is:");
    add (phrasem);

    label = new JLabel(" " + phrase);
    add (label);

    }
    public static void main(String[] args)
    {
    Emotion gui = new Emotion();
    gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    gui.setSize(400, 60);
    gui.setVisible(true);
    gui.setTitle("My App (Alex Gadd)");

    }

}
4

1 回答 1

0

尝试以下从数组中获取随机单词

NSArray *wordList = [NSArray arrayWithObjects:@"Anger",@"Misery",@"Sadness",@"Happiness",@"Joy",@"Fear",@"Anticipation", @"Surprise",@"Shame",@"Envy",@"Indignation",@"Courage",@"Pride",@"Love",@"Confusion",@"Hope",@"Respect",@"Caution",@"Pain",@"Rage Melon", nil];

int randomWord = (int)arc4random() % [wordList count];
NSLog(@"Random word = %@", [wordList objectAtIndex:randomWord]);
于 2013-09-23T14:50:09.607 回答