package st;
public class wordUtil {
/*** 每日一读单词随机抽取*/
private static final int[] SUCCESS = null;
// word 获取的英语单词
static String word;
// 随机数范围的最大值
static int max = 0;
// 随机数范围的最小值
static int min = 0;
// 生成数量
static int n;public static String testword(String word) {// 二次赋值wordUtil.word = word;wordUtil.max = word.length();wordUtil.min = 0;// 判断这个英语单词的长度int wordlength = word.length();//System.out.println("单词长度" + "t" + wordlength);// 对英语单词的长度进行判断if (wordlength <= 5) {// 单词数量小于或等于五个单词 抽空一个int n = 1;rult(min, max, n);} else if (wordlength >= 6 && wordlength <= 10) {// 单词数量小于或等于十个单词,大于等于六个 抽空两个int n = 2;rult(min, max, n);} else if (wordlength >= 11 && wordlength <= 15) {// 要随机的个数int n = 3;rult(min, max, n);} else if (wordlength >= 16) {// 要随机的个数int n = 4;rult(min, max, n);}return word;
}// 随机数去重
public static int[] randomCommon(int min, int max, int n) {if (n > (max - min + 1) || max < min) {return null;}int[] result = new int[n];int count = 0;while (count < n) {int num = (int) (Math.random() * (max - min)) + min;boolean flag = true;for (int j = 0; j < n; j++) {if (num == result[j]) {flag = false;break;}}if (flag) {result[count] = num;count++;}}return result;
}// 循环出打印的次数 并且打印出来 n=抽取次数
public static int[] rult(int min, int max, int n) {
// 调用上面的随机数去重方法,并且放入 min max n三个值
int[] randomCommon = randomCommon(min, max, n);
// System.out.println(word + " " + “随机抽取单词为:”);
for (int i = 0; i < n; i++) {
// 把抽到的随机数赋值给in
int in = randomCommon[i];
//输出抽到的随机数下标
System.out.print(in + " “);
// 把抽到的随机数按下标放进要抽空的单词里面
// char charAt = ;word.charAt(in)
// 把抽到的随机数放进下标里面进行判断 如果为空的话再次进行抽取
String str = String.valueOf(word.charAt(in));
//输出抽到的英语单词
System.out.println(” " + str);
// 对抽到的随机数进行判断
if (str.equals(" “)) {
System.out.println(”–抽到单词有空,重新抽取–");
rult(min, max, n);
break;
}
}
return SUCCESS;
}
}
测试类:
package st;
public class Test {
public static void main(String[] args) {//传一个字符串
String testword = stword("ABCD QWERTY UIP");
//进行随机抽取
System.out.println(testword);
}
}
加粗样式
本文发布于:2024-02-01 03:36:08,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/170672977033575.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |