平衡金额随机增加并减少

时间:2015-11-09 08:04:16

标签: java android

我正在学习制作简单的骰子应用程序,但我刚遇到问题 问题是当我按下Lowbtn和Highbtn它只是增加和减少
随机而不管If语句。
如果我的想法是正确的当按下Highbtn并且数字结果高于乘数时,
那么它应该只将其余额增加0.1。我使用if语句而不是switch

以下是我的代码:

 import android.os.Bundle;
    import android.support.v7.app.AppCompatActivity;
    import android.support.v7.widget.Toolbar;
    import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.TextView;
    import android.content.SharedPreferences;
    import android.preference.PreferenceManager;
    import java.text.DecimalFormat;
    import android.widget.Toast;

    public class FieldActivity extends AppCompatActivity {
    private static DecimalFormat df1 = new DecimalFormat(".##");
    private static DecimalFormat df2 = new DecimalFormat(".########");
    Button Highbtn, Lowbtn;
    EditText BetAmount, Multiplier;
    TextView Balance;
    SharedPreferences mySharedPreferences;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        mySharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_field);
        Highbtn = (Button) findViewById(R.id.Highbtn);
        Lowbtn = (Button) findViewById(R.id.LowBtn);
        BetAmount = (EditText) findViewById(R.id.BetAmount);
        Multiplier = (EditText) findViewById(R.id.Multiplier);
        Balance = (TextView) findViewById(R.id.Balance);
        String userData = Balance.getText().toString();
        float balance1 = mySharedPreferences.getFloat("FLOAT_KEY1", Float.parseFloat(userData));
        Balance.setText(String.format("%f", balance1));
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        Highbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String userData = Balance.getText().toString();
                float balance1 = mySharedPreferences.getFloat("FLOAT_KEY1", Float.parseFloat(userData));
                float betamount1 = 0;
                float multiplier = Float.valueOf(Multiplier.getText().toString());
                try {
                    multiplier = Float.valueOf(Multiplier.getText().toString());
                } catch (NumberFormatException e) {
                    e.printStackTrace();
                }
                if (balance1 >= betamount1) {
                    SharedPreferences.Editor editor = mySharedPreferences.edit();
                    editor.putFloat("FLOAT_KEY1", balance1);
                    editor.apply();
                    double number = Math.random() * 100;
                    TextView Number = (TextView) findViewById(R.id.Number);
                    String Number1 = df1.format(number);
                    String Balance1 = df2.format(balance1);
                    Number.setText(Number1);
                    Balance.setText(Balance1);
                    if (multiplier <= number) {
                        balance1 += 0.10000000;
                        editor.putFloat("FLOAT_KEY1", balance1);
                        editor.apply();
                    } else if(multiplier >= number) {
                        balance1 -= 0.10000000;
                        editor.putFloat("FLOAT_KEY1", balance1);
                        editor.apply();
                    }
                }
                if (balance1 <= 0.00000000) {
                    balance1 += 1.00000000;
                    SharedPreferences.Editor editor = mySharedPreferences.edit();
                    editor.putFloat("FLOAT_KEY1", balance1);
                    editor.apply();
                    Balance.setText(String.format("%f", balance1));
                    String Balance1 = df2.format(balance1);
                    Balance.setText(Balance1);
                }
                if (balance1 < betamount1) {
                    SharedPreferences.Editor editor = mySharedPreferences.edit();
                    editor.putFloat("FLOAT_KEY1", balance1);
                    editor.apply();
                    Balance.setText(String.format("%f", balance1));
                    Toast.makeText(getApplicationContext(), "Insufficient Funds", Toast.LENGTH_LONG).show();
                }
            }
        });

        Lowbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String userData = Balance.getText().toString();
                float balance1 = mySharedPreferences.getFloat("FLOAT_KEY1", Float.parseFloat(userData));
                float betamount1 = 0;
                float multiplier = Float.valueOf(Multiplier.getText().toString());;
                try{
                    multiplier = Float.valueOf(Multiplier.getText().toString());
                } catch (NumberFormatException e) {
                    e.printStackTrace();
                }
                if (balance1 >= betamount1) {
                    SharedPreferences.Editor editor = mySharedPreferences.edit();
                    editor.putFloat("FLOAT_KEY1", balance1);
                    editor.apply();
                    double number = Math.random() * 100;
                    TextView Number = (TextView) findViewById(R.id.Number);
                    String Number1 = df1.format(number);
                    String Balance1  = df2.format(balance1);
                    Number.setText(Number1);
                    Balance.setText(Balance1);
                    if(multiplier >= number){
                        balance1 += 0.10000000;
                        editor.putFloat("FLOAT_KEY1", balance1);
                        editor.apply();
                    }
                    else if(multiplier <= number){
                        balance1 -= 0.10000000;
                        editor.putFloat("FLOAT_KEY1", balance1);
                        editor.apply();
                    }
                }
                if (balance1 <= 0.00000000) {
                    balance1 += 1.00000000;
                    SharedPreferences.Editor editor = mySharedPreferences.edit();
                    editor.putFloat("FLOAT_KEY1", balance1);
                    editor.apply();
                    Balance.setText(String.format("%f", balance1));
                    String Balance1  = df2.format(balance1);
                    Balance.setText(Balance1);
                }
                if (balance1 < betamount1) {
                    SharedPreferences.Editor editor = mySharedPreferences.edit();
                    editor.putFloat("FLOAT_KEY1", balance1);
                    editor.apply();
                    Balance.setText(String.format("%f", balance1));
                    Toast.makeText(getApplicationContext(), "Insufficient Fund", Toast.LENGTH_LONG).show();
                }
            }
        });


    }

}

这段代码有什么问题?

0 个答案:

没有答案