在NetBeans IDE中生成随机单词? [英] Generating random words in NetBeans IDE?

查看:70
本文介绍了在NetBeans IDE中生成随机单词?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想问一下如何制作一个程序,该程序应生成三个单词,这些单词的长度为10个字符,首字母大写,然后将这三个单词合并,以使只有首字母大写.需要注意的是,要使第一个字母大写,应使用toLowerCase,然后使用子字符串增加第一个字母.

I would like to ask about how to make a program that should be generating three words with the length of 10 characters with first letter in uppercase and then those three words should merge so that only the first letter is in uppercase. There is a note that says to make the first letter in uppercase you should do toLowerCase and after that increase the first letter using substring.

推荐答案

这听起来有点愚蠢,但我做到了.如果这些单词必须是真实的,那么您还需要其他东西.

this feels kinda stupid but I did it.If the words have to be real then you will need something else.

import java.util.Random;


public class random {
public static void main(String[] args){
String word1=generateWord();

String word2=generateWord();

String word3=generateWord();

System.out.print(word1+"\n"+word2+"\n"+word3+"\n");

String allThreeWithTheFirstLetterCapital=putThemTogether(word1,word2,word3);

System.out.print(allThreeWithTheFirstLetterCapital);
}
   public static String generateWord(){
        Random r=new Random();
        StringBuilder sb=new StringBuilder();
        sb.append((char)(65+r.nextInt(25)));
        for(int i=0;i<9;i++){
        sb.append((char)(97+r.nextInt(25)));
    }
    return sb.toString();

}
public static String putThemTogether(String...strings){
    StringBuilder sb=new StringBuilder(strings[0]);
    for(int i=1;i<strings.length;i++){
        sb.append(strings[i].toLowerCase());
    }
    return sb.toString();}
}

就这样

    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Font;
    import java.awt.GridBagLayout;
    import java.awt.Toolkit;

    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JOptionPane;
    import javax.swing.JPanel;


    public class weirdMath {

      public static void main(String[] args){
          double number1=Double.parseDouble(JOptionPane.showInputDialog("input a first number"));
          double number2=Double.parseDouble(JOptionPane.showInputDialog("input a second number")); 
          String operation=JOptionPane.showInputDialog("input an operation (+-/*)");
          number1*=2;number2*=2;
          double result=0;
          if(operation.equals("+")){result=number1+number2;}
          else if(operation.equals("-")){result=number1-number2;}
          else if(operation.equals("/")){result=number1/number2;}
          else if(operation.equals("*")){result=number1*number2;}
          JFrame jf=new JFrame("your answer");
          jf.setSize(300,75);
             Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
             int width = (int)screenSize.getWidth();
             int height = (int)screenSize.getHeight();
           jf.setLocation((int)(width/4.5), (int)(height/3.5));
          JLabel jlmessagepart1=new JLabel("       taking your two numbers times 2");
          JLabel jlmessagepart2=new JLabel("then performing your operation you get...");
          JLabel jlresult=new JLabel(""+result);
          jlresult.setFont(new Font(null, Font.BOLD, 20));
          jlresult.setForeground(Color.blue);
          jf.setLayout(new BorderLayout());
          jf.add(jlmessagepart1,BorderLayout.NORTH);
          jf.add(jlmessagepart2,BorderLayout.CENTER);
          jf.add(jlresult,BorderLayout.SOUTH);
          jf.setVisible(true);


      }
    }

这篇关于在NetBeans IDE中生成随机单词?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆