大家好 有哪位高手可以帮我解决有关这道JAVA计算机体呢 急需 谢谢啦~
大家好我是来自英国的学生由于刚上大学不久才接触到有关JAVA的题目再加上英语不是很好所以不会做这道题同时也问了好多朋友都是不会作业又要求马上交所以请求各位会的人帮我解决下...
大家好 我是来自英国的学生 由于刚上大学不久 才接触到有关JAVA的题目 再加上英语不是很好 所以不会做这道题 同时也问了好多朋友 都是不会 作业又要求马上交 所以请求各位会的人帮我解决下 谢谢了!
题目是:
Write a Java program that will generate random four letter words. The words must consist of only lower case letters. The program will prompt the user to enter the number of words to generate. The program should request that the user enter a number between 1 and 500 and ensure that they comply. It will then generate the appropriate number of words neatly displaying them separated by spaces 10 words to a line. After the words are displayed the program must display the first word and also the last word if they were sorted alphabetically.
懂的人帮我解决下 万分感谢了! 展开
题目是:
Write a Java program that will generate random four letter words. The words must consist of only lower case letters. The program will prompt the user to enter the number of words to generate. The program should request that the user enter a number between 1 and 500 and ensure that they comply. It will then generate the appropriate number of words neatly displaying them separated by spaces 10 words to a line. After the words are displayed the program must display the first word and also the last word if they were sorted alphabetically.
懂的人帮我解决下 万分感谢了! 展开
1个回答
展开全部
import java.util.Random;
import javax.swing.JOptionPane;
/**
*
* @author gang-liu
*/
public class MyWords {
private static final int WORD_LENGTH = 4;
private static final int WORDS_PER_LINE = 10;
private Random rand;
private char[] alphaTable = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i',
'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w',
'x', 'y', 'z'};
public MyWords(final int numOfWords) {
rand = new Random();
String min = null;
String max = null;
for (int round = 1; round <= numOfWords; round++) {
final String temp = getWord();
if (min == null) {
min = temp;
} else {
if (temp.compareTo(min) < 0) {
min = temp;
}
}
if (max == null) {
max = temp;
} else {
if (temp.compareTo(max) > 0) {
max = temp;
}
}
System.out.print(temp);
if ((round % WORDS_PER_LINE) == 0) {
System.out.print("\n");
} else {
System.out.print(" ");
}
}
System.out.println("\nThe Alphabetically Minimum word is :" + min);
System.out.println("The Alphabetically Maximum word is :" + max);
}
private String getWord() {
final StringBuilder sb = new StringBuilder();
for (int i = 0; i < WORD_LENGTH; i++) {
sb.append(alphaTable[rand.nextInt(alphaTable.length)]);
}
return sb.toString();
}
public static void main(String[] args) {
int numOfWords = 0;
while ((numOfWords < 1) || (numOfWords > 500)) {
try {
numOfWords = Integer.parseInt(JOptionPane.showInputDialog(
"Please enter an integer between 1 and 500"));
} catch (NumberFormatException ex) {
continue;
}
}
final MyWords mw = new MyWords(numOfWords);
}
}
import javax.swing.JOptionPane;
/**
*
* @author gang-liu
*/
public class MyWords {
private static final int WORD_LENGTH = 4;
private static final int WORDS_PER_LINE = 10;
private Random rand;
private char[] alphaTable = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i',
'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w',
'x', 'y', 'z'};
public MyWords(final int numOfWords) {
rand = new Random();
String min = null;
String max = null;
for (int round = 1; round <= numOfWords; round++) {
final String temp = getWord();
if (min == null) {
min = temp;
} else {
if (temp.compareTo(min) < 0) {
min = temp;
}
}
if (max == null) {
max = temp;
} else {
if (temp.compareTo(max) > 0) {
max = temp;
}
}
System.out.print(temp);
if ((round % WORDS_PER_LINE) == 0) {
System.out.print("\n");
} else {
System.out.print(" ");
}
}
System.out.println("\nThe Alphabetically Minimum word is :" + min);
System.out.println("The Alphabetically Maximum word is :" + max);
}
private String getWord() {
final StringBuilder sb = new StringBuilder();
for (int i = 0; i < WORD_LENGTH; i++) {
sb.append(alphaTable[rand.nextInt(alphaTable.length)]);
}
return sb.toString();
}
public static void main(String[] args) {
int numOfWords = 0;
while ((numOfWords < 1) || (numOfWords > 500)) {
try {
numOfWords = Integer.parseInt(JOptionPane.showInputDialog(
"Please enter an integer between 1 and 500"));
} catch (NumberFormatException ex) {
continue;
}
}
final MyWords mw = new MyWords(numOfWords);
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询