0

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

这是我的代码:

累加器应用程序.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();
        }

    }
}

累加器.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;
        }

    }
}
4

3 回答 3

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

}
于 2013-10-23T04:26:28.430 回答
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。

于 2013-10-23T04:47:09.873 回答
0

有 2 -3 件事需要改变

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

2)你应该integerEntered作为一个财产,所以我把它改成了 public int integerEntered { get; set; }

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

        }

    }
}
于 2013-10-23T04:59:29.807 回答