我需要乘法表输出?有什么问题吗?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace MvcApplication5.Controllers
{
public class HomeController : Controller
{
//
// GET: /Home/
public ActionResult Index()
{
int row;
int col;
for (row = 1; row <= 2; row++)
{
for (col = 1; col <= 2; col++)
{
Console.Write(" the answer is " + row * col);
}
}
int answer = row * col;
return View(answer);
}
}
}
我希望我的答案出现在乘法口诀表中。 1*1=1, 1*2=2, 2*1=2, 2*2=4 这就是我想要的。但上面的编码给了我 9 作为答案。怎么会?我在这里做错了什么?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace MvcApplication5.Controllers
{
public class HomeController : Controller
{
//
// GET: /Home/
public ActionResult Index()
{
int row;
int col;
for (row = 1; row <= 2; row++)
{
for (col = 1; col <= 2; col++)
{
Console.Write(" the answer is " + row * col);
}
}
int answer = row * col;
return View(answer);
}
}
}
I want my answers to be in multiplication table. 1*1=1, 1*2=2, 2*1=2, 2*2=4 it was i want. but the above coding gives me 9 as answer. how come? what i am doing wrong here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可能想要一个
IEnumerable
作为视图模型?事实上很难说出你想要从你的问题中得到什么,但让我尝试猜测:模型:
控制器:
视图(
~/Views/Home/Index.aspx
):或者如果你使用显示模板( 推荐):
然后在
~/Views/Home/DisplayTemplates/MyViewModel.ascx
内:You probably want an
IEnumerable<SomeViewModel>
as view model? In fact hard to say what you want from your question, but let me try to guess:Model:
Controller:
View (
~/Views/Home/Index.aspx
):Or if you use Display Templates (recommended):
and then inside
~/Views/Home/DisplayTemplates/MyViewModel.ascx
:因为是在循环之后计算的,所以
answer
当前是row
和col
最终值相乘的结果,忽略所有初始值和中间值。您需要构建一组要显示的结果,并将其添加到当前有Console.Write(" the answer is " + row * col);
的循环中。如果您将迭代变量范围限制在循环范围内,您的问题和解决方案将更加明显,即删除独立的
int row;
并将循环语句修改为for (int row = 1 ;行<=2行++)。
Because it is calculated after the loop,
answer
is currently the result of the multiplication of the final values ofrow
andcol
, and ignores all the initial and intermediary values. You need to build a set of results to display, adding to that set within your loop where you currently haveConsole.Write(" the answer is " + row * col);
.If you kept your iterating variables scoped to the loop, your problem and the solution would be more apparent, i.e. removing the standalone
int row;
and modifying the loop statement tofor (int row = 1; row <= 2; row++)
.在每个循环中,使用 row++ 递增 row。然后它检查是否满足条件(行 <= 2)。如果不满足条件,则会中断循环。但仍然有所增加。因此,当 row 达到 2 时,它会执行循环,增加到 3,然后继续。与 col 循环相同。所以在循环结束时,你有 3 * 3,即 9。
Within each loop, row is incremented using row++. It then checks to see if it meets the criteria (row <= 2). If it does not meet the criteria, it breaks the loop. But it has still been incremented. So when row hits 2, it runs through the loop, gets incremented to 3, and continues. Same with the col loop. So at the end of the loop, you have 3 * 3 which is 9.
9 是该函数的正确返回值。我认为您可能不明白您编写的代码是如何工作的。让我带您了解一下:
}
那么在这个语句的末尾 row 和 col 应该分别等于什么?它们都等于 3。因为您循环直到它们等于 2(因此在最后一次迭代期间它们的值都等于 2)。然而,在最后一次迭代结束时,它们都再次增加 1,因此它们的值都是 3。
您的返回值是 row*col。使用替代。 3*3 = 9. 好了。
为了将表值显示在您的视图中,您应该遵循 Dmitri 的建议作为最佳实践。如果您不明白,让我尝试用更基本的术语进行解释。
为了让您的视图显示表格,您的视图需要接受与表格兼容的数据。没有办法将整数转换为表格。
您想要的数据如下所示:
因此,您需要最简单形式的二维整数数组,其值如下:
因此,您的视图应该接受的模型类型类似于
int[][]
9 is the correct return value for the function. I think you may not be understanding how the code you wrote works. Let me walk you through it:
}
So what should row and col each be equal to at the end of this statement? They are both equal to 3. Because you looped until they were equal to 2, (so during the last iteration their values were both equal to 2). However, at the end of the last iterations they were both incremented by 1, one more time, so their values are both 3.
Your return value is what is row*col. Use substitution. 3*3 = 9. There ya go.
In order to get the table values to your view, you should follow Dmitri's advice as a best practice. If you don't understand that let me try to explain it in more basic terms.
In order for your view to show a table, your view needs to accept data that is compatible with a table. There is no way to convert an integer into a table.
You want data that looks like:
So, you need in its most simple form a 2-dimensional integer array with values like:
So, the type of model your view should accept is something like a
int[][]