我的代码目前是一个随机数生成器,允许用户输入他们的名字和数字进行猜测,并显示他们的名字,他们的猜测以及猜测是否正确。它目前所做的是使它显示每个猜测,而我只希望它显示最新的一个,我将如何进行此操作?我的代码如下:
public void addClick(View v){
String randtext ="";
Random rand = new Random();
int noofguess = 0;
int n = rand.nextInt(20)+1; //generate random number
comments = "";
comments = tvCommentOuput.getText().toString();
comments = comments+ "\n" + etCommentInput.getText();
comments = comments+ "\n" + etnameInput.getText();
int userguess = Integer.parseInt(etCommentInput.getText().toString());
if (userguess < 1 || userguess > 20){
comments = comments+ "\n" + "Please enter a number between 1-20!";
tvCommentOuput.setText(comments);
}
else if (userguess == n){
comments = comments+ "\n" + "You got it right!";
tvCommentOuput.setText(comments);
} else if (userguess > n) {
comments = comments+ "\n" + "Number too high!";
tvCommentOuput.setText(comments);
noofguess = noofguess + 1;
} else{
comments = comments+ "\n" + "Number too low!";
tvCommentOuput.setText(comments);
noofguess = noofguess + 1;
}
randtext = Integer.toString(n);
}