问题描述
任何帮助或建议都将受到极大地理解.我正在尝试创建一个生成十个不同随机问题的简单游戏.问题可以包含2,3或4个整数.所以这样的东西:55 2 - 4 - 101,102/3/3,589 - 281,123 + 5 6 + 2.
问题将在TextView中显示,然后用户可以猜测,将值输入到Edittext,然后在我创建的自定义键盘上单击密钥时,它将检查答案,然后显示下一个问题在10的序列中.
我知道如何创建随机数,只是努力解决如何用随机运算符创建一个整个问题(+, - /,*).
大致感谢任何有时间建立回复的人.
推荐答案
一段时间生成一个完整的例子为您的情况.创建new randomathquestiongenerator.java文件,它是编译的.
import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.Random; public class RandomMathQuestionGenerator { private static final int NUMBER_OF_QUESTIONS = 10; private static final int MIN_QUESTION_ELEMENTS = 2; private static final int MAX_QUESTION_ELEMENTS = 4; private static final int MIN_QUESTION_ELEMENT_VALUE = 1; private static final int MAX_QUESTION_ELEMENT_VALUE = 100; private final Random randomGenerator = new Random(); public static void main(String[] args) { RandomMathQuestionGenerator questionGenerator = new RandomMathQuestionGenerator(); List<Question> randomQuestions = questionGenerator.getGeneratedRandomQuestions(); for (Question question : randomQuestions) { System.out.println(question); } } public List<Question> getGeneratedRandomQuestions() { List<Question> randomQuestions = new ArrayList<Question>(NUMBER_OF_QUESTIONS); for (int i = 0; i < NUMBER_OF_QUESTIONS; i++) { int randomQuestionElementsCapacity = getRandomQuestionElementsCapacity(); Question question = new Question(randomQuestionElementsCapacity); for (int j = 0; j < randomQuestionElementsCapacity; j++) { boolean isLastIteration = j + 1 == randomQuestionElementsCapacity; QuestionElement questionElement = new QuestionElement(); questionElement.setValue(getRandomQuestionElementValue()); questionElement.setOperator(isLastIteration ? null : Operator.values()[randomGenerator.nextInt(Operator.values().length)]); question.addElement(questionElement); } randomQuestions.add(question); } return randomQuestions; } private int getRandomQuestionElementsCapacity() { return getRandomIntegerFromRange(MIN_QUESTION_ELEMENTS, MAX_QUESTION_ELEMENTS); } private int getRandomQuestionElementValue() { return getRandomIntegerFromRange(MIN_QUESTION_ELEMENT_VALUE, MAX_QUESTION_ELEMENT_VALUE); } private int getRandomIntegerFromRange(int min, int max) { return randomGenerator.nextInt(max - min + 1) + min; } } class Question { private List<QuestionElement> questionElements; public Question(int sizeOfQuestionElemets) { questionElements = new ArrayList<QuestionElement>(sizeOfQuestionElemets); } public void addElement(QuestionElement questionElement) { questionElements.add(questionElement); } public List<QuestionElement> getElements() { return questionElements; } public int size() { return questionElements.size(); } @Override public String toString() { StringBuilder sb = new StringBuilder(); for (QuestionElement questionElement : questionElements) { sb.append(questionElement); } return sb.toString().trim(); } } class QuestionElement { private int value; private Operator operator; public int getValue() { return value; } public void setValue(int value) { this.value = value; } public Operator getOperator() { return operator; } public void setOperator(Operator operator) { this.operator = operator; } @Override public String toString() { return value + (operator == null ? "" : " " + operator.getDisplayValue()) + " "; } } enum Operator { PLUS("+"), MINUS("-"), MULTIPLIER("*"), DIVIDER("/"); private String displayValue; private Operator(String displayValue) { this.displayValue = displayValue; } public String getDisplayValue() { return displayValue; } }
运行和预览.希望这有帮助.
感谢:
其他推荐答案
在范围[0,3]的范围内创建一个数组char[] ops = { '+', '-', '/', '*' }并在范围内创建随机int i,并选择ops[i]
您需要注意您不会以零问题生成鸿沟.
您可以通过创建interface MathOp并创建实现它的4个类更通用:Divide,Sum,...并创建数组:MathOp[] ops而不是char[]
使用这一点,它还可以让您更轻松地检查后面的结果...
其他推荐答案
将运算符放在数组(4个元素)中,从0到3生成随机整数,然后选择在阵列中此索引处的操作符.
每次需要有一个随机运算符,即除了最后一个之外的每个问题之后.
问题描述
Any help or advice would be greatly appreciated. I'm trying to create a simple game which generates ten different, random questions. The questions can contain 2, 3 or 4 integers. So something like this: 55 2 − 4 − 101, 102/3/3, 589 − 281, 123 + 5 6 + 2.
The question will be displayed in a textview and then the user can take a guess, entering values into an edittext and then upon clicking a key on a custom keypad I have created it will check the answer, and then display the next question in the sequence of 10.
I know how to create random numbers, just struggling to work out how to create a whole question with random operators (+, -, /, *).
Big thank you to anyone who has the time to construct a reply.
推荐答案
A little of spare time produced a complete example for your case. Create new RandomMathQuestionGenerator.java file and it is cooked for compilation.
import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.Random; public class RandomMathQuestionGenerator { private static final int NUMBER_OF_QUESTIONS = 10; private static final int MIN_QUESTION_ELEMENTS = 2; private static final int MAX_QUESTION_ELEMENTS = 4; private static final int MIN_QUESTION_ELEMENT_VALUE = 1; private static final int MAX_QUESTION_ELEMENT_VALUE = 100; private final Random randomGenerator = new Random(); public static void main(String[] args) { RandomMathQuestionGenerator questionGenerator = new RandomMathQuestionGenerator(); List<Question> randomQuestions = questionGenerator.getGeneratedRandomQuestions(); for (Question question : randomQuestions) { System.out.println(question); } } public List<Question> getGeneratedRandomQuestions() { List<Question> randomQuestions = new ArrayList<Question>(NUMBER_OF_QUESTIONS); for (int i = 0; i < NUMBER_OF_QUESTIONS; i++) { int randomQuestionElementsCapacity = getRandomQuestionElementsCapacity(); Question question = new Question(randomQuestionElementsCapacity); for (int j = 0; j < randomQuestionElementsCapacity; j++) { boolean isLastIteration = j + 1 == randomQuestionElementsCapacity; QuestionElement questionElement = new QuestionElement(); questionElement.setValue(getRandomQuestionElementValue()); questionElement.setOperator(isLastIteration ? null : Operator.values()[randomGenerator.nextInt(Operator.values().length)]); question.addElement(questionElement); } randomQuestions.add(question); } return randomQuestions; } private int getRandomQuestionElementsCapacity() { return getRandomIntegerFromRange(MIN_QUESTION_ELEMENTS, MAX_QUESTION_ELEMENTS); } private int getRandomQuestionElementValue() { return getRandomIntegerFromRange(MIN_QUESTION_ELEMENT_VALUE, MAX_QUESTION_ELEMENT_VALUE); } private int getRandomIntegerFromRange(int min, int max) { return randomGenerator.nextInt(max - min + 1) + min; } } class Question { private List<QuestionElement> questionElements; public Question(int sizeOfQuestionElemets) { questionElements = new ArrayList<QuestionElement>(sizeOfQuestionElemets); } public void addElement(QuestionElement questionElement) { questionElements.add(questionElement); } public List<QuestionElement> getElements() { return questionElements; } public int size() { return questionElements.size(); } @Override public String toString() { StringBuilder sb = new StringBuilder(); for (QuestionElement questionElement : questionElements) { sb.append(questionElement); } return sb.toString().trim(); } } class QuestionElement { private int value; private Operator operator; public int getValue() { return value; } public void setValue(int value) { this.value = value; } public Operator getOperator() { return operator; } public void setOperator(Operator operator) { this.operator = operator; } @Override public String toString() { return value + (operator == null ? "" : " " + operator.getDisplayValue()) + " "; } } enum Operator { PLUS("+"), MINUS("-"), MULTIPLIER("*"), DIVIDER("/"); private String displayValue; private Operator(String displayValue) { this.displayValue = displayValue; } public String getDisplayValue() { return displayValue; } }
Run and preview. Hope this helps.
Thanks to:
其他推荐答案
Create an array char[] ops = { '+', '-', '/', '*' } and create a random int i in range [0,3], and chose ops[i]
You will need to take care that you do not generate a divide by zero question.
You can make it even more generic by creating an interface MathOp and creating 4 classes that implement it: Divide, Sum , ... and create an array: MathOp[] ops instead of the char[]
Using this, it will also give you much easier time to check the result later on...
其他推荐答案
Put your operators in an array (4 elements), generate a random integer from 0 to 3, and pick the operator that is at this index in the array.
Do that each time you need to have a random operator, i.e. after every number of your question except the last one.