亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

具體內容如下,關于java 題目!

具體內容如下,關于java 題目!

一只斗牛犬 2022-02-09 11:07:35
編寫隨機數創建句子的程序。使用4個名為article,noun,verb和preposition的字符串數組。要求01.通過按照article,noun,verb和preposition的順序 隨機地從每個數組中選擇一個單詞來創建句子。02.在挑選每個單詞后,將它鏈接到句子前一個單詞的后面。03.這些單詞應用空格分開。在輸出最終的句子時,它的第一個字母應為大寫,并用句號終止。04.該程序應產生20個句子,并在一個文本區中輸出他們。05.使用窗體。article數組應包含冠詞the ,a,one,some,anynoun數組包含名詞boy ,girl,dog,town,carverb數組應包含drove,jumped,ran,walk,skippedpreposition數組應包含介詞to ,from,over,under,on
查看完整描述

3 回答

?
慕的地8271018

TA貢獻1796條經驗 獲得超4個贊

import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Random;

public class testWord {

String[] article={"the", "a", "one", "some", "any"};
String[] noun={"boy", "girl", "dog", "town", "car"};
String[] verb={"drove", "jumped", "ran", "walk", "skipped"};
String[] preposition={"to", "from", "over", "under", "on"};
Frame frame=new Frame("test");
TextArea ta=new TextArea();

testWord(){
ta.setSize(350, 250);
frame.setSize(400,300);
frame.add(ta);
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}

private int[] getSerial(){
int[] result=new int[4];
Random r=new Random();
for(int i=0;i<4;i++)
result[i]=r.nextInt(5);
return result;
}

private String comb(String a, String n, String v, String p){
return a.substring(0,1).toUpperCase()+
a.substring(1,a.length())+" "+n+" "+v+" "+p+".";
}

public String getSentence(){
int[] serial=getSerial();
return comb(article[serial[0]], noun[serial[1]],
verb[serial[2]], preposition[serial[3]]);
}

private void output(){
String tmp="";
for(int i=0;i<20;i++) tmp=tmp+getSentence()+"\n";
ta.setText(tmp);
frame.setVisible(true);
}

public static void main(String[] args) {
testWord tw=new testWord();
tw.output();
}
}

 


查看完整回答
反對 回復 2022-02-14
?
精慕HU

TA貢獻1845條經驗 獲得超8個贊

import javax.swing.SwingUtilities;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JLabel;
import java.awt.Rectangle;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Random;
import javax.swing.SwingConstants;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JButton;

public class HelpTest extends JFrame {

private static final long serialVersionUID = 1L;

/**
* 下面的幾個數組是用來保存各個單詞的,可以自己添加其他的
*/
private String[] article = {"the","a","one","some","any"};
private String[] noun = {"boy ","girl","dog","town","car"};
private String[] verb = {"drove","jumped","ran","walk","skipped"};
private String[] preposition = {"to","from","over","under","on"};

private String sentence = "",word = null;//用來保存生成的句子 // @jve:decl-index=0:

private JPanel jContentPane = null;

private JLabel jLabel = null;

private JScrollPane jScrollPane = null;

private JTextArea jTextArea = null;

private JButton jButton = null;

private List addToList(){
List list = new ArrayList();
list.add(0, article);
list.add(1, noun);
list.add(2, verb);
list.add(3, preposition);
return list;
}
private String getSentence(){//獲取句子
List list = addToList();
Iterator it = list.iterator();
sentence = "";
for(int i=0;i<4;i++){
sentence = sentence + getWord((String[])it.next())+" ";
}
String cha = ""+sentence.charAt(0);
sentence = sentence.substring(1);
sentence = cha.toUpperCase()+sentence;
return sentence;
}
private String getWord(String[] words){
int len = words.length;
Random r = new Random();
int num = r.nextInt(len);
word = words[num];
return word;
}
private JScrollPane getJScrollPane() {
if (jScrollPane == null) {
jScrollPane = new JScrollPane();
jScrollPane.setBounds(new Rectangle(34, 57, 453, 123));
jScrollPane.setViewportView(getJTextArea());
}
return jScrollPane;
}

private JTextArea getJTextArea() {
if (jTextArea == null) {
jTextArea = new JTextArea();
jTextArea.setLineWrap(true);
jTextArea.setText("");
}
return jTextArea;
}

private JButton getJButton() {
if (jButton == null) {
jButton = new JButton();
jButton.setBounds(new Rectangle(229, 198, 62, 21));
jButton.setText("確定");
jButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
for(int i=0;i<20;i++){
jTextArea.append(getSentence()+"\n");
}
}
});
}
return jButton;
}

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
HelpTest thisClass = new HelpTest();
thisClass.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
thisClass.setVisible(true);
}
});
}

public HelpTest() {
super();
initialize();
}

private void initialize() {
this.setBounds(250, 200, 540, 275);
this.setContentPane(getJContentPane());
this.setTitle("句子生成器");
this.setResizable(false);
}

private JPanel getJContentPane() {
if (jContentPane == null) {
jLabel = new JLabel();
jLabel.setBounds(new Rectangle(145, 22, 224, 26));
jLabel.setHorizontalAlignment(SwingConstants.CENTER);
jLabel.setText("點擊下面的按鈕生成20句簡單的話");
jContentPane = new JPanel();
jContentPane.setLayout(null);
jContentPane.add(jLabel, null);
jContentPane.add(getJScrollPane(), null);
jContentPane.add(getJButton(), null);
}
return jContentPane;
}

}
是想要這種結果的嗎?我用的是Eclipse。不過在命令行下運行也是通過的。



查看完整回答
反對 回復 2022-02-14
?
慕妹3146593

TA貢獻1820條經驗 獲得超9個贊

好像挺簡單的...

import java.awt.*;

import javax.swing.*;

public class RandomSentence extends JFrame {
private String[] article = { "the", "a", "one", "some", "any" };
private String[] noun = { "boy", "girl", "dog", "town", "car" };
private String[] verb ={"drove", "jumped", "ran", "walk", "skipped" };

private String[] preposition = { "to", "from", "over","under","on" };

private JTextArea ta;

public RandomSentence() {
super("隨機創建句子");
ta = new JTextArea();
getContentPane().setLayout(new BorderLayout());
getContentPane().add("Center", new JScrollPane(ta));
setSize(300, 300);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

public void sentence() {
for (int i = 0; i < 20; i++) {
String s1 = article[(int) (Math.random() * article.length)];
String s11 = s1.substring(0, 1).toUpperCase().concat(s1.substring(1));

String s2 = noun[(int) (Math.random() * noun.length)];
String s3 = verb[(int) (Math.random() * verb.length)];
String s4 = preposition[(int) (Math.random() * preposition.length)];

StringBuffer s =
new StringBuffer(s11 + " " + s2 + " " + s3 + " "+ s4 + ".");

String str = s.toString();
ta.append(str + "\n");
}
}

public static void main(String[] args) {
new RandomSentence().sentence();

}

}



查看完整回答
反對 回復 2022-02-14
  • 3 回答
  • 0 關注
  • 206 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號