我正在学习android并创建一个问答游戏。在游戏中有不同级别的新手,简单,中等,硬,有一部分我必须生成随机算术数学问题,我已设法做到这似乎有点像20 + 7,20-3,10 * 2,20 / 2(所有这些都是在游戏中随机的。)我需要帮助将游戏限制为最多十个问题。
任何帮助都会很棒。谢谢
以下是我的游戏类代码。
package com.example.quiz2;
import java.util.Random;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;
public class Play extends Activity implements OnClickListener {
//gameplay elements
private int level = 0, answer = 0, operator = 0, operand1 = 0, operand2 = 0,operand3= 0, operand4 = 0,operand5 = 0, operand6 = 0;
//operator constants
private final int ADD_OPERATOR = 0, SUBTRACT_OPERATOR = 1, MULTIPLY_OPERATOR = 2,
DIVIDE_OPERATOR = 3;
//operator text
private String[] operators = {"+", "-", "x", "/"};
//min and max for each level and operator
private int[][] levelMin = {
{1, 13, 21,28},
{1, 6, 12,16},
{2, 8, 13,18},
{2, 4, 7,12}};
private int[][] levelMax = {
{10, 25, 45,85},
{10, 20, 55,90},
{5, 10, 15,25},
{10, 50, 100,200}};
//random number generator
private Random random;
//ui elements
private TextView question, answerTxt;
private ImageView response;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_play);
//text
question = (TextView)findViewById(R.id.question);
answerTxt = (TextView)findViewById(R.id.answer);
response = (ImageView)findViewById(R.id.response);
//hide tick cross initially
response.setVisibility(View.INVISIBLE);
//number, enter and delete buttons
View btn1 = findViewById(R.id.btn1);//finding button with id
btn1.setOnClickListener(this);//listening for clicks
View btn2 = findViewById(R.id.btn2);
btn2.setOnClickListener(this);
View btn3 = findViewById(R.id.btn3);
btn3.setOnClickListener(this);
View btn4 = findViewById(R.id.btn4);
btn4.setOnClickListener(this);
View btn5 = findViewById(R.id.btn5);
btn5.setOnClickListener(this);
View btn6 = findViewById(R.id.btn6);
btn6.setOnClickListener(this);
View btn7 = findViewById(R.id.btn7);
btn7.setOnClickListener(this);
View btn8 = findViewById(R.id.btn8);
btn8.setOnClickListener(this);
View btn9 = findViewById(R.id.btn9);
btn9.setOnClickListener(this);
View btn0 = findViewById(R.id.btn0);
btn0.setOnClickListener(this);
View enter = findViewById(R.id.enter);
enter.setOnClickListener(this);
View del = findViewById(R.id.del);
del.setOnClickListener(this);
//get passed level number
Bundle extras = getIntent().getExtras();
if(extras !=null)
{
int passedLevel = extras.getInt("level", -1);
if(passedLevel>=0) level = passedLevel;
}
//initialize random
random = new Random();
//play
chooseQuestion();
}
@Override
public void onClick(View view) {
if(view.getId()==R.id.enter){
//enter button
//get answer
String answerContent = answerTxt.getText().toString();
//check we have an answer
if(!answerContent.endsWith("?")){
//get number
int enteredAnswer = Integer.parseInt(answerContent.substring(2));
//check answer
if(enteredAnswer==answer){
//correct
response.setImageResource(R.drawable.correct);
response.setVisibility(View.VISIBLE);
}
else{
//incorrect
response.setImageResource(R.drawable.wrong);
response.setVisibility(View.VISIBLE);
}
chooseQuestion();
}
}
else if(view.getId()==R.id.del){
//clear button
answerTxt.setText("=");
}
else {
//number button
response.setVisibility(View.INVISIBLE);
//get number from tag
int enteredNum = Integer.parseInt(view.getTag().toString());
//either first or subsequent digit
if(answerTxt.getText().toString().endsWith("?"))
answerTxt.setText("= "+enteredNum);
else
answerTxt.append(""+enteredNum);
}
}
//method generates questions
private void chooseQuestion(){
//reset answer text
answerTxt.setText("= ?");
//choose operator
operator = random.nextInt(operators.length);
//choose operands
operand1 = getOperand();
operand2 = getOperand();
operand3 = getOperand();
operand4 = getOperand();
operand5 = getOperand();
//checks for operators
if(operator==SUBTRACT_OPERATOR){
//no negative answers
while(operand2>operand1){
operand1 = getOperand();
operand2 = getOperand();
}
}
else if(operator==DIVIDE_OPERATOR){
//whole numbers only
while((((double)operand1/(double)operand2)%1 > 0)
|| (operand1==operand2)){
operand1 = getOperand();
operand2 = getOperand();
}
}
//calculate answer
switch(operator){
case ADD_OPERATOR:
answer = operand1+operand2-operand3;
break;
case SUBTRACT_OPERATOR:
answer = operand1-operand2+operand3;
break;
case MULTIPLY_OPERATOR:
answer = operand1*operand2;
break;
case DIVIDE_OPERATOR:
answer = operand1/operand2;
break;
default:
break;
}
//show question
question.setText(operand1+" "+operators[operator]+" "+operand3+operators[operator]+" "+operand4);
}
//method generates operands
private int getOperand(){
return random.nextInt(levelMax[operator][level] - levelMin[operator][level] + 1)
+ levelMin[operator][level];
}
}
答案 0 :(得分:0)
您可以使用java.random通过使用nextint方法将整数设置为随机数
http://developer.android.com/reference/java/util/Random.html