在NetBeans IDE中生成随机单词?

时间:2014-12-09 18:51:04

标签: java netbeans

我想问一下如何制作一个程序应该生成三个单词,长度为10个字符,第一个字母为大写,然后这三个单词应该合并,这样只有第一个字母是大写的。有一条说明是用大写的第一个字母表示你应该使用toLowerCase,然后使用子字符串增加第一个字母。

3 个答案:

答案 0 :(得分:0)

我假设它是用Java编写的(对C来说可能相同)。

查看Ascii表(http://www.asciitable.com/

对于第一个上部字母,生成65-90之间的整数,然后将其设置为Character。对于小写,生成一个介于97-122之间的整数,然后使其成为Character。将字符合并在一起形成一个词。

答案 1 :(得分:0)

这感觉有点愚蠢,但我做到了。如果这些话必须是真实的,那么你将需要别的东西。

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);


      }
    }

答案 2 :(得分:0)

这是:

导入javax.swing.JOptionPane;

public class Main {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    new Main();
}
public Main() {
    double number1 = Double.parseDouble(JOptionPane.showInputDialog("Input first numnber:"));
    double number2 = Double.parseDouble(JOptionPane.showInputDialog("Input second number:"));
    String operation = JOptionPane.showInputDialog("Input operation +,-,/,*");
    JOptionPane.showMessageDialog(null, number1*2);
    JOptionPane.showMessageDialog(null, 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;
  }

} }