我的按钮第一次工作,以解决方程,但不是第二次后,一个新的数字已放在编辑文本字段



我是一个热情的编码新手,所以我真的很感谢任何帮助我的问题。我正在设计一个小数学游戏,它生成一个随机数小于11,然后用户在编辑文本字段中输入一个数字,按下按钮。这个想法是,如果用户输入的随机数和用户输入的数字等于10,用户就得一分,如果他们标记错了,那么就会出现另一个随机生成的数字,让用户继续这个过程。

我的问题是,它似乎第一次工作,然后当用户第二次输入一个数字,按下按钮,它崩溃了。请原谅,对于你们这些专业人士来说,这似乎是一种混乱的编码,但要做到这一点,需要大量的教程和试验和错误。

    package com.example.mathtesttwo;

    public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    final TextView textOne = (TextView) findViewById(R.id.textView3);
    textOne.setText("4");
    final TextView guessText = (TextView) findViewById(R.id.textView2);
    final EditText userGuess = (EditText) findViewById(R.id.editText1);
    final TextView correctScore = (TextView) findViewById(R.id.textView1);
    final TextView wrongScore = (TextView) findViewById(R.id.textView4);
    wrongScore.setText("0");
    ////////////////////////

    String randText = "";
    Random randGen = new Random();
    int rando = randGen.nextInt(11);

    randText = Integer.toString(rando);
    textOne.setText(randText);
    /////////////////
    Button pushMe = (Button) findViewById(R.id.button1);
    pushMe.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

    int userNumber = Integer.parseInt(userGuess.getText().toString());
    int textNumber = Integer.parseInt(textOne.getText().toString());
    int highScore = Integer.parseInt(correctScore.getText().toString());
    int lowScore = Integer.parseInt(wrongScore.getText().toString());

            if ( userNumber== 10 - textNumber) {
                guessText.setText("you got it correct");
            correctScore.setText("correct:" +       highScore+1));


            } else {
                guessText.setText("WRONG");
                wrongScore.setText("wrong:" + (lowScore+1));
            }

            String randText = "";
            // TODO Auto-generated method stub
            Random randGen = new Random();
            int rando = randGen.nextInt(11);

            randText = Integer.toString(rando);
            textOne.setText(randText);
        }
    });

}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}
    }

LOGCAT

03-28 15:24:19.314: D/AndroidRuntime(959): Shutting down VM
03-28 15:24:19.314: W/dalvikvm(959): threadid=1: thread exiting with uncaught exception (group=0x414c4700)
03-28 15:24:19.484: E/AndroidRuntime(959): FATAL EXCEPTION: main
03-28 15:24:19.484: E/AndroidRuntime(959): java.lang.NumberFormatException: Invalid int: "correct:1"
03-28 15:24:19.484: E/AndroidRuntime(959):  at java.lang.Integer.invalidInt(Integer.java:138)
03-28 15:24:19.484: E/AndroidRuntime(959):  at java.lang.Integer.invalidInt(Integer.java:138)
03-28 15:24:19.484: E/AndroidRuntime(959):  at java.lang.Integer.parseInt(Integer.java:366)
03-28 15:24:19.484: E/AndroidRuntime(959):  at java.lang.Integer.parseInt(Integer.java:332)
03-28 15:24:19.484: E/AndroidRuntime(959):  at com.example.mathtesttwo.MainActivity$1.onClick(MainActivity.java:55)
03-28 15:24:19.484: E/AndroidRuntime(959):  at android.view.View.performClick(View.java:4240)
03-28 15:24:19.484: E/AndroidRuntime(959):  at android.view.View$PerformClick.run(View.java:17721)
03-28 15:24:19.484: E/AndroidRuntime(959):  at android.os.Handler.handleCallback(Handler.java:730)
03-28 15:24:19.484: E/AndroidRuntime(959):  at android.os.Handler.dispatchMessage(Handler.java:92)
03-28 15:24:19.484: E/AndroidRuntime(959):  at android.os.Looper.loop(Looper.java:137)
03-28 15:24:19.484: E/AndroidRuntime(959):  at android.app.ActivityThread.main(ActivityThread.java:5103)
03-28 15:24:19.484: E/AndroidRuntime(959):  at java.lang.reflect.Method.invokeNative(Native Method)
03-28 15:24:19.484: E/AndroidRuntime(959):  at java.lang.reflect.Method.invoke(Method.java:525)
03-28 15:24:19.484: E/AndroidRuntime(959):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
03-28 15:24:19.484: E/AndroidRuntime(959):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
03-28 15:24:19.484: E/AndroidRuntime(959):  at dalvik.system.NativeStart.main(Native Method)

试试这个…

OnClick中删除以下行

int highScore = Integer.parseInt(correctScore.getText().toString());
int lowScore = Integer.parseInt(wrongScore.getText().toString());
编辑:

int highScore = 0;
if(correctScore.getText().toString().contains("correct") || correctScore.getText().toString().contains("wrong")){
       String[] separated = correctScore.getText().toString().split(":");
       highScore = Integer.parseInt(separated[1]);
}else{
       highScore = Integer.parseInt(correctScore.getText().toString());
}
int lowScore = 0;
if(wrongScore.getText().toString().contains("correct") || wrongScore.getText().toString().contains("wrong")){
       String[] separated = wrongScore.getText().toString().split(":");
       lowScore = Integer.parseInt(separated[1]);
}else{
       lowScore = Integer.parseInt(wrongScore.getText().toString());
}

相关内容

  • 没有找到相关文章

最新更新