Android App猜测游戏限制猜测

时间:2017-02-11 15:25:29

标签: java android if-statement while-loop

我再次

试图限制我的机器人猜测游戏中猜测的次数,但是当我们试图比较其中一个已经转换为字符串的两个整数时,它正在努力:

while (userGuess != userNumber && ++attempts < maxAttempts) ;

我不确定如何更改它。

顺便说一下,我知道现在我的代码将直接进入最后的if语句,每次按下猜测按钮,它都会说我已经有3次尝试。

由于

package lab.mad.cct.c3375331task1;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import java.util.Random;

public class Task1Activity extends AppCompatActivity {

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

        final TextView textResponse = (TextView)  findViewById(R.id.txtResponse);
        final TextView guessText = (TextView) findViewById(R.id.txtAnswer);
        final EditText userGuess = (EditText) findViewById(R.id.etNumber);

        Button pressMe = (Button) findViewById(R.id.btnGuess);



        // When the button is clicked, it shows the text assigned to the txtResponse TextView box
        pressMe.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String randText = "";
                Random randGen = new Random();
                int ranNum = randGen.nextInt(5);
                int userNumber = Integer.parseInt(userGuess.getText().toString());
                int attempts = 0;
                final int maxAttempts = 3;

                while (userGuess != userNumber && ++attempts < maxAttempts) ;

                if (userNumber >19 ) {
                    guessText.setText("Please guess between 0 and 20");
                } else if (userNumber == ranNum) {
                    guessText.setText("You did it!");
                    guessText.setBackgroundColor(Color.WHITE);
                } else if (userNumber < ranNum) {
                    guessText.setText("Your answer is too low. Guess  again!");
                    guessText.setBackgroundColor(Color.RED);
                } else if (userNumber > ranNum) {
                    guessText.setText("Your answer is too high.  Guess again!");
                    guessText.setBackgroundColor(Color.RED);
                }



                if (attempts == maxAttempts)
                    guessText.setText("You have guessed incorrectly three times.  The answer was" + ranNum);

                randText = Integer.toString(ranNum);
                textResponse.setText("");

                userGuess.setText("");

            }  
        });

    } 



}

1 个答案:

答案 0 :(得分:0)

运营商&#39;!=&#39;不能应用于&#39; android.widget.EditText&#39;,&#39; int&#39;。

由于错误显示userGuessuserNumber无法像这样进行比较,因为它们在int中是不同的,而android.widget.EditText则是gettext()

您应首先在userGuess上使用userNumber,然后将其与String进行比较,方法是稍后转换为int或将前者转换为 while(userGuess.getText().equals(String.valueOf(ranNum) && attempts++ < maxAttempts )

试试这个,

attempts

同时将while loop声明为全局,您将在每次不需要的点击时重置它。如果您要执行3次点击,请移除userNumber。同时删除pub fn sum(a: i32, b: i32) -> i32 { return a + b; } pub fn just_print() { println!("I am just_print() function. I just say hello!"); } #[test] fn rusttest_foo_sum1() { assert!(sum(3, 4) == 7); } #[test] fn rusttest_foo_sum2() { assert!(sum(4, 5) == 9); } #[test] fn rusttest_foo_sum3() { assert!(sum(5, 6) == 11); } #[test] fn rusttest_foo_sum4() { assert!(sum(5, 6) == 11); } ,也不需要它。