如何制作Android Guessing Game以将猜测次数限制为3次尝试,之后将显示一条消息

不同的

我需要制作到目前为止有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) {
        }
    }
}
乔治·安东尼奥利(Giorgio Antonioli)

您必须对点击次数进行计数,例如:

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] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何获取所有最新消息,但每条用户消息限制为一条

如何制作唯一的Android ID数组?

Android中的布局-如何制作3列?

TypeError:无法排序的类型:int()> Guessing_game()

如何将android分区的图像制作到您的PC

如何将网页制作成Android应用

如何显示一条消息,告诉用户他们的猜测与随机生成的数字有多远?

如何限制Akka Stream每秒只能执行和发送一条消息一次?

如何在Android中制作离散的搜索/进度条?

如何在Android中制作这个进度条?

如何显示某个成员在Firestore(Android)中制作的文档?

如何制作Android上的Unity游戏以显示系统导航栏?

启动san ssh连接时,如何在显示的“最后登录”之后添加一条消息?

如何制作一个android凸起按钮?

将字符变量转换为整数时,会显示一条消息:强制引入NA。如何避免这个错误?

如何使用 matplotlib 将线性回归绘制为一条线段

如何一次在asp.net的验证摘要中仅显示一条消息

当用户名和密码为空白时,单击“登录”按钮将引发一条消息,即如何打印显示的消息

如何一次将一个Jquery效果限制为一项?

如何使用 android studio 制作一个简单的跟踪 android 应用程序

如何在 Android RecyclerView Android 列表中制作一个占位符项目

如何将选中的复选框一次限制为一个?

如何在android中制作一个只能点击一次的按钮

在将电子邮件作为一条消息发送之前,如何首先绑定内容?

如何在foreach循环中显示一条消息?

如何使用AngularJS每秒显示一条消息?

AngularApp:如何显示对话中的最后一条消息?

libGDX - 如何将多边形绘制为一条线(斜接连接类型)?

如何在Android上制作一个显示退格(⌫)字符的按钮?