2012-03-09 38 views
1

我想在按下兩次keypad_hash時顯示下一個表達式。當按下兩次keypad_hash時顯示下一個表達式

我能夠在此刻做的是:

  • 顯示「正確」的時候,答案是正確的,當按下keypad_hash一次是正常的
  • 當我按下keypad_hash再次它不」 t做任何事情

如果我的觀察是正確的「點擊= 1」在第一個中頻站沒有做任何事情。

if ("2+3=5".equals(display.getText().toString())) 
     { 
      correct.setText("CORRECT"); 
      click=1; 
     } 

有什麼建議嗎?


package org.example.question; 

import java.util.Random; 



import android.app.Activity; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.View; 
import android.widget.Button; 
import android.widget.TextView; 

public class QuestionActivity extends Activity implements View.OnClickListener { 
    /** Called when the activity is first created. */ 

    //variable for different questions 
    int fnum0, snum0,fnum1, snum1,fnum2, snum2,fnum3, snum3, 
    fnum4, snum4,fnum5, snum5,fnum6, snum6,fnum7, snum7, 
    fnum8, snum8,fnum9, snum9, answer; 


    //variable and type declaration for buttons and text 
Button keyOne; 
Button keyTwo; 
Button keyThree; 
Button keyFour; 
Button keyFive; 
Button keySix; 
Button keySeven; 
Button keyEight; 
Button keyNine; 
Button keyDel; 
Button keyZero; 
Button keyHash; 
Button keySubtract; 
TextView display; 
TextView display1; 
TextView correct; 
TextView Incorrect; 
int q=0; 
int click=0; 





    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     //display text on screen 

     display1 = (TextView)findViewById(R.id.Guess); 
     display = (TextView)findViewById(R.id.Title); 



     correct= (TextView)findViewById(R.id.CORRECT); 
     Incorrect= (TextView)findViewById(R.id.INCORRECT); 




     //assigning names to each keypad 
     keyOne= (Button) findViewById(R.id.keypad_1); 
     keyTwo= (Button) findViewById(R.id.keypad_2); 
     keyThree= (Button) findViewById(R.id.keypad_3); 
     keyFour= (Button) findViewById(R.id.keypad_4); 
     keyFive = (Button) findViewById(R.id.keypad_5); 
     keySix= (Button) findViewById(R.id.keypad_6); 
     keySeven = (Button) findViewById(R.id.keypad_7); 
     keyEight = (Button) findViewById(R.id.keypad_8); 
     keyNine = (Button) findViewById(R.id.keypad_9); 
     keyZero = (Button) findViewById(R.id.keypad_0); 
     keySubtract = (Button) findViewById(R.id.keypad_subtract); 
     keyHash = (Button) findViewById(R.id.keypad_hash); 
     keyDel = (Button) findViewById(R.id.delete); 

     //setting button to produce an event when each button is pressed 
     keyOne.setOnClickListener(this);  keyTwo.setOnClickListener(this);  keyThree.setOnClickListener(this); 
     keyFour.setOnClickListener(this); keyFive.setOnClickListener(this);  keySix.setOnClickListener(this);  
     keySeven.setOnClickListener(this); keyEight.setOnClickListener(this); keyNine.setOnClickListener(this);  
     keySubtract.setOnClickListener(this); keyHash.setOnClickListener(this); keyDel.setOnClickListener(this); 



} 

    public void onClick(View arg0) { 

     //created a new string object 
     String str=new String(); 
     int click=0; 
     int q=0; 


     switch(arg0.getId()){ 

     //what happens when hash button is pressed 

     case R.id.keypad_hash: 


      if ("2+3=5".equals(display.getText().toString())) 
      { 
       correct.setText("CORRECT"); 
       click=1; 
      } 

      else if (click==1 && q==1 && "CORRECT".equals(display1.getText().toString())) 

      { 
       fnum0=(int) ((double) ((Math.random() * 10))); 
       snum0=(int) ((double) ((Math.random() * 10))); 

       int operation = (int) ((double) ((Math.random() * 10))); 

       if(operation == 0) 
        str = fnum0+ "+" + snum0+ "=" + ((fnum0+snum0<10)? "?" : "??"); 
       else if(operation == 1) 
        str = fnum0+ "-" + snum0+ "=" + ((fnum0+snum0<10)? "?" : "??"); 
       else if(operation == 2) 
        str = fnum0+ "*" + snum0+ "=" + ((fnum0+snum0<10)? "?" : "??"); 
       else if(operation == 0) 
        str = fnum0+ "/" + snum0+ "=" + ((fnum0+snum0<10)? "?" : "??"); 
       display.setText(str); 
          display.setText(fnum0+"+"+snum0+"="); 

          click=2; 

      } 

     case R.id.keypad_1: 

      String str1 = display.getText().toString(); 
      display.setText(str1.replace("?", "1")); 
     break; 



     case R.id.keypad_2: 
      String str2 = display.getText().toString(); 
      display.setText(str2.replace("?", "2")); 
      break; 

     case R.id.keypad_3: 
      String str3 = display.getText().toString(); 
      display.setText(str3.replace("?", "3")); 
      break; 

     case R.id.keypad_4: 
      String str4 = display.getText().toString(); 
      display.setText(str4.replace("?", "4")); 
      break; 

     case R.id.keypad_5: 
      String str5 = display.getText().toString(); 
      display.setText(str5.replace("?", "5")); 
      break; 

     case R.id.keypad_6: 
      String str6 = display.getText().toString(); 
      display.setText(str6.replace("?", "6")); 
      break; 

     case R.id.keypad_7: 
      String str7 = display.getText().toString(); 
      display.setText(str7.replace("?", "7")); 
      break; 

     case R.id.keypad_8: 
      String str8 = display.getText().toString(); 
      display.setText(str8.replace("?", "8")); 
      break; 

     case R.id.keypad_9: 
      String str9 = display.getText().toString(); 
      display.setText(str9.replace("?", "9")); 
      break; 


     case R.id.keypad_0: 
      String str0 = display.getText().toString(); 
      display.setText(str0.replace("?", "0")); 
      break; 

     case R.id.keypad_subtract: 
     display.setText("-"); 
     break; 
     } 
    } 

    public void requestFocus() { 
     // TODO Auto-generated method stub 

    } 
} 

回答

0

你正在創建的每一個按鈕按下特定click的新方法。

public void onClick(View arg0) { 

    //created a new string object 
    String str=new String(); 
    int click=0; /// <---- here 
    int q=0; 

這是什麼被引用,並且永遠不會在方法調用之間增加。

+0

我在每次按下按鈕時都從「click = 1」更改爲「click = 0」,但仍然沒有得到任何隨機表達式 – 2012-03-09 12:20:40

+0

您需要一併刪除此行,並在您實際需要時重置「click = 0」它。 – cjk 2012-03-09 14:37:30

+0

當我刪除該行時,出現錯誤說「在IF語句中創建一個局部變量int click = 0」,這意味着它不允許我在If語句中使用「click = 0」,除非我聲明並初始化它 – 2012-03-09 15:07:52

相關問題