using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Timers;
using System.Threading;namespace ConsoleApplication232
{
    class Program
    {
        static void Main(string[] args)
        {
            string answer = Console.ReadLine().ToUpper();
            CropForecaster myForecas = new CropForecaster();
           
            while (answer == "Y")
            {
               Console.WriteLine("Wheat crop size in tons :{0:N2}" ,myForecas.WheatCrop());
               Console.WriteLine("Would you like to perform another crop forecast? Y)es N)o");
               answer = Console.ReadLine().ToUpper();
            }
        }
    }    class Water
    {
        public static double CalcuateRainfallDay()
        {
            
            Random random = new Random();
            return (double)(random.Next(40,100));
        }
    }    class CropForecaster
    {
        private double rainfallDay=0f;
        private bool ranfallDayUpdate = true;        public CropForecaster()
        {
            System.Timers.Timer timer = new System.Timers.Timer(20000);
            timer.Elapsed += new ElapsedEventHandler(OneTime);
            timer.Enabled = true;
        }        public void OneTime(Object sender, ElapsedEventArgs e)
        {
            Console.WriteLine("chu fa shijian :{0}", e.SignalTime);
            ranfallDayUpdate = true;
        }        public double RainfallDay
        {
            get
            {
                if (ranfallDayUpdate)
                {
                    Console.WriteLine(Water.CalcuateRainfallDay());
                    rainfallDay = (double)(Water.CalcuateRainfallDay());
                    ranfallDayUpdate = false;
                    
                }                return rainfallDay;            }
        }        public double ComplexResultA()
        {
            
            return ((RainfallDay / 2) + (RainfallDay / 3) + (RainfallDay / 4));
        }
        
        public double ComplexResultB()
        {
            
            return (((RainfallDay / 10) - 100) + (RainfallDay / 100));
        }        public double WheatCrop()
        {
            Console.WriteLine("Commencing forecast calculations.Please wait....");
           
            return ((ComplexResultA() / 2 + ComplexResultB() / 4 + ComplexResultB()) * 100000D);
        }
    }
}

解决方案 »

  1.   


     public static double CalcuateRainfallDay()
      {
        
      Random random = new Random();
      return (double)(random.Next(40,100)); //你这个地方返回的RainfallDay 始终小于100
      }  public double ComplexResultB()
      {
        
      return (((RainfallDay / 10) - 100) + (RainfallDay / 100)); //这个地方先除以10又减去100,得到的肯定是负数
      } public double ComplexResultB()
      {
        
      return (((RainfallDay / 2) - 2) + (RainfallDay / 100)); //这样修改得到的肯定就是正数了
      }
      

  2.   


    public double ComplexResultA()
      {
        
      return ((RainfallDay / 2) + (RainfallDay / 3) + (RainfallDay / 4));
      }
        
      public double ComplexResultB()
      {
        
      return (((RainfallDay / 10) - 100) + (RainfallDay / 100));
      }
    你看看这两个函数,为什么算出来的会是负数,ComplexResultA应该不会有负数 ,但是 ComplexResultB里面   
    (RainfallDay / 10) - 100如果前面的值小于100  不就会出现负数吗 
      

  3.   


    ComplexResultB() 方法计算后的确是负数,但是得出计算最后结果的是WheatCrop()方法,而且这个方法还在最后乘以了100000  ,为什么得到的结果还是负数 , 想不通
      

  4.   


    ComplexResultB() 方法计算后的确是负数,但是得出计算最后结果的是WheatCrop()方法,而且这个方法还在最后乘以了100000 ,为什么得到的结果还是负数 , 想不通
      

  5.   

    return ((RainfallDay / 2) + (RainfallDay / 3) + (RainfallDay / 4));
      }
        
      public double ComplexResultB()
      {
        
      return (((RainfallDay / 10) - 100) + (RainfallDay / 100));
      }  public double WheatCrop()
      {
      Console.WriteLine("Commencing forecast calculations.Please wait....");
        
      return ((ComplexResultA() / 2 + ComplexResultB() / 4 + ComplexResultB()) * 100000D);
    楼主简单的算一下A返回的结果, 13RainfallDay /12 
    B (9RainfallDay -10000)/100
    最后方法里边,负数*10000也还是负数吧
      

  6.   

    ComplexResultA=13/12*RainfallDay 
    ComplexResultB=11/100*RainfallDay-100
    ComplexResultA() / 2 + ComplexResultB() / 4 + ComplexResultB()
    =ComplexResultA() / 2 + 5/4 *ComplexResultB()
    =13/24*RainfallDay +11/80 *ComplexResultB()-125
    =163/240*RainfallDay-125
    <0
    负数乘10000,还是负数……
      

  7.   

    这样好看点using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Timers;
    using System.Threading;namespace ConsoleApplication232
    {
      class Program
      {
      static void Main(string[] args)
      {
      string answer = Console.ReadLine().ToUpper();
      CropForecaster myForecas = new CropForecaster();
        
      while (answer == "Y")
      {
      Console.WriteLine("Wheat crop size in tons :{0:N2}" ,myForecas.WheatCrop());
      Console.WriteLine("Would you like to perform another crop forecast? Y)es N)o");
      answer = Console.ReadLine().ToUpper();
      }
      }
      }  class Water
      {
      public static double CalcuateRainfallDay()
      {
        
      Random random = new Random();
      return (double)(random.Next(40,100));
      }
      }  class CropForecaster
      {
      private double rainfallDay=0f;
      private bool ranfallDayUpdate = true;  public CropForecaster()
      {
      System.Timers.Timer timer = new System.Timers.Timer(20000);
      timer.Elapsed += new ElapsedEventHandler(OneTime);
      timer.Enabled = true;
      }  public void OneTime(Object sender, ElapsedEventArgs e)
      {
      Console.WriteLine("chu fa shijian :{0}", e.SignalTime);
      ranfallDayUpdate = true;
      }  public double RainfallDay
      {
      get
      {
      if (ranfallDayUpdate)
      {
      Console.WriteLine(Water.CalcuateRainfallDay());
      rainfallDay = (double)(Water.CalcuateRainfallDay());
      ranfallDayUpdate = false;
        
      }  return rainfallDay;  }
      }  public double ComplexResultA()
      {
        
      return ((RainfallDay / 2) + (RainfallDay / 3) + (RainfallDay / 4));
      }
        
      public double ComplexResultB()
      {
        
      return (((RainfallDay / 10) - 100) + (RainfallDay / 100));
      }  public double WheatCrop()
      {
      Console.WriteLine("Commencing forecast calculations.Please wait....");
        
      return ((ComplexResultA() / 2 + ComplexResultB() / 4 + ComplexResultB()) * 100000D);
      }
      }
    }