2014-02-20 4 views
0

다른 값과 같다 :값 I가 두 배열이 안드로이드

사용자가 배열 myExpressions에서 표현을 생성 생성 버튼을 클릭하여 텍스트 필드에 표시
String []myExpressions = {"20+10","50+50","25+25","10+15"}; 
String []answers = {"30#","100#","50#","25#"}; 

. 그런 다음 제공된 단추를 사용하여 사용자가 대답을 입력하도록합니다. 대답은 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

, 그래서 때문에 인덱스 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 


    } 
} 

다소 흔들린 논리가있는 것처럼 보입니다. IMHO 당신은 다차원 배열을 사용해야합니다.

다차원 배열을 사용하면 기본적으로 키와 값을 설정할 수 있습니다.

이 내가

int random = ranGenerate.nextInt(4) ; 

가 왜 random 클래스 내의 인스턴스 변수하지 않는해야 응용 프로그램이 라인에서

// 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

편집 : 그것은 작동합니다, 나는 멀티 루프가 for 루프에서 철자가 맞지 않았다는 것을 깨닫지 못했습니다. 고맙습니다. – sumr

+0

문제 없습니다, 도와 드리겠습니다! 색인에 대해서도 명심하십시오. multiArray [x] [0]을 선택하면 요소의 배열에서 첫 번째 요소 (색인)를 가져올 것임을 기억하지 못할 수도 있습니다 배열의 x 여기서 x는 배열 길이보다 작은 임의의 수입니다. –

0

를 구성 어떻게 생각입니까? 이렇게하면 색인을 보존 할 수 있고 어떤 색인을 사용하여 대답을 비교할 지 알 수 있습니다.