如何制作 Android 猜谜游戏将猜测次数限制为 3 次,之后将显示一条消息
How to make Android Guessing Game to restrict the number of guesses to 3 attempts after which a message will be displayed
我需要制作 android 猜谜游戏 到目前为止,我有 3 次尝试的限制,之后将显示游戏结束之类的消息,但是我不知道该怎么做,因为我我对编程还是个新手,所以非常感谢您的帮助
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
}
}
}
我需要制作 android 猜谜游戏 到目前为止,我有 3 次尝试的限制,之后将显示游戏结束之类的消息,但是我不知道该怎么做,因为我我对编程还是个新手,所以非常感谢您的帮助
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
}
}
}