gpt4 book ai didi

java - 将 Android 猜谜游戏限制为只能猜测 3 次

转载 作者:行者123 更新时间:2023-12-01 08:53:05 26 4
gpt4 key购买 nike

我正在做一个 Android 应用程序猜测游戏,但我似乎无法将其限制为三个。此外,每次用户单击猜测时,它都会生成一个新的随机数,但我不知道如何制作它,以便生成的数字在用户猜测期间保持不变。

非常感谢任何帮助。

谢谢

package lab.mad.cct.c3375331task1;

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 {

int attempts = 0;
final int maxAttempts = 3;
Random randGen = new Random();
int ranNum;

@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);

randGen = new Random();
// Generate number once
ranNum = randGen.nextInt(5);


// 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());


if (userNumber > 19) {
guessText.setText("Please guess between 0 and 20");
} 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);
} else if (userNumber == ranNum) {
ranNum = randGen.nextInt(5);
guessText.setText("You did it!");
guessText.setBackgroundColor(Color.WHITE);
}

while (ranNum != userNumber && attempts++ < maxAttempts) {

if (attempts == maxAttempts) {

guessText.setText("You have guessed incorrectly three times. The answer was " + ranNum);
}
}
randText = Integer.toString(ranNum);
textResponse.setText("");

userGuess.setText("");
}
}
);

}
}

最佳答案

您在 onClick 函数中第二次实例化 ranNum。摆脱这个。第二:您应该在 onClick 函数之外创建一个计数器,以计算用户尝试次数并在每次用户点击时检查计数器。

编辑:您已经获得了计数器,您应该仅在尝试时检查用户输入

public class Task1Activity extends AppCompatActivity {

int attempts = 0;
final int maxAttempts = 3;
Random randGen = new Random();
int ranNum;

@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);

randGen = new Random();
// Generate number once
ranNum = randGen.nextInt(5);


// 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) {
if(++attempts>maxAttempts){
//anything when more then maxAttempts needed
}else{
String randText = "";
//Random randGen = new Random(); not needed anymore
//int ranNum = randGen.nextInt(5); will generate new random number every click
int userNumber = Integer.parseInt(userGuess.getText().toString());


if (userNumber > 19) { //20???
guessText.setText("Please guess between 0 and 20");
} 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);
} else if (userNumber == ranNum) {
ranNum = randGen.nextInt(5);
guessText.setText("You did it!");
guessText.setBackgroundColor(Color.WHITE);
}

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

userGuess.setText("");
}
}
}
);

}

}

关于java - 将 Android 猜谜游戏限制为只能猜测 3 次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42252427/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com