用Java计算机器人移动的距离

用Java计算机器人移动的距离,java,function,math,Java,Function,Math,我正在为学校做一个项目,要求我移动一个机器人。机器人每秒移动的距离(变量t)由下面的函数计算 第一个功能很简单。另一边的第二个和第三个是我被卡住的地方。我如何写F(t-1)?以下是我到目前为止的情况 if (t == 0) { distance = 2; } else if (t > 0 && <=6 || t > 12) { // No clue on how to write the 2nd distance equation. } els

我正在为学校做一个项目,要求我移动一个机器人。机器人每秒移动的距离(变量t)由下面的函数计算

第一个功能很简单。另一边的第二个和第三个是我被卡住的地方。我如何写F(t-1)?以下是我到目前为止的情况

if (t == 0) {
    distance = 2;
} else if (t > 0 && <=6 || t > 12) {
    // No clue on how to write the 2nd distance equation.
} else if (t >= 7 && <=12) {
    // No clue on how to write the 3rd distance equation.
}
if(t==0){
距离=2;
}否则如果(t>0&&12){
//没有写第二个距离方程的线索。

}否则,如果(t>=7&&这是一个涉及使用的问题。大体上,请密切注意表示法Ft-1,因为它指的是对
t-1
处特定功能的评估

我不会写出所有的代码,但我会给你一些基础知识:

  • 当t=0时,返回2。这是您的基本情况
  • 当t介于0和6之间(含0和6)或大于12时,返回对t-1处函数的求值并添加2
  • 当t介于7和12之间(包括7和12)时,返回t-1处的函数求值并添加log2(t)
这里有一些东西可以让你至少从正确的方向开始

public double evaluateDistance(int t) {
    if(t == 0) {
        return 2;
    } else if(t > 0 && t <= 6) || (t > 12) {
        // Think about this - it would involve another call to evaluateDistance, but what is t again?
    } else if(t >= 7 && t <= 12) {
        // Another evaluation involving the function.
        // For free, the change of base operation you'll need to get base-2 evaluation for the log:
        return ??? + Math.log(t)/Math.log(2);
    }
}
公共双重评估距离(int t){
如果(t==0){
返回2;
}否则如果(t>0和&t 12){
//想一想,这将涉及到另一个调用EvaluatedDistance,但t又是什么呢?
}否则,如果(t>=7&&t真的没有必要解决这个问题

注意,在每个非零时间情况下,
F(t)=F(t-1)+某物

因此,您可以简单地执行以下操作:

double f = 2;  /* Initial value at t=0 */
for (int t = 1; t <= maxT; ++t) {  // maxT is the maximum value of t.
  if (t <= 6 || t > 12) {
    f += /* something for case 2 */;
  } else {
    f += /* something for case 3 */;
  }
}
System.out.println(f);

你也可以通过预先计算这些值来消除这个循环。

我想我已经解决了。对不起,如果我不清楚我需要什么,我只需要解决如何在函数中写入方程。不过我想我已经解决了

    public double move()
{
    int t = 0;
    if(t == 0) // After the first second, robot moves 2
    {
        distance = 2;
    }
    else if(t > 0 && t <= 6 || t > 12) // From seconds 0 to 6 and after 12, robot moves distance equation
    {
        distance = (2*t)+2;
    }
    else if(t >= 7 && t <= 12) // From seconds 7 to 12, robot moves distances equation
    {
        distance = (2*t)+(Math.log(t)/Math.log(2));
    }
    position = position + distance;
    return position;
}
公共双移动()
{
int t=0;
如果(t==0)//在第一秒之后,机器人移动2
{
距离=2;
}
否则,如果(t>0&&t12)//从0秒到6秒以及12秒之后,机器人移动距离方程
{
距离=(2*t)+2;
}

否则如果(t>=7&&t您如何使用此函数?如果您解释上下文或给出您正在尝试的函数定义,可能会更好。您必须使用递归来解决此问题!!您知道如何使用递归吗?@RenukaDeshmukh不,您不知道。@AndyTurner有其他方法吗?@AndyTurner是的,我活该,不是吗?我如果函数被定义为F(t),而不是Ft,那么Ft-1是错误的,因为它应该是F(t-1)。无论如何,因为只有一个递归存在,而且对于
t-1
,循环比递归更好,尽管首先解释递归解决方案是好的。(在评论中似乎不起作用,但你知道我的意思)我并不反对@Andreas的观点。循环可能更有效、更直接,但从两个方面看都不会有什么坏处。这就是我的观点。从两个方面看。你只展示了递归方式,这是给定赋值的最直接的方式,我已经对此表示赞赏,但你没有展示“更好”的方式循环方式,对于
t
的大值不会导致
StackOverflowException
。@Andreas您是指递归还是其他方式?对于t=1..6:
F(t)=t*2
。对于t>12:
F(t)=F(12)+(t-12)*2
。对于t=7..12,您可以执行循环或6个预先计算的常量数组来计算
12+log(7)+log(8)+log(9)+log(10)+log(11)+log(12)
或取决于
t
的任何子集。顺便说一句:名为
F
的变量,大写?真的?;-)@Andreas只是简单地匹配有问题的符号。您也可以找到缺少的分号,或者缺少RHS表达式:)@Andreas您可以做得比您的三种情况更简单:简单地做
F'(t)=t*2
,然后对7..12个值使用
log(t)-2
    public double move()
{
    int t = 0;
    if(t == 0) // After the first second, robot moves 2
    {
        distance = 2;
    }
    else if(t > 0 && t <= 6 || t > 12) // From seconds 0 to 6 and after 12, robot moves distance equation
    {
        distance = (2*t)+2;
    }
    else if(t >= 7 && t <= 12) // From seconds 7 to 12, robot moves distances equation
    {
        distance = (2*t)+(Math.log(t)/Math.log(2));
    }
    position = position + distance;
    return position;
}