我如何打印打印数字替换“??”

时间:2012-03-06 16:53:25

标签: android

基本上我正在尝试更换“?”按任何键盘按钮。例如,如果问题显示为“0 + 1 =?”,我想要“1”来替换问号。

我的Java代码是:

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 answer0;
int question=0;
boolean q;




    @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);
        answer0 = (TextView)findViewById(R.id.Answer);

        ///Code for correct and 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) {

        switch(arg0.getId()){   
        case R.id.keypad_hash:

            //Generates random numbers
            fnum0 = (int) ((double) ((Math.random() * 10))); 
            snum0 = (int) ((double) ((Math.random() * 10))); 
            String str = ""; 

            //genrates random number between 0 to 9
            int operation = (int) ((double) ((Math.random() * 10)));
            Log.d("debug", "operation value: " + operation);


            if(operation == 0)  
               str = fnum0+  "+"  +  snum0+  "="+ "??"; 

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

        case R.id.keypad_1:
            if(q="?" )
            {
                display.setText("1");
            }
            else
            {
                return;
            }

            break;

        case R.id.keypad_2:
            display.setText("2");
            break;

        case R.id.keypad_3:
            display.setText("3");
            break;

        case R.id.keypad_4:
            display.setText("4");
            break;

        case R.id.keypad_5:
            display.setText("5");
            break;

        case R.id.keypad_6:
            display.setText("6");
            break;

        case R.id.keypad_7:
            display.setText("7");
            break;

        case R.id.keypad_8:
            display.setText("8");
            break;

        case R.id.keypad_9:
            display.setText("9");
            break;

        case R.id.delete:
            display.setText("");
            break;

        case R.id.keypad_0:
            display.setText("0");
            break;

        case R.id.keypad_subtract:
            display.setText("-");
            break;   
        }   
     }
    public void requestFocus() {
        // TODO Auto-generated method stub

    }
}

然而,此时它显示的问题是“0 + 1 =”但是当我按下它时它会移除问题并在屏幕上自行显示“1”。

更新错误

case R.id.keypad_1:

public void onClick(View arg0) {
            String str=null;
        switch(arg0.getId()){   
        case R.id.keypad_hash:

            //Generates random numbers
            fnum0 = (int) ((double) ((Math.random() * 10))); 
            snum0 = (int) ((double) ((Math.random() * 10))); 


            //genrates random number between 0 to 9
            int operation = (int) ((double) ((Math.random() * 10)));
            Log.d("debug", "operation value: " + operation);


            if(operation == 0)  
               str = fnum0+  "+"  +  snum0+  "="+ "??"; 

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

        case R.id.keypad_1:
            if(str.equals("??"))
            {
                String str1 = display.getText().toString(); 
                display.setText(str1.replace("??", "1")); 
            }
            else
            {
                return;
            }

            break;

应用程序正在崩溃.....

2 个答案:

答案 0 :(得分:1)

而不是

display.setText("1");

display.setText(str.replace("??", "1"));

替换方法表示替换“??” str字符串上带有“1”文本的文本,因此搜索“??”文本并将其替换为此字符串上的“1”文本。

比较字符串应使用equals方法,例如代替str==?,写:

str.equals("?");

替换keys_1案例的代码(删除if):

case R.id.keypad_1:
      String str1 = display.getText().toString(); 
      display.setText(str1.replace("??", "1")); 
break;

希望它有所帮助。

答案 1 :(得分:0)

我不确定所需的确切方法调用(已经有一段时间了),但您要做的是从显示中获取当前文本,修改它并重新显示它。我相信这会奏效:

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