RandomWord.java
package com.gary.util.japanese;
import java.util.Random;
/**
* 随机,帮助记忆五十音图
* @author gary
*
*/
public class RandomWord {
/**
* 随机平假名
* @return
*/
public static String randomHiranagana(){
String word = null;
try{
word = JapaneseUtil.hiranagana_words[new Random().nextInt(12)][new Random().nextInt(6)];
if(JapaneseUtil.emptyStr.equals(word)){
word = randomHiranagana();
}
}catch(ArrayIndexOutOfBoundsException e){
word = randomHiranagana();
}
return word;
}
/**
* 随机片假名
* @return
*/
public static String randomKatakana(){
String word = null;
try{
word = JapaneseUtil.katakana_words[new Random().nextInt(12)][new Random().nextInt(6)];
if(JapaneseUtil.emptyStr.equals(word)){
word = randomKatakana();
}
}catch(ArrayIndexOutOfBoundsException e){
word = randomKatakana();
}
return word;
}
/**
* 随机罗马音
* @return
*/
public static String randomSound(){
String sound = null;
try{
sound = JapaneseUtil.sounds[new Random().nextInt(12)][new Random().nextInt(6)];
if(JapaneseUtil.emptyStr.equals(sound)){
sound = randomSound();
}
}catch(ArrayIndexOutOfBoundsException e){
sound = randomSound();
}
return sound;
}
/**
* 随机平假名+片假名
* @return
*/
public static String randomAllWords(){
int switchWords = new Random().nextInt(2);
if(switchWords == 0){
return randomHiranagana();
}else{
return randomKatakana();
}
}
/**
* 随机全部
* @return
*/
public static String randomAll(){
int flag = new Random().nextInt(3);
if(flag == 0){
return randomHiranagana();
}else if(flag == 1){
return randomKatakana();
}else{
return randomSound();
}
}
}
RandomWords.java
package com.gary.util.japanese;
import java.io.File;
import java.util.List;
import java.util.Map;
import java.util.Random;
import com.gary.util.jxl.JXLUtil;
/**
* 背单词辅助工具
* @author gary
*
*/
public class RandomWords {
private static List<Map<String,String>> data;
public static Random random;
static{
data = new JXLUtil().getImportData(new File("words.xls"), "japanese");
random = new Random();
}
/**
* 随机取出日语
* @return
*/
public static String randomJapanese(){
return ((Map<String,String>)data.get(random.nextInt(data.size()))).get("japanese");
}
/**
* 随机取出汉语
* @return
*/
public static String randomChinese(){
return ((Map<String,String>)data.get(random.nextInt(data.size()))).get("chinese");
}
/**
* 随机取出单词
* @return
*/
public static String randomAll(){
int flag = random.nextInt(2);
if(flag == 0){
return randomJapanese();
}else{
return randomChinese();
}
}
}
JXLUtil.java
package com.gary.util.jxl;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import jxl.Sheet;
import jxl.Workbook;
/**
* 读取Excel
* @author gary
*
*/
public class JXLUtil {
public List<Map<String,String>> getImportData(File file, String target){
List<Map<String, String>> data = new ArrayList<Map<String, String>>();
try {
Workbook book = Workbook.getWorkbook(file);
Sheet sheet = book.getSheet(0);
int rows = sheet.getRows();
int columns = sheet.getColumns();
boolean hasText = false;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++)
if (sheet.getCell(j, i).getContents() != "") {
hasText = true;
break;
}
if (hasText) {
Map<String, String> temp = new HashMap<String, String>();
String excelKey = "japanese,chinese";
String[] excelKeyArray = excelKey.split(",");
for(int k = 0 ; k < excelKeyArray.length ; k++){
temp.put(excelKeyArray[k], sheet.getCell(k, i).getContents());
}
data.add(temp);
}
}
book.close();
} catch (Exception e) {
e.printStackTrace();
}
return data;
}
}