使用float变量在android应用中显示板球

时间:2018-07-10 18:42:01

标签: java android

我正在创建一个板球得分跟踪器android应用程序,作为在线课程的项目。在该应用程序中,我想显示跑步,检票口和收口。共有六个按钮供用户输入: 1、2、3、4、6和检票口

当用户按下一个按钮时,跑步/检票口和翻车将相应地更新。但是要显示这些内容,我面临一个问题。

如果您不知道,在板球比赛中,上方有6个球。

在我的应用中,游戏限制为5个打折(30个球)。因此,只要按下按钮,over变量就会增加 0.1 。我希望overs变量在达到 1.6 时自动更改为 2.0 。达到 2.6 时,必须更改为 3.0 ,依此类推,直到达到 5.0

不使用多个if块,我不知道如何在我的应用程序中实现此功能。 这是我的尝试:

@model List<Products>

@foreach (var product in Model)
{
    //Do something with product
}

我必须在每个按钮方法中使用此代码段,这使整个过程看起来非常难看。

我很好奇,如果不使用Java中的多个嵌套if循环,是否有办法做到这一点。

7 个答案:

答案 0 :(得分:2)

如果您的问题是在每个按钮中都使用此代码,尽管我对板球一无所知,请创建以下代码:

private double getOvers(double overs) {
    if(overs<5.0){

        if(overs == 1.6){
            overs = 2.1;
        }

        if(overs == 2.6){
            overs = 3.1;
        }

        if(overs == 3.6){
            overs = 4.1;
        }

        else{
            overs += 0.1;
        }
    }
    return overs;
}

并在每个侦听器中使用它。

答案 1 :(得分:2)

这可能不是很优雅,但是您可以在满足条件时增加叠加次数。

if (overs % 1 == .6) // decimal ends as .6
   overs += .5;

由于它是浮点数,因此您可能需要使用epsilon值:

if (Math.abs((overs % 1)-.6) < .0000001)
    overs += .5;
else 
    overs += .1;

答案 2 :(得分:1)

打这样的电话并传球数应该可以。

   public float calcOvers(float balls)
    {
            float overs;
            int overPrefix;

            int newBalls = balls * 10;
            overPrefix = (int)(newBalls / 6);
            balls = (newBalls % 6) / 10; 
            overs = overPrefix + balls;
            if(overs < 5)
            {
                return overs;
            }
            else
            {
                System.out.println("Exeeded 5 overs")
                return 0;
            }
    }

答案 3 :(得分:0)

在所有编程语言中,比较float与Identity都是有风险的。
(将讨论与公认的不是Java进行比较:Is floating point math broken?)。

如果要以十分之一为单位,请使用整数并显示除以10的计数器。

int overs=0;

if(overs<50){

    if(overs == 16){
    overs = 21;
    }

    if(overs == 26){
    overs = 31;
    }

    if(overs == 36){
    overs = 41;
    }

    else{
    overs += 1;
    }
}

/* for anything which actually makes output use (1.0*overs)/10.0 */

请注意,我也将一些==(比较表达式)更改为显然打算使用的=(赋值语句)。

答案 4 :(得分:0)

  

我希望overs变量在达到1.6时自动更改为2.0。达到2.6时,必须更改为3.0,依此类推,直到达到5.0。

假设您使用Java进行编码,为什么不使用ceil方法?

 Math.ceil(overs);

例如:

double x = 7.5;
System.out.println("Math.ceil(x) : " + Math.ceil(x));

结果:8.0

答案 5 :(得分:0)

如果您特别想使用float / Double来实现此代码,而不是使用嵌套的if语句,您可以简单地获取十进制值并检查其是否大于或等于6并增加基值,例如     //获取浮点小数部分,实际上是上方的球数

    double over = 4.6;
    //get the decimal value
    double ball = over % 1;
    // current value in ball is not a round off decimal value e.g 3.5 % 1 = .4999999
    //converting the ball value to a round off value as needed
    ball = Math.round(ball * 100.0) / 10.0;
    // ball = round(.60002*100)/10 
     if (ball >= 6){
       over = Math.floor(over) + 1;
    }
    System.out.println(over);
    //output => 5

答案 6 :(得分:0)

只得到两个变量

int overs=0;
int balls=0;

 private void OversCounter()
    {


         if (balls==6)
    {
        balls=0;
        overs+=1;
    }
相关问题