2014-02-20 25 views
0

I的值有两个数组:值1个阵列的等于另一个Android中

String []myExpressions = {"20+10","50+50","25+25","10+15"}; 
String []answers = {"30#","100#","50#","25#"}; 

当用户点击该按钮生成它从阵列myExpressions产生一个表达,并显示在文本字段。然后,我要求用户使用提供的按钮输入答案。答案显示在EditText中。当用户输入答案时,他们应该输入一个#(如提交按钮),如果是正确的答案,它应该在文本字段中显示正确。所以如果表达式数组中的位置是1,正确的答案是在同一位置的答案数组中。我将如何检查他们是否处于相同的位置?

例如:myExpressions[1]正确的答案是answers[1]

这里是我的代码:

package com.gamesup.braingame; 

import java.util.Random; 

import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.TextView; 

public class Easy extends Activity implements OnClickListener{ 

EditText display; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.easy); 

    display = (EditText)findViewById(R.id.displayText); 
    display.setText("?"); 

    final String []myExpressions = {"20+10","50+50","25+25","10+15"}; 
    final String []answers = {"30#","100#","50#","25#"}; 

    final TextView displayExpression = (TextView) findViewById(R.id.expression); 
    Button generate = (Button) findViewById(R.id.random_gen); 

    generate.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      Random ranGenerate = new Random(); 
      int random = ranGenerate.nextInt(4) ; 
      displayExpression.setText(myExpressions[random]); 
     } 
    }); 

} 

static boolean isEmpty = true; 

public void num_Clicked(View v){ 
    Button btn = (Button) findViewById(v.getId()); 
    //getting the button object and using a view to get the id of the buttons 

    if (v.getId()== R.id.del_button){ 
      String s = display.getText().toString(); 
      s = s.substring(0, s.length() - 1); 
      display.setText(s); 
      return; 
    } 


    if(isEmpty){ 
     display.setText(btn.getText()); 
     isEmpty = false; 
    } 
    else{ 
     display.append(btn.getText().toString()); 
     // storing the existing number into editText and current text is set 
     //to current button value 
     //display.setText(number+btn.getText().toString()); 
     //the total in the editText 
    } 

    if (v.getId()== R.id.hash_button){ 
     String userAns = display.getText().toString(); 



    } 

} 

@Override 
public void onClick(View v) { 
    // TODO Auto-generated method stub 

} 


} 
+0

清楚地说明什么是不工作或什么是错的? –

+0

我怎么会得到myExpressions数组相等的答案数组? – sumr

回答

0

对于Java的初学者数组索引0开头因此,因此,要比较你应该使用这样的检查,如果事情都是平等的数组的第一个项目:

EditText myAnswer = (EditText) findViewById(R.id.myAnswer); 

String answer = myAnswer.getText().toString(); 

// Notice my loop starts at 0 because the first index of an array is 0 
for(int i = 0 ; i < answers.length; i++) 
{ 
    if(answers[i].equals(answer)) 
    { 
    // Congratulate the User for getting it Right 


    } 
} 

看起来好像你有一点摇摇欲坠的逻辑。恕我直言,你应该使用多维数组。

使用多维数组,您可以基本设置键和值。

这是我想你的应用程序应该是配置

// This Array says , I am an array that holds arrays 
String [][] multiArray = {{"4 + 5", "9"},  
          {"20 * 3","60"}, 
          {"99 - 9","90"}}; 

// Fetch your random question, since we know our questions are the first item in out Array we can use the index [x][0] depending on the index you pull from 
String question = multiArray[0][0]; 

// To do this randomly 
Random ranGenerate = new Random(); 
int random = ranGenerate.nextInt(4) ; 
String question = multiArray[random][0]; 

// Get the Answer from yout EditText 
String answer = myAnswer.getText().toString(); 

// 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(mutliArray[i][1]) 
     { 
     // We have found the answer, Congratulate the User 

     }else{ 
     // Tell them how bad they are since they can't solve simple equations! 
     // ....joking obviously we would be nice and let them know its not the answer 
     } 

} 
+0

是的,我认为这是我之后的事情。我没有真正使用多维数组。你可以在我给出的代码中编辑它,因为它说我需要在for循环中进行本地定义。我已经尝试了onClick方法和onCreate方法。我也不知道在哪里我将不得不添加此代码:/ – sumr

+0

编辑:它的工作原理,我没有意识到multiArray没有正确拼写在for循环。谢谢。 – sumr

+0

没问题,我喜欢帮忙!请记住,尽管关于索引,你可能会看到一些你并不期望的东西,只记得如果你选择multiArray [x] [0],你会得到元素数组中的第一个元素(你的问题) x的数组,其中x可以是小于数组长度的任何数字。 –

0

在这一行

int random = ranGenerate.nextInt(4) ; 

你为什么不使random您的类中的实例变量?这样您将保留索引,并且您将知道使用哪个索引来比较答案。

相关问题