将值传递给类将始终返回0

时间:2018-11-09 06:12:21

标签: c#

这可能是一个简单/愚蠢的问题,但我试图弄清楚为什么我的代码总是返回0。我认为传递值的语法不正确,但是我无法弄清楚正确的做法

@page
@model Security.Dto.Models.ApplicationRoleModel

<form asp-controller="security" asp-action="CreateRole" method="post">

<h4>Create new Role</h4>
<hr />
<div asp-validation-summary="All" class="text-danger"></div>
<div class="form-group" id="item-list">
    <label>Role Name</label>
    <input asp-for="RoleList[0].Name" class="form-control items" />
</div>
<a href="#" id="add">Add another</a>//click to add
<br />
<button type="submit" class="btn btn-default">Create</button>

</form>

@section Scripts {
<script>
$(function () {

   $("#add").click(function (e) {
       e.preventDefault();
       var i = ($(".items").length);
       var n = '<input class="form-control items" name="RoleList[' + i + '].Name" />' 
       $("#item-list").append(n);
   });

});
</script>
}

class ICESMARK { static int ICECount = 0; public double average = 0; public double[] ICES = new double[8]; public ICESMARK(double Mark) { Mark = ICES[ICECount]; if (ICECount == (ICES.Length - 1)) { for (int x = 0; x < ICES.Length; x++) { average += ICES[x]; } average /= ICES.Length; } ICECount++; } } class Program { static void Main(string[] args) { ICESMARK[] ICE = new ICESMARK[8]; //LABSMARK[] LAB = new LABSMARK[6]; double userInput; for (int counter = 0; counter < ICE.Length ; counter++) { Console.Write("Please enter your mark for ICE{0}: ", counter + 1 ); bool ifInt = double.TryParse(Console.ReadLine(), out userInput); ICE[counter] = new ICESMARK(userInput); } Console.WriteLine(ICE[1].average); Console.ReadLine(); } } 显示0

如果有人有更有效的方法,请随时告诉我。除了平均值,必须计算,不能使用内置方法。

2 个答案:

答案 0 :(得分:2)

最简单的代码即可完成工作:

void Main()
{
    double[] ICE = new double[8];

    double userInput = 0.0;

    for (int counter = 0; counter < ICE.Length; counter++)
    {
        Console.WriteLine($"Please enter your mark for ICE {counter}: ");

        bool isNumerical = false;

        while(!isNumerical)
            isNumerical = double.TryParse(Console.ReadLine(), out userInput);

        ICE[counter] = userInput;
    }

    Console.WriteLine("Average : " + ICE.Average());
    Console.ReadLine();
}

工作方式:

  1. 消除了所有的装饰,为此您不需要额外的课程
  2. 强制代码输入有效的双精度值以填充所有插槽
  3. 最后使用Linq Average来计算Average

答案 1 :(得分:0)

我想澄清的是,有解决此问题的简便方法,但是项目的整个重点是使用一个类,它具体说我不能使用内置的方法,例如“ array.average”。

很抱歉,我的代码太乱了,老实说,我到处都是,很困惑。话虽如此,我终于到达了这个解决方案。感谢所有尝试提供帮助的人!非常感谢,这里的一些技巧对解决和清理我的代码很有帮助。

    class ICESMARK
{
    public static int ICECount = 0;
    public static double average = 0;

    public ICESMARK(double Mark)
    {

        average += Mark;
        if (ICECount < 8) { ICECount++; }
        if (ICECount == 8) { average /= ICECount;}            
    }

}
class Program
{
    static void Main(string[] args)
    {
        ICESMARK[] ICE = new ICESMARK[8];

        double userInput;

        for (int counter = 0; counter < ICE.Length ; counter++)
        {
            Console.Write("Please enter your mark for ICE{0}: ", counter + 1 );
            bool ifInt = double.TryParse(Console.ReadLine(), out userInput);
            ICE[counter] = new ICESMARK(userInput);
            Console.WriteLine(ICESMARK.ICECount);
        }

        Console.WriteLine(ICESMARK.average);
        Console.WriteLine(ICESMARK.ICECount);
        Console.ReadLine();
    }
}