DouglasQ DouglasQ - 2 months ago 6
Android Question

Stucked on Loops, not sure what is the wrong

I'm a just a beginner to android & java. I'm trying to build an app that will tell you if you win or lose base on the Martingale on gambling.

My concept is, you can set your money, target and the minimum bet.

for example is if I set my current money is 1000, and my target is to get 1100, and the minimum bet is 100, the app will auto run the function for example 10 times and calculate the win rate.

now I'm stucked, the android studio shows no error, however, whenever i try to emulate the app, it show "unfortunately, app has stopped" everything seem just fine to me, i don't know how to find the error... please guide me. million thanks

package com.example.android.gambling;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

}

EditText cMoney = (EditText) findViewById(R.id.money);
double currentMoney = Double.parseDouble(cMoney.getText().toString());

EditText target = (EditText) findViewById(R.id.target);
double theTarget = Double.parseDouble(target.getText().toString());

EditText bet = (EditText) findViewById(R.id.bet);
double minBet = Double.parseDouble(bet.getText().toString());

boolean findRate = calRate(currentMoney, theTarget, minBet);


public void seeRate(View view) {


TextView textview = (TextView)findViewById(R.id.textView);
textview.setText("You " + winPercentage());



}

public boolean calRate(double currentMoney, double theTarget, double minBet) {

while (currentMoney>minBet){
boolean win = winRate();
if (win){
currentMoney += minBet;
minBet = minBet;
}
else {
currentMoney -= minBet;
minBet *= 2;
}
if (currentMoney>=theTarget){
return true;
}

}

return false;
}

private boolean winRate() {
double d = Math.random();
if (d < 0.5)
return true;
else
return false;

}

public int winPercentage (){
int numberWin = 0;
for (int i=0; i<=10; i++){
boolean win = calRate(currentMoney, theTarget, minBet);
if (win){
numberWin = numberWin + 1;
}

}
return numberWin/10*100;
}
}


Edit(1)

package com.example.android.gambling;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {


EditText cMoney = (EditText) findViewById(R.id.money);
double currentMoney = Double.parseDouble(cMoney.getText().toString());

EditText target = (EditText) findViewById(R.id.target);
double theTarget = Double.parseDouble(target.getText().toString());

EditText bet = (EditText) findViewById(R.id.bet);
double minBet = Double.parseDouble(bet.getText().toString());

boolean findRate = calRate(currentMoney, theTarget, minBet);

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

}


public void seeRate(View view) {


TextView textview = (TextView)findViewById(R.id.textView);
textview.setText("You " + winPercentage());



}

public boolean calRate(double currentMoney, double theTarget, double minBet) {

while (currentMoney>minBet){
boolean win = winRate();
if (win){
currentMoney += minBet;
minBet = minBet;
}
else {
currentMoney -= minBet;
minBet *= 2;
}
if (currentMoney>=theTarget){
return true;
}

}

return false;
}

private boolean winRate() {
double d = Math.random();
if (d < 0.5)
return true;
else
return false;

}

public int winPercentage (){
int numberWin = 0;
for (int i=0; i<=10; i++){
boolean win = calRate(currentMoney, theTarget, minBet);
if (win){
numberWin = numberWin + 1;
}

}
return numberWin/10*100;
}
}

Answer

Do this:

private EditText cMoney;
private double currentMoney;
private EditText target;
private double theTarget;
private EditText bet;
private double minBet;
private boolean findRate;

@Override
protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  cMoney = (EditText) findViewById(R.id.money);
  currentMoney = Double.parseDouble(cMoney.getText().toString());
  target = (EditText) findViewById(R.id.target);
  theTarget = Double.parseDouble(target.getText().toString());
  bet = (EditText) findViewById(R.id.bet);
  minBet = Double.parseDouble(bet.getText().toString());
  findRate = calRate(currentMoney, theTarget, minBet);
}

You can't put the code like this outside of any method/constructor, unless it's static/initializer block and there's definitely no reason to use them here.

Also, I see that your parameter name in calRate is currentMoney same as your variable, try to avoid this, it may be confusing for someone else reading your code, for you and maybe even for compiler if you go too far.