10-31
package ruanjiangouzao;
import java.util.*;
public class zuoye3 {
static int NUM=50,RANGE=100;
static Set<Quests> set = new HashSet<Quests>();
static Set<String> st = new HashSet<String>();
private static class Quests{
int x,y,z;
char op;
public Quests(int x, int y, char op,int z) {
this.x = x;
this.y = y;
this.op = op;
this.z=z;
}
public String getQuests(){
return String.format("%d"+op+"%d=",x,y);
}
public int getAnswer(){
return z;
}
}
private static void showTitle() {
System.out.println(String.format("----------- 程序输出50道100以内的加减法运算的口算题 ------------"));
}
//生成随机数
private static void geneQuests() {
Random random = new Random(System.nanoTime());
char op[]={'+','-'};
int num=1;
while(num<=NUM){
int x=random.nextInt(RANGE);
int y= random.nextInt(RANGE);
int idx= random.nextInt(2);
int z=operate(x,y,op[idx]);
if(ck(x,op[idx],y,z)){
continue;
}
st.add(""+x+op[idx]+y);
Quests quests = new Quests(x, y, op[idx],z);
set.add(quests);
num++;
}
}
//生成随机加减符号
private static int operate(int x, int y, char op) {
if(op=='+'){
return x+y;
}else{
return x-y;
}
}
//判断口算题答案是否在0~100范围内
private static boolean ck(int x,char op,int y,int z) {
return st.contains(""+x+op+y)&&z>=0&&z<=RANGE;
}
//显示随机口算题
private static void showQuests() {
System.out.println("题目");
int num=1;
for(Quests quests:set){
System.out.println(Integer.toString(num)+":"+quests.getQuests());
num++;
}
}
//显示答案
private static void showAnswer() {
System.out.println("答案");
int num=1;
for(Quests quests:set){
System.out.println(Integer.toString(num)+":"+quests.getAnswer());
num++;
}
}
//主函数
public static void main(String[] args) {
showTitle();
geneQuests();
showQuests();
showAnswer();
}
}