我需要乘法表输出?有什么问题吗?

发布于 2024-10-21 11:14:32 字数 753 浏览 6 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

晚雾 2024-10-28 11:14:32

您可能想要一个 IEnumerable 作为视图模型?事实上很难说出你想要从你的问题中得到什么,但让我尝试猜测:

模型:

public MyViewModel
{
    public int Col { get; set; }
    public int Row { get; set; }
    public int Result { get; set; }
}

控制器:

public class HomeController : Controller 
{ 
    // GET: /Home/
    public ActionResult Index()
    {
        var model = new List<MyViewModel>();
        for (int row = 1; row <= 2; row++)
        for (int col = 1; col <= 2; col++)
        {
            model.Add(new MyViewModel
            {
                Row = row,
                Col = col,
                Result = row * col
            });
        }
        return View(model);
    }
}

视图(~/Views/Home/Index.aspx):

<%@ Page 
    Language="C#" 
    MasterPageFile="~/Views/Shared/Site.Master" 
    Inherits="System.Web.Mvc.ViewPage<IEnumerable<AppName.Models.MyViewModel>>" 
%>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    <% foreach (var item in Model) { %>
        <div>
            <span>Col: <%= item.Col %></span>
            <span>Row: <%= item.Row %></span>
            <span>Answer: <%= item.Result %></span>
        </div>
    <% } %>
</asp:Content>

或者如果你使用显示模板( 推荐):

<%@ Page 
    Language="C#" 
    MasterPageFile="~/Views/Shared/Site.Master" 
    Inherits="System.Web.Mvc.ViewPage<IEnumerable<AppName.Models.MyViewModel>>" 
%>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    <%= Html.DisplayForModel() %>
</asp:Content>

然后在~/Views/Home/DisplayTemplates/MyViewModel.ascx内:

<%@ Control 
    Language="C#" 
    Inherits="System.Web.Mvc.ViewUserControl<AppName.Models.MyViewModel>" 
%>
<div>
    <span>Col: <%= Html.DisplayFor(x => x.Col) %></span>
    <span>Row: <%= Html.DisplayFor(x => x.Row) %></span>
    <span>Answer: <%= Html.DisplayFor(x => x.Result) %></span>
</div>

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:

public MyViewModel
{
    public int Col { get; set; }
    public int Row { get; set; }
    public int Result { get; set; }
}

Controller:

public class HomeController : Controller 
{ 
    // GET: /Home/
    public ActionResult Index()
    {
        var model = new List<MyViewModel>();
        for (int row = 1; row <= 2; row++)
        for (int col = 1; col <= 2; col++)
        {
            model.Add(new MyViewModel
            {
                Row = row,
                Col = col,
                Result = row * col
            });
        }
        return View(model);
    }
}

View (~/Views/Home/Index.aspx):

<%@ Page 
    Language="C#" 
    MasterPageFile="~/Views/Shared/Site.Master" 
    Inherits="System.Web.Mvc.ViewPage<IEnumerable<AppName.Models.MyViewModel>>" 
%>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    <% foreach (var item in Model) { %>
        <div>
            <span>Col: <%= item.Col %></span>
            <span>Row: <%= item.Row %></span>
            <span>Answer: <%= item.Result %></span>
        </div>
    <% } %>
</asp:Content>

Or if you use Display Templates (recommended):

<%@ Page 
    Language="C#" 
    MasterPageFile="~/Views/Shared/Site.Master" 
    Inherits="System.Web.Mvc.ViewPage<IEnumerable<AppName.Models.MyViewModel>>" 
%>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    <%= Html.DisplayForModel() %>
</asp:Content>

and then inside ~/Views/Home/DisplayTemplates/MyViewModel.ascx:

<%@ Control 
    Language="C#" 
    Inherits="System.Web.Mvc.ViewUserControl<AppName.Models.MyViewModel>" 
%>
<div>
    <span>Col: <%= Html.DisplayFor(x => x.Col) %></span>
    <span>Row: <%= Html.DisplayFor(x => x.Row) %></span>
    <span>Answer: <%= Html.DisplayFor(x => x.Result) %></span>
</div>
没有心的人 2024-10-28 11:14:32

因为是在循环之后计算的,所以 answer 当前是 rowcol 最终值相乘的结果,忽略所有初始值和中间值。您需要构建一组要显示的结果,并将其添加到当前有 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 of row and col, 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 have Console.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 to for (int row = 1; row <= 2; row++).

偏爱自由 2024-10-28 11:14:32

在每个循环中,使用 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.

野の 2024-10-28 11:14:32
  1. 您的返回值是一个整数。我不知道你希望如何获得一张桌子。
  2. 9 是该函数的正确返回值。我认为您可能不明白您编写的代码是如何工作的。让我带您了解一下:

    int 行; // 变量“row”设置为默认整数值 0
    整数列=2; // 变量“cols”设置为默认整数值 0
    
    对于(行= 1;行<= 2;行++)
        // 设置变量 row = 1。
        // 当“row”的值小于或等于2时, 
        // 在内部语句的末尾,将行变量增加 1 
    {
       for (col = 1; col <= 2; col++)
        // 设置变量 col = 1。
        // 当“col”的值小于或等于2时, 
        // 在内部语句的末尾,将 col 变量加 1 
       {
           Console.Write("答案是" + row * col);         
    
       }
    

    }

那么在这个语句的末尾 row 和 col 应该分别等于什么?它们都等于 3。因为您循环直到它们等于 2(因此在最后一次迭代期间它们的值都等于 2)。然而,在最后一次迭代结束时,它们都再次增加 1,因此它们的值都是 3。

您的返回值是 row*col。使用替代。 3*3 = 9. 好了。

为了将表值显示在您的视图中,您应该遵循 Dmitri 的建议作为最佳实践。如果您不明白,让我尝试用更基本的术语进行解释。

为了让您的视图显示表格,您的视图需要接受与表格兼容的数据。没有办法将整数转换为表格。

您想要的数据如下所示:

      1  2
   1  1  2
   2  2  4

因此,您需要最简单形式的二维整数数组,其值如下:

   tableArray[0] = new int[]{0,1,2};
   tableArray[1] = new int[]{1,1,2};
   tableArray[2] = new int[]{2,2,4};

因此,您的视图应该接受的模型类型类似于 int[][]

  1. Your return value is an integer. I don't know how you expect to get a table.
  2. 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:

    int row;    //  The variable "row" is set to the default integer value, 0
    int col=2;  //  The variable "cols" is set to the default integer value, 0
    
    for (row = 1; row <= 2; row++)
        // set the variable row = 1.
        // while the value of "row" is less than or equal to 2, 
        // at the end of the inner statement, increment the row variable by 1 
    {
       for (col = 1; col <= 2; col++)
        // set the variable col = 1.
        // while the value of "col" is less than or equal to 2, 
        // at the end of the inner statement, increment the col variable by 1 
       {
           Console.Write(" the answer is " + row * col);         
    
       }
    

    }

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:

      1  2
   1  1  2
   2  2  4

So, you need in its most simple form a 2-dimensional integer array with values like:

   tableArray[0] = new int[]{0,1,2};
   tableArray[1] = new int[]{1,1,2};
   tableArray[2] = new int[]{2,2,4};

So, the type of model your view should accept is something like a int[][]

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文