Contract.Ensures(Contract.Result< int>()==(x * x)); [英] Contract.Ensures(Contract.Result<int>() == (x*x));

查看:67
本文介绍了Contract.Ensures(Contract.Result< int>()==(x * x));的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好, 任何人都可以看到此代码中的错误吗?

Hello,  Can anybody see the error in this code?

 static int g(int x)
        {
            Contract.Requires(x >= 0);
            Contract.Ensures(Contract.Result<int>() == (x*x));
            
            int j = 0;
            for (int i = 0; i < x; i++)            
                j += x;            

            return j;
        }




谢谢!


Thanks!

推荐答案

是的,对于静态检查器要求太高以及确保条件不正确  :)

Yep, too high demands to static checker and incorrect condition in Ensures  :)

试试这个:

static int g(int x)
{
    Contract.Requires(x >= 0);
    Contract.Ensures(Contract.Result<int>() == Contract.OldValue(x) * Contract.OldValue(x));
 
    int j = 0;
    for (int i = 0; i < x; i++)
        j += x;
 
    Contract.Assume(j == x * x);
    return j;
}








这篇关于Contract.Ensures(Contract.Result&lt; int&gt;()==(x * x));的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆