我需要制作到目前为止有3次尝试限制的android猜测游戏,之后将显示诸如游戏结束之类的消息,但是我不知道如何进行操作,因为我还是编程新手,所以我会感谢你的帮助
到目前为止的Android代码:
import java.util.Random;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class Task1Activity extends Activity {
private TextView tvPlayer;
private EditText eName;
private Button butSubmit;
private TextView nameOutput;
private TextView tvNumber;
private EditText eNumber;
private TextView numOutput;
private Button butGuess;
private Button butStart;
private String name_output;
private String number_output;
private TextView tvTries;
private TextView tvTries2;
public static int N;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.task1_layout);
//Reference the input and output views on the interface
tvPlayer = (TextView) findViewById(R.id.tvPlayer);
eName = (EditText) findViewById(R.id.eName);
butSubmit = (Button) findViewById(R.id.butSubmit);
tvNumber = (TextView) findViewById(R.id.tvNumber);
eNumber = (EditText) findViewById(R.id.eNumber);
butGuess = (Button) findViewById(R.id.butGuess);
butStart = (Button) findViewById(R.id.butStart);
nameOutput = (TextView) findViewById(R.id.nameOutput);
numOutput = (TextView) findViewById(R.id.numOutput);
tvTries = (TextView) findViewById(R.id.tvTries);
tvTries2 = (TextView) findViewById(R.id.tvTries2);
}
public void addClick1(View Button){
name_output = eName.getText().toString(); //get the existing comments
nameOutput.setText(name_output);//Display the new comments
} public void addClick2(View Button){
Random r = new Random();
int Low = 0;
int High = 20;
N = r.nextInt(High-Low) + Low;
}
public void addClick3(View Button){
number_output = eNumber.getText().toString(); //get the existing comments
try {
int compareNum = Integer.parseInt(number_output);
if(compareNum == N){
numOutput.setText(number_output + " Good Guess!");//Display the new comments
numOutput.setBackgroundColor(Color.GREEN);
}else if(compareNum > N){
numOutput.setText(number_output + " is too HIGH!");//Display the new comments
numOutput.setBackgroundColor(Color.RED);
}else if(compareNum < N){
numOutput.setText(number_output + " is too LOW!");//Display the new comments
numOutput.setBackgroundColor(Color.RED);
}
}
catch (NumberFormatException e) {
}
}
}
您必须计算点击次数,例如:
public void addClick2(View Button){
Random r = new Random();
int Low = 0;
int High = 20;
int count = 0;
N = r.nextInt(High-Low) + Low;
}
public void addClick3(View Button){
count++;
number_output = eNumber.getText().toString(); //get the existing comments
try {
int compareNum = Integer.parseInt(number_output);
if(compareNum == N){
numOutput.setText(number_output + " Good Guess!");//Display the new comments
numOutput.setBackgroundColor(Color.GREEN);
}else if(compareNum > N){
numOutput.setText(number_output + " is too HIGH!");//Display the new comments
numOutput.setBackgroundColor(Color.RED);
}else if(compareNum < N){
numOutput.setText(number_output + " is too LOW!");//Display the new comments
numOutput.setBackgroundColor(Color.RED);
}
}
catch (NumberFormatException e) {
}
if(count == 3){
//set your game over message
}
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句