使用多个类

时间:2013-10-23 04:17:05

标签: c# class methods method-call

我正在尝试创建一个简单的程序,要求用户输入一个整数。一旦程序接收到输入,它就会获取并存储它,然后从1计数到输入整数,并计算总计数。然后它以有意义的方式向用户显示结果,并提示他们是否要处理另一个号码。该程序的要点是使用循环和多个类。我知道我非常接近所需的最终产品,但无法弄清楚为什么AccumulateValue()方法无法正常工作。它似乎没有进入我所做的有条件的while语句。如果有人能给我一些关于我的问题的见解会很棒!

这是我的代码:

AccumulatorApp.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();
        }

    }
}

Accumulator.cs

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;
        }

    }
}

3 个答案:

答案 0 :(得分:0)

当您通过包含一个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;
    }

}

答案 1 :(得分:0)

看起来问题实际上可能与您的值构造函数有关。调用此行时:     Accumulator number = new Accumulator(enteredValue);

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

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

问题是integerEntered并没有真正保存在任何地方,一旦enterInteger超出范围(构造函数的结尾),就Accumulator对象而言,输入的值基本上就会丢失。我想你想要的是:

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

作为抬头,您可能必须这样做.integerEntered = integerEntered; 另外我想你想在AccumulateValues()的while循环的每次迭代中从integerEntered中减去1。

答案 2 :(得分:0)

需要更改2-3件事

1)你没有在构造函数中为integerEntered赋值,所以我改了它

2)您应integerEntered作为财产,因此我将其更改为public int integerEntered { get; set; }

3)计算计数的逻辑AccumulateValues ..实际数学公式是整数n的总和n =(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;

        }

    }
}