列表框未发送正确的输出

时间:2018-10-01 00:33:16

标签: c#

我正在编写一个程序来计算人口随时间的增长,但是我的部分代码存在问题。

这是我的代码的主要部分:

public class PAService extends Service {

static boolean is_ready_to_speak = false;
PendingIntent pendingIntent;

public PAService() {

}

public class ScreenReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        if(is_ready_to_speak)
        {
            PASpeak paspeak = new PASpeak();
            paspeak.sayit(getApplicationContext(),"You're using your phone for the first time this morning");
           is_ready_to_speak = false;
        }
    }
}


public static class AlarmReceiver extends BroadcastReceiver {

    public AlarmReceiver()
    {
        Log.d("AlarmReceiver func called","alarm receiver func called");
    }
    @Override
    public void onReceive(Context context, Intent intent) {
        Log.d("RECEIVED BROADCAST", "Sometype of ALARM Broadcast received");
        PASpeak paspeak = new PASpeak();
        paspeak.sayit(context.getApplicationContext(),"ALARM ALARM ALARM");
        is_ready_to_speak = true;
    }

}



@Override
public int onStartCommand(Intent intent, int flags, int startId) {

    IntentFilter filter = new IntentFilter(Intent.ACTION_USER_PRESENT);
    BroadcastReceiver mReceiver = new ScreenReceiver();
    registerReceiver(mReceiver, filter);

    PASpeak paspeak = new PASpeak();
    paspeak.sayit(getApplicationContext(),"process has started");

    Intent alarmIntent = new Intent(PAService.this, AlarmReceiver.class);
    pendingIntent = PendingIntent.getBroadcast(PAService.this, 1, alarmIntent, 0);

    // Set the alarm to start at 5:00 AM
    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(System.currentTimeMillis());
    calendar.set(Calendar.HOUR_OF_DAY, 5);
    calendar.set(Calendar.MINUTE, 0);
    calendar.set(Calendar.SECOND, 0);

    AlarmManager manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);

    manager.setRepeating(AlarmManager.RTC_WAKEUP,
            calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, // for repeating
            // in every 24
            // hours
            pendingIntent);

    return Service.START_STICKY;
}

@Override
public IBinder onBind(Intent intent) {
    // TODO: Return the communication channel to the service.
    throw new UnsupportedOperationException("Not yet implemented");
}

Here is my output example

似乎每次都将其设置为0时,并未获得正确的gainPercent。我不太确定自己做错了什么。据我所知,我的数学和代码是正确的。我已经尝试了do-while循环和for循环。

3 个答案:

答案 0 :(得分:0)

这是因为您要进行整数除法,该整数除法将返回一个整数,这意味着在任何情况下您都将始终失去浮点数。

示例:

20/100 = 0.2; // Which will result in 0 (integer remember?)
20/10 = 2; // Will result in 2.0 but 2
25/10 = 2; // Again not 2.5 but 2.

您真正想要的是将数字之一除以浮点数。

increasePercent = (averageIncrease / 100.0); // I've added .0 so it will be clear it's a floating point variable.

答案 1 :(得分:0)

根据您的代码以及Fabio注释中的建议,我还建议您将averageIncrease设置为小数。除此之外,将您的方程式更改为以下内容:

increasePercent = (averageIncrease / 100m)

其中m告诉C#100是一个十进制值。

另一方面,如果要在字符串中显示小数,请尝试使用string.format,如下所示:

string.Format("{0:0.00##}", [DecimalValue])

0是必需的小数位,而#是可选的:)

以您的情况为例,尝试这样做:

string listItemValue = string.Format("{0}\t {1}\t{2:0.00##}", count, approximatePopulation, increasePercent);

然后像这样将其添加到您的列表框中

outputListBox.Items.Add(listItemValue);

答案 2 :(得分:0)

increasePercent的类型应为decimal
并且由于其他变量的值基于increasePercent,因此它们也应声明为小数。

private void calculateButton_Click(object sender, EventArgs e)
{
    var parsing = new[]
    {
        decimal.TryParse(startingTextBox.Text, out decimal approximatePopulation),
        decimal.TryParse(increaseTextBox.Text, out decimal averageIncrease),
        int.TryParse(daysTextBox.Text, out int numberOfDays),
    };

    if (parsing.All())
    {
        decimal increasePercent = averageIncrease / 100;
        int count = 1;
        do
        {
            outputListBox.Items.Add(count + "\t " + approximatePopulation + "\t" + increasePercent);
            approximatePopulation = (approximatePopulation + (approximatePopulation * increasePercent));
            count++;
         }
         while (count <= numberOfDays);
    }
}