2013-10-23 22 views
0

我想做一个简单的程序,要求用户输入一个整数。一旦程序收到它所需要的输入并将其存储起来,然后从1开始对输入整数进行计数,并将计数总和相加。然后以有意义的方式向用户显示结果,并在他们想要处理另一个号码时提示他们。这个程序的重点是使用循环和多个类。我知道我非常接近期望的最终产品,但无法弄清楚为什么AccumulateValue()方法无法正常工作。它似乎没有进入我作出的有条件的声明。如果有人能给我一些见解,那我的问题就会很棒!使用多个类

这里是我的代码:

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

    } 
} 
+0

正如你可能摆脱数据成员integerEntered的说明和你的价值构造(保存价值,这是在价值构造的通话结束时失去了局部变量)。为了给你的值构造函数更多的含义,你应该将传递给它的参数保存到数据库中。在这种情况下,这应该是完全合法的: public Accumulator(int integerEntered) { integerEntered = integerEntered; } – Teeknow

回答

0

当您通过包含你一个int参数它的构造函数实例累加器的新实例设置传递的值等于该类中的字段(将它们都设置为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; 
    } 

} 
0

它看起来像这个问题实际上可以与你的价值构造。当这条线被称为:

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

的问题是,integerEntered是不是真的保存在任何地方,一旦enteredInteger超出范围(结束: Accumulator number = new Accumulator(enteredValue);

一个新的累加器正与你的价值构造制成的构造函数),就累加器对象而言,输入的值实质上已经丢失。我想你想要的是:

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

作为一个头,你可能必须做this.integerEntered = integerEntered; 另外我想你想从accumulateValues()中的int循环的每个迭代中减去1。

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; 

     } 

    } 
}