Android——猜数字小游戏

                      实现功能:总共分三个模式
普通模式:数字0-100 困难模式:数字0-1000 地狱模式:数字0-10000 输入数字,会提示大还是小,猜对了,三种提示,还会有猜测数字显示

MainActivity.java代码如下:

public class MainActivity extends AppCompatActivity {
    private Button General,Diffculty,Hell;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        InitView();
        Listener();
    }
    private void InitView(){
        General = findViewById(R.id.general);
        Diffculty = findViewById(R.id.difficulty);
        Hell = findViewById(R.id.hell);
    }
    private void Listener(){
        OnClick onClick = new OnClick();
        General.setOnClickListener(onClick);
        Diffculty.setOnClickListener(onClick);
        Hell.setOnClickListener(onClick);
    }

    private class OnClick implements View.OnClickListener{

        @Override
        public void onClick(View v) {
            Intent intent = null;
            switch (v.getId()){
                case R.id.general:
                    intent = new Intent(MainActivity.this,General.class);
                    break;
                case R.id.difficulty:
                    intent = new Intent(MainActivity.this,Difficulty.class);
                    break;
                case R.id.hell:
                    intent = new Intent(MainActivity.this,Hell.class);
                    break;
            }
            startActivity(intent);
        }
    }
}

activity_main.xml:效果图如下:
Android——猜数字小游戏_第1张图片
activity_main.xml:代码如下:



    
    
    
    

建立三个活动分别为General,Difficulty,Hell,由于代码相似,只展示普通模式的代码,其余俩只更改随机函数中的数值范围即可
General.java代码如下:

public class General extends AppCompatActivity {
    private EditText Number;
    private Button Submit;
    private TextView GuessNumberResult, GuessNumberTimes;
    private int intGuessNumber = 0;
    private int NumberOfGuesses = 0;
    private int RandomNumber = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_general);
        Number = findViewById(R.id.Number);
        Submit = findViewById(R.id.submit);
        GuessNumberResult = findViewById(R.id.GuessNumberResult);
        GuessNumberTimes = findViewById(R.id.GuessNumberTimes);
        RandomNumber = (int) (Math.random()*(100-1+1));

        Submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(final View v) {
                String StrGuessNumber = Number.getText().toString();
                try{
                    intGuessNumber = Integer.parseInt(StrGuessNumber);
                }catch (NumberFormatException e)
                {
                    e.printStackTrace();
                }
                if (intGuessNumber < RandomNumber )
                {
                    NumberOfGuesses++;
                    GuessNumberResult.setVisibility(View.VISIBLE);
                    GuessNumberTimes.setVisibility(View.VISIBLE);
                    GuessNumberResult.setText("猜的数字偏小");
                    GuessNumberTimes.setText("猜测次数:"+NumberOfGuesses);
                }
                if (intGuessNumber > RandomNumber )
                {
                    NumberOfGuesses++;
                    GuessNumberResult.setVisibility(View.VISIBLE);
                    GuessNumberTimes.setVisibility(View.VISIBLE);
                    GuessNumberResult.setText("猜的数字偏大");
                    GuessNumberTimes.setText("猜测次数:"+NumberOfGuesses);
                }
                if (intGuessNumber == RandomNumber )
                {
                    NumberOfGuesses++;
                    GuessNumberResult.setVisibility(View.VISIBLE);
                    GuessNumberTimes.setVisibility(View.VISIBLE);
                    GuessNumberResult.setText("恭喜你,猜对了");
                    GuessNumberTimes.setText("猜测次数:"+NumberOfGuesses);
                }
            }
        });
    }
}

activity_general.xml代码如下:




    
    
    
        

        
    
    

效果图展示:

Android——猜数字小游戏_第2张图片 Android——猜数字小游戏_第3张图片 Android——猜数字小游戏_第4张图片

你可能感兴趣的:(Android,Studio,android,android,studio,安卓,java)