From the whim of memorizing English IV words:
Is it possible to implement a program that randomly selects words from essays for review through the Java language.
First, show the results:
Click to view code
package Demo;
import ;
import ;
import ;
public class random_words {
public static void main(String[] args) {
//Import English essay data
String data = "***English essay data abcdefg***" ;
ArrayList<String> list = new ArrayList<>(); //store all words with length greater than 3
ArrayList<String> doneList = new ArrayList<>();//store words that have already appeared
Random r = new Random();; // Store the words that have appeared.
Scanner sc = new Scanner();
// Split the string by spaces and punctuation marks
String[] words = ("[\\\p{Punct}\\\s+]");
// Add words longer than 3 to the list.
for (String word : words){
if (()>3 && ! ()) { // if () >3 && !
(word); }
}
}
//Generate 5 non-repeating words
boolean choose = true;// control loop
while(choose && ()>()){
ArrayList<String> nowList= new ArrayList<>();// for storing the generated 5 words
while (()<5 && ()> ()){
int n = (());
String randomWords = (n);
// Check if the set of words are duplicates
if (! (randomWords) && ! (randomWords)) {
(randomWords); !
(randomWords).
}
}
// Output the generated words
("Please review:"); }
for (String word:nowList) {
(word); }
}
//Check if the number of remaining words is less than 5
if (()< ()) {
("Whether to continue reviewing (y/n)"); }
String userChick = ();
if (! ("y")) {
choose = false; }
}
}else{
for (String word:list) {
if (! (word)) {
(word); }
}
}
break; }
}
}
("The current word has been reviewed, please change to the next one.");
();
}
}
Instructions for use:
Just put an English article into the DATA data and run it.
Program Run Logic:
Randomly selected articles more than 4 letters of the English words, every five words for a group, according to the program prompt (y/n)? Input "y" to continue to review the next group of words, on the contrary, input "n" to end the program. When the remaining words of the article can not come up with 5 words / group requirements, the direct output of the remaining words.
The realization process and the knowledge literacy involved (written for myself):
-
The initial concept considered only segmented words, from which one word was randomly selected for review. It was found that reviewing one word at a time was too slow and the words that appeared were prepositions, which were not useful for English review.
-
Solution:
Add a while loop with groups of 5 words; exclude words under 4 letters and add an ArrayList collection (list) for storing all words longer than 3 letters.
-
Code:
Click to view code
ArrayList<String> list = new ArrayList<>();//store all words of length greater than 3
while(choose && ()>()){
ArrayList<String> nowList= new ArrayList<>();//for storing the generated 5 words
while (()<5 && ()> ()){
int n = (());
String randomWords = (n);
-
Upon completion, it was found that multiple randomly generated data in a fixed document had duplication and the presence of less than five words remaining in the final set.
-
Solution:
Add collection, add ArrayList(doneList): generated words and ArrayList(NowList): is generating 5 words on top of the already existing list collection for solving the repetitiveness problem.
-
Code:
Click to view code
ArrayList<String> doneList = new ArrayList<>();//to store the words that have appeared
ArrayList<String> nowList= new ArrayList<>();// for storing the generated 5 words
-
Code:
Click to view code
if (!(randomWords) && !(randomWords)) {
(randomWords);
(randomWords);
}
Set the choose control program, the initial value is true, if the doneList is smaller than the list, ask whether to continue (y/n)? Users choose "Y", print the next set of words, the remaining words less than 5 direct printing; choose "N", return to choose the value of false, the end of the program run.
-
Code:
Click to view code
if (()< ()) {
("Whether to continue reviewing(y/n)");
String userChick = ();
if (!("y")) {
choose = false;
}
}else{
for (String word:list) {
if (!(word)) {
(word);
}
}
break;
}
@andmin_tai rookie original
I would like to ask for a correction!