Java简单单词本

包含添加、删除、取随机单词

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;
    }
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值