我试图跟踪一个人从两个日期可以输入的餐点,您可以从DateTimePickers输入。
每天之间的特定时间之间有三顿饭:
如果某人在某个特定时间之后到达,他们会错过一顿饭,例如,如果我在2018年1月1日凌晨3点到达,而在2018年1月2日下午2点离开,我将只收到5顿饭,因为我错过了最后一餐。
这是我想出的代码,但是不幸的是,如果两个日期之间的时间间隔超过一天,我将很难编写代码。
def nearestLowerPower(a,b):
n=1
while True:
n *= a
if n > b:
return n / a
答案 0 :(得分:1)
我发现您的代码很难维护(如果您以后想加第四餐,更改代码将是一场噩梦),因此我为您提供了一种不同的方法,并给出了答案您的问题。
首先,我将定义一个这样的类:
1
然后,我将简单地实例化该类并调用函数:
public class DayMeals
{
private int[] entryTimes = new int[] { 6, 12, 17 };
private int[] exitTimes = new int[] { 8, 14, 19 };
private int[] mealCounts = new int[3];
private bool countHalfMeals = false;
public DayMeals(bool countHalfMeals)
{
this.countHalfMeals = countHalfMeals;
}
public void AddFullDay()
{
mealCounts[0]++;
mealCounts[1]++;
mealCounts[2]++;
}
public void CountMealsForADay(DateTime timeArrived, DateTime timeExit)
{
for (int i = 0; i < mealCounts.Length; i++)
{
int mealEntryTime = entryTimes[i];
int mealExitTime = exitTimes[i];
if (timeArrived.Hour <= mealEntryTime && timeExit.Hour >= mealExitTime)
mealCounts[i]++;
else if (countHalfMeals && timeExit.Hour > mealEntryTime && timeExit.Hour <= mealExitTime)
mealCounts[i]++;
}
}
public void PrintMealsCount()
{
for (int i = 0; i < mealCounts.Length; i++)
{
System.Console.WriteLine($"Meal #{i + 1} count = {mealCounts[i]}");
}
}
}
我尝试了这段代码,它似乎按预期工作。请查看它,让我知道这是否是您想要实现的目标。
注意:我知道“ AddDays(1)”的用法是违反直觉的,因为我将第一天的同一小时保持为下一天。但是,如果您对某个人在星期一而不是星期二的11点进入这个事实不感兴趣,则用餐次数是相同的。基本上,我只是将入场时间追溯到最后一天。
答案 1 :(得分:1)
您可以在这里循环播放。我仅通过使用标准c#对象进一步简化了代码。诀窍是计算整天的工作时间,并计算时间跨度。
import android.os.Bundle;
import io.flutter.facade.Flutter;
import io.flutter.app.FlutterActivity;
public class MyActivity extends FlutterActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
// use software rendering (ideally only when you need to)
getIntent().putExtra("enable-software-rendering", true);
// start Flutter
Flutter.startInitialization(this);
super.onCreate(savedInstanceState);
}
}
答案 2 :(得分:0)
我还没有在VS中进行检查,但是类似的东西应该可以工作。我复制了您当天的代码,并假设它也是正确的:
public class MealCalculation
{
int countA, countB, countC = 0;
int total = 0;
public void Calculate()
{
var start = DateTime.Now;
var finish = DateTime.Now;
// Same Day
if (start.Date == finish.Date)
{
MealsCalculate(start.Hour, start.Hour);
}
// Next Day
else if (start.AddDays(1).Date == finish.Date)
{
MealsCalculate(start.Hour, 24);
MealsCalculate(0, finish.Hour);
}
// Great Than 1 Day
else
{
// First Day
MealsCalculate(start.Hour, 24);
// Middle Full Days
var days = NumberOfDays(start.Date, finish.Date);
countA += days;
countB += days;
countC += days;
// Finish Day
MealsCalculate(0, finish.Hour);
}
// Total
total = countA + countB + countC;
}
public int NumberOfDays(DateTime start, DateTime finish)
{
var days = 0;
while (start < finish)
{
start.AddDays(1);
days++;
}
return days - 1;
}
public void MealsCalculate(int totalHoursArrived, int totalHoursExit)
{
if (totalHoursArrived <= 8 && totalHoursExit >= 17) //if date is before or on 8AM and leaves on or after 5PM.
{
countA += 1;
countB += 1;
countC += 1;
}
else if (totalHoursArrived <= 8 && (totalHoursExit >= 12 && totalHoursExit < 17)) //if date is before or on 8AM and leaves before 5PM
{
countA += 1;
countB += 1;
}
else if (totalHoursArrived <= 8 && totalHoursExit < 12) //if date is before or on 8AM and leaves before 12PM
{
countA += 1;
}
else if ((totalHoursArrived <= 12 && totalHoursArrived > 8) && totalHoursExit >= 17) //if date is before or on 12PM and leaves on or after 5PM
{
countB += 1;
countC += 1;
}
else if ((totalHoursArrived <= 12 && totalHoursArrived > 8) && totalHoursExit < 17) //if date is before or on 12PM and leaves before 5PM
{
countB += 1;
}
else if (totalHoursArrived >= 17) //if date is after or on 5PM
{
countC += 1;
}
}
}