我试图获取存储在我的数组中的值(正确的答案),并将其与用户输入的答案进行比较。
这是我的数组:
String [][] multiArray = {{"4 + 5 = ", "9"},
{"20 * 3 - 1 = ","59"},
{"99 - 9 = ","90"},
{"50 / 2 + 18 = ","43"},
{"9 * 8 = ","72"},
{"4 + 20 - 20 = ","4"},
{"75 / 5 = ","15"},
{"99 - 1 * 3 = ","96"},
{"75 + 25 = ","100"}};
如果答案正确或不正确,这就是解决问题的方法。如果不正确并且选中了提示选项。它应该比较用户输入的答案和数组中的正确答案。但是,如果数组是随机生成的,我将如何从数组中获取值?
public void hash_Clicked(View v){
// Get the Answer from your EditText
String answer = display.getText().toString();
setAnswer.setText(answer);
// Using a for loop iterate on the base index
for(int i = 0; i < multiArray.length ; i++)
{
// if the answer is in position 1 of Array [i]
if(answer.equals(multiArray[i][1]))
{
// We have found the answer, Congratulate the User
displayAnswer.setTextColor(getResources().getColor(R.color.green));
displayAnswer.setText("CORRECT");
break;
}else{
// Tell them how bad they are since they can't solve simple equations!
displayAnswer.setTextColor(getResources().getColor(R.color.red));
displayAnswer.setText("INCORRECT");
for (int count = 0; count<5; count++){
if(Prefs.getHints(this)){
if (answer > multiArray[1]){
setHints.setText("greater");
}
}
}
}
}
}
这是生成随机表达式的代码:
public void onClick(View v) {
Random ranGenerate = new Random ();
int random = ranGenerate.nextInt(multiArray.length) ;
// Fetch your random question
String Rquestion = multiArray[random][0];
displayExpression.setText(Rquestion);
displayAnswer.setText("");
setAnswer.setText("?");
}
这是我需要帮助的地方:
if (answer > multiArray[i][1]){
setHints.setText("greater");
}
它说运营商&gt;未定义参数类型java.lang.String,java.lang.String。那我怎么从数组中获取值?
答案 0 :(得分:0)
首先,我认为你可能没有准确地描述你的问题。
正如你所说, public void onClick(View v)
似乎没有“生成”一个数组。它从现有multiArray[][]
中选择一个随机问题。
话虽如此,您可以将其中生成的随机值存储为变量,以便您始终知道用户当前正在回答的问题...
修改强>
看到你的编辑后。
您正在使用字符串,必须将它们转换为数字类型(int
,float
,double
等。)
// (Do some checkings before parsing, as the user may have inputted text...)
if (Integer.parseInt(answer) > Integer.parseInt(multiArray[i][1])){
setHints.setText("greater");
}
编辑2:
您可以将当前问题的位置存储在Activity
的变量中。每当您选择一个新的随机问题(在onClick(View v)
中)时,请记住该变量中所选问题的位置。
然后,当您想要验证用户的答案时,您知道答案在multiArray[random][1]
。解析用户答案的值以及从string
到 int 等的正确答案。
class MyActivity extends Activity{
int random; // store the current question...
String [][] multiArray = {{"4 + 5 = ", "9"},
{"20 * 3 - 1 = ","59"},
{"99 - 9 = ","90"},
{"50 / 2 + 18 = ","43"},
{"9 * 8 = ","72"},
{"4 + 20 - 20 = ","4"},
{"75 / 5 = ","15"},
{"99 - 1 * 3 = ","96"},
{"75 + 25 = ","100"}};
public void onClick(View v) {
Random ranGenerate = new Random ();
random = ranGenerate.nextInt(multiArray.length);
// Fetch your random question
String Rquestion = multiArray[random][0];
displayExpression.setText(Rquestion);
displayAnswer.setText("");
setAnswer.setText("?");
}
public void hash_Clicked(View v) {
// 1 - The correct answer for current question is in multiarray[i][1]
// 2 - Parse the strings to int/double/float
// 3 - Do your checkings...
// (careful as i might haven't been initialized)
}
}
答案 1 :(得分:0)
if (answer > multiArray[i][1]){
setHints.setText("greater");
}
您应该将两个字符串转换为整数以进行比较。
if (Integer.valueOf(answer) > Integer.valueOf(multiArray[i][1])) {
setHints.setText("greater");
}
或Integer.parseInt()