【问题标题】:I am struggling with creating a random word rather than a string of words [closed]我正在努力创建一个随机单词而不是一串单词[关闭]
【发布时间】:2013-08-04 17:23:12
【问题描述】:

这是我的代码

我得到一串单词而不是一个单词,我认为我做得很好。如果你能给我一些很棒的建议,我包含所有代码的唯一原因是我可能看多了一些东西,我认为这与短语的构建有关,但我不确定

//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[] wordlistone =
        {
                "anger","misery"+"sadness"+"happiness"+"joy"+"fear"+"anticipation"+"surprise"+"shame"+"envy"+"indignation"+"courage"+    "pride"+"love"+"confusion"+"hope"+"respect"+"caution"+"pain"
        };

        //number of words in each list
        int onelength = wordlistone.length;

        //random number
        int rand1 = (int) (Math.random() * onelength);


        //building phrase
        String phrase = wordlistone[rand1];

        // printing phrase

        phrasem = new JLabel("PhraseOMatic says:");
        add (phrasem);

        label = new JLabel("Today you emotion is: " + phrase);
        add (label);

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

    }

}

【问题讨论】:

    标签: java macos random


    【解决方案1】:

    你有+,你应该在单词列表中有,
    我想你只是把这两个弄错了。

    String[] wordlistone = {
        "anger", "misery", "sadness", "happiness", "joy", "fear", "anticipation",
        "surprise", "shame", "envy", "indignation", "courage", "pride", "love",
        "confusion", "hope", "respect", "caution", "pain"
    };
    

    另外,你可以很容易地用 java.util.Random 获得随机整数,它比Math.random() 更好

    Random rand = new Random();
    
    int i = rand.nextInt(wordlistone.length);
    

    【讨论】:

    • 谢谢,这真的很有帮助:)
    【解决方案2】:

    加号“+”运算符将字符串连接起来,得到一个单词。初始化字符串数组时,使用逗号作为单词分隔符。

    【讨论】:

      【解决方案3】:

      您的单词列表数组只有两个元素。您在第一个和第二个之间使用了逗号,然后意外地通过与其余单词连接创建了一个大字符串。改变这个:

          String[] wordlistone =
          {
                  "anger","misery"+"sadness"+"happiness"+"joy"+"fear"+"anticipation"+"surprise"+"shame"+"envy"+"indignation"+"courage"+    "pride"+"love"+"confusion"+"hope"+"respect"+"caution"+"pain"
          };
      

      到这里

          String[] wordlistone =
          {
                  "anger", "misery", "sadness", "happiness", "joy", "fear", "anticipation", "surprise", "shame", "envy", "indignation", "courage", "pride", "love", "confusion", "hope", "respect", "caution", "pain"
          };
      

      【讨论】:

        【解决方案4】:

        两个观察结果:

        • 您的array 包含串联的String 值,因此您应该将+ 替换为,
        • 您可能想在此处使用 Random 对象 - Math.random() * wordlistone.length 不起作用

        这是我的版本:

        String[] wordlistone = {
            "anger","misery","sadness","happiness","joy","fear","anticipation","surprise","shame","envy",
            "indignation","courage", "pride","love","confusion","hope","respect","caution","pain"           
        };
        
        Random r = new Random(); // you can reuse this - no need to initialize it every time
        System.out.println(wordlistone[r.nextInt(wordlistone.length)]);
        

        【讨论】:

          猜你喜欢
          • 2016-05-21
          • 1970-01-01
          • 1970-01-01
          • 2016-12-21
          • 2014-10-16
          • 1970-01-01
          • 1970-01-01
          • 2011-09-30
          • 2017-11-03
          相关资源
          最近更新 更多