包含添加、删除、取随机单词
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;
public class VocabularyApp {
private ArrayList<Word> wordList;
public VocabularyApp() {
wordList = new ArrayList<>();
}
public void addWord(String word, String definition) {
Word newWord = new Word(word, definition);
wordList.add(newWord);
}
public void removeWord(String word) {
for (Word w : wordList) {
if (w.getWord().equals(word)) {
wordList.remove(w);
args.IsEmpty-=1;
args.SuccessfullyRemoved=true;
break;
}
}
}
public Word getRandomWord() {
Random random = new Random();
int index = random.nextInt(wordList.size());
return wordList.get(index);
}
public static class args{
public static long IsEmpty;
public static boolean SuccessfullyRemoved=false;
}
public void run() {
Scanner scanner = new Scanner(System.in);
boolean running = true;
args.IsEmpty = 0;
long EnoughTry=0;
while (running) {
System.out.println("What would you like to do?");
System.out.println("1. Add a word");
System.out.println("2. Remove a word");
System.out.println("3. Get a random word");
System.out.println("4. Quit");
String inputvalue=" ";
if (!scanner.hasNextInt()) {
System.out.println("Invalid input! Please enter a valid integer.");
scanner.next(); // 清空缓冲区
continue; // 重新开始下一轮循环
}
int choice = scanner.nextInt();
inputvalue= String.valueOf((int)choice);
//System.out.println(inputvalue);
int num = Integer.parseInt(inputvalue);//将String转变为Int
//System.out.println(num);
if(num==1||num==2||num==3||num==4){
switch (choice) {
case 1:
System.out.print("Enter the word: ");
String newWord = scanner.next();
System.out.print("Enter the definition: ");
String definition = scanner.next();
addWord(newWord, definition);
System.out.println("Added " + newWord);
args.IsEmpty+=1;
EnoughTry=0;
break;
case 2:
System.out.print("Enter the word to remove: ");
String wordToRemove = scanner.next();
removeWord(wordToRemove);
if(args.SuccessfullyRemoved){System.out.println("Removed " + wordToRemove);}
else{System.out.println("Word "+wordToRemove+" Unfindable");}
args.SuccessfullyRemoved=false;
//args.IsEmpty-=1;
break;
case 3:
if(EnoughTry>=3){System.out.println("FUCK YOU");System.exit(0);}
if(args.IsEmpty<=0){System.out.println("Vocabulary Empty,try again");EnoughTry++;break;}
if(args.IsEmpty>0){
Word randomWord = getRandomWord();
System.out.println("Random word: " + randomWord.getWord());
System.out.println("Definition: " + randomWord.getDefinition());
}
break;
case 4:
running = false;
break;
default:
//System.out.println("Invalid choice.");
break;
}
}
else{System.out.println("Invalid input! Please enter a valid integer.");}
}
}
public static void main(String[] args) {
VocabularyApp app = new VocabularyApp();
app.run();
}
}
class Word {
private String word;
private String definition;
public Word(String word, String definition) {
this.word = word;
this.definition = definition;
} public String getWord() {
return word;
}
public String getDefinition() {
return definition;
}
}
















