我正在做一个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("");
}
}
);
}
}
答案 0 :(得分:1)
你在onClick函数中第二次实例化了runNum。摆脱这个。第二:你应该在你的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("");
}
}
}
);
}
}