我只是一个初学者来到android& java的。我试图建立一个应用程序,告诉你是否在赌博上赢得或失去了鞅。
我的理念是,您可以设定您的金钱,目标和最低赌注。
例如,如果我将当前的金额设置为1000,并且我的目标是获得1100,并且最小赌注是100,那么应用程序将自动运行该功能10次并计算赢率。
现在我被困了,关于如何让应用程序计算赢率,我尝试了下面的代码,但是它没有用。
public int winPercentage (){
int numberWin = 0;
for (int i = 0; i <= 10; i++) {
boolean win = calRate(double currentMoney, double theTarget, double minBet);
if (win) {
numberWin = numberWin + 1;
}
}
return numberWin;
}
我的完整Java代码
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);
}
public void seeRate(View view) {
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());
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(double currentMoney, double theTarget, double minBet);
if (win){
numberWin = numberWin + 1;
}
}
return numberWin;
}
}
修改
现在我修改了下面的代码,android studio没有显示任何错误,但是当我尝试模拟它时,它无法打开,继续显示(不幸的是,应用已停止)
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;
}
}
答案 0 :(得分:0)
调用时必须将参数传递给方法'calRate()
。无需指定参数的类型。将行更改为
boolean win = calRate(double currentMoney,double theTarget,double minBet);
到
boolean win = calRate(currentMoney, theTarget, minBet);
这样可行。
修改强>
您必须在方法中定义变量currentMoney
,theTarget
和minBet
。为此,您有两种选择。
seeRate()
方法之外定义变量并使用它们。winPercentage()
方法。按如下方式定义winPercentage()
:
public int winPercentage (double currentMoney, double theTarget, double minBet) {
int numberWin = 0;
for (int i=0; i<=10; i++){
boolean win = calRate(currentMoney, theTarget, minBet);
if (win){
numberWin = numberWin + 1;
}
}
return numberWin;
}