C# 使用多个类

C# 使用多个类,c#,class,methods,method-call,C#,Class,Methods,Method Call,我试图制作一个简单的程序,要求用户输入一个整数。一旦程序接收到输入,它将接收并存储该输入,然后从1计数到输入整数,并对总数进行求和。然后,它以一种有意义的方式向用户显示结果,并提示他们是否要处理另一个数字。这个程序的要点是使用循环和多个类。我知道我非常接近所需的最终产品,但无法理解为什么AccumageValue()方法不能正常工作。它似乎没有进入我所做的条件while语句。如果有人能给我一些关于我的问题的见解,那就太好了 这是我的密码: 累加器pp.cs using System; using

我试图制作一个简单的程序,要求用户输入一个整数。一旦程序接收到输入,它将接收并存储该输入,然后从1计数到输入整数,并对总数进行求和。然后,它以一种有意义的方式向用户显示结果,并提示他们是否要处理另一个数字。这个程序的要点是使用循环和多个类。我知道我非常接近所需的最终产品,但无法理解为什么AccumageValue()方法不能正常工作。它似乎没有进入我所做的条件while语句。如果有人能给我一些关于我的问题的见解,那就太好了

这是我的密码:

累加器pp.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Project
{
    class AccumulatorApp
    {


        static void Main(string[] args)
        {
            string loopControl = "Y";
            int sum;
            int enteredValue;
            DisplayTitle();

            while (loopControl == "Y" || loopControl == "YES")
            {
                enteredValue = InputInteger(0);
                Accumulator number = new Accumulator(enteredValue);
                sum = number.AccumulateValues();
                DisplayOutput(sum, enteredValue);
                Console.Write("\tWould you like to process another number? \n\t\t<Y or N>: ");
                loopControl = Console.ReadLine().ToUpper();
            }

        }


        public static void DisplayTitle()
        {
            Console.BackgroundColor = ConsoleColor.White;
            Console.ForegroundColor = ConsoleColor.Black;
            Console.Clear();
            Console.WriteLine();
            Console.WriteLine("\tProgramming Assignment 05 - Accumulator - Robert");
            DrawLine();

        }


        public static int InputInteger(int enteredValue)    
        {

            Console.Write("\tPlease enter a positive integer: ");
            enteredValue = Convert.ToInt32(Console.ReadLine());
            if (enteredValue > 0)
            {
                return enteredValue;
            }
            else 
            {
                Console.WriteLine("\tInvalid input. Please enter a POSITIVE integer: ");
                enteredValue = Convert.ToInt32(Console.ReadLine());
            }
            return enteredValue;


            /*
            Console.Write("Please enter a positive integer: ");
            int enteredValue = Convert.ToInt32(Console.ReadLine());
            return enteredValue;
             * */
        }


        public static void DisplayOutput(int sum, int inputValue)
        {
            Console.WriteLine("\tThe inputed integer is: {0}", inputValue);
            Console.WriteLine("\tThe sum of 1 through {0} = {1}", inputValue, sum); 
            DrawLine();
        }


        public static void DrawLine()
        {
            Console.WriteLine("\t______________________________________________________");
            Console.WriteLine();
        }

    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
命名空间项目
{
类累加器
{
静态void Main(字符串[]参数)
{
字符串loopControl=“Y”;
整数和;
int输入值;
DisplayTitle();
while(loopControl==“Y”| | loopControl==“YES”)
{
enteredValue=InputIntegrater(0);
累加器编号=新累加器(输入值);
sum=number.acgregateValues();
显示输出(总和,输入值);
控制台。写(“\t您想处理另一个号码吗?\n\t\t:”);
loopControl=Console.ReadLine().ToUpper();
}
}
公共静态void DisplayTitle()
{
Console.BackgroundColor=ConsoleColor.White;
Console.ForegroundColor=ConsoleColor.Black;
Console.Clear();
Console.WriteLine();
Console.WriteLine(“\t编程赋值05-累加器-Robert”);
抽绳();
}
公共静态int-inputineger(int-enteredValue)
{
Console.Write(“\t请输入一个正整数:”);
enteredValue=Convert.ToInt32(Console.ReadLine());
如果(输入值>0)
{
返回输入值;
}
其他的
{
Console.WriteLine(“\t有效输入。请输入一个正整数:”);
enteredValue=Convert.ToInt32(Console.ReadLine());
}
返回输入值;
/*
控制台。写入(“请输入一个正整数:”);
int enteredValue=Convert.ToInt32(Console.ReadLine());
返回输入值;
* */
}
公共静态void DisplayOutput(int sum,int inputValue)
{
WriteLine(“\t输入的整数为:{0}”,inputValue);
WriteLine(“\t 1到{0}的和={1}”,inputValue,sum);
抽绳();
}
公共静态无效抽绳()
{
控制台.WriteLine(“\t\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu;
Console.WriteLine();
}
}
}
累加器

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Project
{
    class Accumulator
    {
        int integerEntered; 

        public Accumulator()
        {
        }

        public Accumulator(int integerEntered)
        {
            int enteredInteger = integerEntered;
        }

        public int AccumulateValues()
        {
            int accumulatedValue = 0;
            int counterValue = 1;
            while (counterValue <= integerEntered)
            {
                Console.WriteLine("\tPasses through loop = {0}", accumulatedValue);
                accumulatedValue = accumulatedValue + counterValue;
                counterValue = counterValue + 1;
            }
            return accumulatedValue;
        }

    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
命名空间项目
{
类累加器
{
整数积分;
公共累加器()
{
}
公共累加器(整数整数)
{
整数输入整数=整数;
}
公共整数累加值()
{
int累计值=0;
int计数器值=1;

而(counterValue当您通过包含一个int参数的构造函数实例化Accumulator的新实例时,您将传递的值设置为等于类中的字段(将它们都设置为0)

您的累加器类应该如下所示:

class Accumulator
{
    int integerEntered;

    public Accumulator()
    {
    }

    public Accumulator(int passedInteger)
    {
        //Local field is equal to passedInteger, not the other way around.
        integerEntered = passedInteger;
    }

    public int AccumulateValues()
    {
        int accumulatedValue = 0;
        int counterValue = 1;
        while (counterValue <= integerEntered)
        {
            Console.WriteLine("\tPasses through loop = {0}", accumulatedValue);
            accumulatedValue = accumulatedValue + counterValue;
            //Increment does the same thing you were doing
            counterValue++;
        }
        return accumulatedValue;
    }

}
类累加器
{
整数积分;
公共累加器()
{
}
公共累加器(整数次整数)
{
//局部字段等于passedInteger,而不是相反。
整数=传递整数;
}
公共整数累加值()
{
int累计值=0;
int计数器值=1;

while(counterValue看起来问题实际上可能与值构造函数有关。调用此行时:
累加器编号=新累加器(输入值);

正在使用值构造函数创建一个新的累加器:

public Accumulator(int integerEntered)
{
    int enteredInteger = integerEntered;
}
问题是integerEntered并没有真正保存在任何地方,一旦enteredInteger超出范围(构造函数末尾),就Accumulator对象而言,输入的值基本上丢失了。我认为您需要的是:

public Accumulator(int integerEntered)
{
    integerEntered = integerEntered;
}
作为一个头脑清醒的人,你可能必须这样做;
另外,我认为您希望在AccumerateValues()中,从while循环的每次迭代积分中减去1。

需要更改2-3项内容

1) 您没有将值赋给构造函数中的
整数
,因此我对其进行了更改

2) 您应该将
整数
作为属性,因此我将其更改为
公共整数{get;set;}

3) 计算计数
累加值的逻辑是
。实际上数学公式是整数n is=(n*(n+1))/2的总和,所以我也修改了它

试试这个

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Project
{
    class Accumulator
    {

        public int integerEntered { get; set; }     

        public Accumulator()
        {
        }

        public Accumulator(int integerPassed)
        {
            integerEntered = integerPassed;
        }

        public int AccumulateValues()
        {
            int accumulatedValue = 0;
            if(integerEntered > 0)
            {
                accumulatedValue = (integerEntered * (integerEntered + 1))/2;
            }
            return accumulatedValue;

        }

    }
}

注意,您可能可以去掉datamember Integereneted和您的值构造函数(将值保存到局部变量,该变量在值构造函数的调用结束时丢失)。若要赋予值构造函数更大的含义,您应该将传递给它的参数保存到datamember。在这种情况下,这样做应该是完全合法的:公共累加器(int integerEntered){integerEntered=integerEntered;}