- 流程图
- 代码思路流程图
- 代码设计流程图
- 注意的问题
- Scanner next()与nextline()的区别 :
一句话 nextline()可以输入空格、tab等特殊的字符,而且还可以避免在连续输入多个字符串的时候出现输入问题
- 父类与子类在这里尽量不要重名
- 代码
package MyDictionary;
public class Word {
String eWord;
String cWord;
public String geteWord() {
return eWord;
}
public void seteWord(String eWord) {
this.eWord = eWord;
}
public String getcWord() {
return cWord;
}
public void setcWord(String cWord) {
this.cWord = cWord;
}
public Word(String eWord, String cWord) {
this.eWord = eWord;
this.cWord = cWord;
}
public Word() {
this.eWord = null;
this.cWord = null;
}
public String toString() {
return (this.eWord + "===" + this.cWord);
}
}
package MyDictionary;
public class dictionaryFuction {
Word[] words;
int count;
public dictionaryFuction() {
words = new Word[100];
count = 0;
}
public boolean add(Word word) {
if (count < 100) {
this.words[this.count++] = word;
return true;
} else
return false;
}
public Word searchWord(String word) {
for (int i = 0; i < count; i++) {
if (this.words[i].geteWord().equalsIgnoreCase(word) || this.words[i].getcWord().equalsIgnoreCase(word))
return words[i];
}
return null;
}
public void listAllWords() {
System.out.println("以下是所有的单词");
for (int i=0;i<count;i++)
System.out.println(this.words[i].toString());
}
public boolean deleteWords(String word) {
int location = -1;
for (int i = 0; i < count; i++) {
if (this.words[i].geteWord().equalsIgnoreCase(word) || this.words[i].getcWord().equalsIgnoreCase(word)) {
location = i;
break;
}
}
if (location >= 0) {
for (int i = location; i < count - 1; i++) {
words[i] = words[i + 1];
}
count--;
return true;
}
else
return false;
}
public boolean updateWord(Word word) {
if (word != null) {
int i = indexOf(word.getcWord());
if (i >= 0) {
this.words[i] = word;
return true;
} else
return false;
} else
return false;
}
private int indexOf(String str) {
if (str == null)
return -1;
else {
for (int i = 0; i < this.count; i++) {
if (this.words[i].getcWord().equals(str))
return i;
}
return -1;
}
}
}
package MyDictionary;
import java.util.*;
public class LookUpDictionary {
dictionaryFuction my;
Scanner input;
public LookUpDictionary() {
my = new dictionaryFuction();
input = new Scanner(System.in);
}
public void welcome() {
System.out.println("****************");
System.out.println("欢迎使用我的字典");
System.out.println("****************");
}
public void lookUp() {
char choice;
do {
System.out.println("1、查单词");
System.out.println("2、加单词");
System.out.println("3、删除单词");
System.out.println("4、产看所有单词");
System.out.println("5、修改单词");
System.out.println("0、退出");
choice = input.nextLine().charAt(0);
switch (choice) {
case '1':
showWord();
break;
case '2':
showAdd();
break;
case '3':
showDel();
break;
case '4':
my.listAllWords();
break;
case '5':
showUpdata();
break;
default:
break;
}
} while (choice != '0');
}
public void showWord() {
System.out.println("请输入你要产找单词的中英文");
String str = input.nextLine();
Word word = my.searchWord(str);
if (word != null) {
System.out.println("产找单词的中英文含义为:");
System.out.println(word.toString());
} else
System.out.println("这个单词没找到");
}
public void showAdd() {
if (my.count < 100) {
System.out.println("请输入你想要添加单词的中文含义");
String Cword = input.nextLine();
System.out.println("请输入你想要添加单词的英文含义");
String Eword = input.nextLine();
Word word = new Word(Eword, Cword);
my.add(word);
System.out.println("添加单词成功");
} else
System.out.println("字典已满");
}
public void showDel() {
System.out.println("请输入你要删除的单词的含义");
String string = input.nextLine();
boolean ifDel = my.deleteWords(string);
if (ifDel)
System.out.println("删除成功");
else
System.out.println("删除失败!!!");
}
public void showUpdata() {
System.out.println("仅能修改单词的英文含义");
System.out.println("请输入你想要修改单词的中文含义");
String cStr = input.nextLine();
System.out.println("请输入单词修改后的英文含义");
String eStr = input.nextLine();
Word word = new Word(eStr, cStr);
if (my.updateWord(word))
System.out.println("单词修改成功");
else
System.out.println("词典中没有这个单词");
}
public void bye() {
System.out.println("****************");
System.out.println("Bye —Bye");
System.out.println("****************");
}
public void run() {
welcome();
lookUp();
bye();
}
}
package MyDictionary;
public class dictionaryDemo {
public static void main(String[] args) {
LookUpDictionary Demo =new LookUpDictionary();
Demo.run();
}
}