日语学习小工具(GUI版)(二)

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;
	}
}

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值