2012-04-11 51 views
0

這個程序運行時,我得到以下輸出時的程序錯誤出來的時候從一個方法調用返回的值:無法發送的數組,並在參考運行排序

Please enter the Social Security Number for taxpayer 0: 111111111 
Please enter the gross income for taxpayer 0: 20000 
Please enter the Social Security Number for taxpayer 1: 555555555 
Please enter the gross income for taxpayer 1: 50000 
Please enter the Social Security Number for taxpayer 2: 333333333 
Please enter the gross income for taxpayer 2: 5464166 
Please enter the Social Security Number for taxpayer 3: 222222222 
Please enter the gross income for taxpayer 3: 645641 
Please enter the Social Security Number for taxpayer 4: 444444444 
Please enter the gross income for taxpayer 4: 29000 
Taxpayer # 1 SSN: 111111111, Income is $20,000.00, Tax is $0.00 
Taxpayer # 2 SSN: 555555555, Income is $50,000.00, Tax is $0.00 
Taxpayer # 3 SSN: 333333333, Income is $5,464,166.00, Tax is $0.00 
Taxpayer # 4 SSN: 222222222, Income is $645,641.00, Tax is $0.00 
Taxpayer # 5 SSN: 444444444, Income is $29,000.00, Tax is $0.00 

未處理的異常:System.InvalidOperationException:未能比較數組中的兩個元素。 ---> System.ArgumentException:至少有一個對象必須實現IComparable。
在System.Collections.Comparer.Compare(對象A,對象B)
在System.Collections.Generic.ObjectComparer`1.Compare(T X,T Y)
在System.Collections.Generic.ArraySortHelper`1 .SwapIfGreaterWithItems(T [] keys,IComparer`1 comparer,Int32 a,Int32 b)
at System.Collections.Generic.ArraySortHelper`1.QuickSort(T [] keys,Int32 left,Int32 right,IComparer`1 comparer)
在System.Collections.Generic.ArraySortHelper`1.Sort(T []按鍵,索引的Int32,的Int32長度,IComparer`1比較器)
---內部異常堆棧跟蹤的結尾---
在系統。 Collections.Generic.ArraySortHelper`1.Sort(T [] k在System.Array.Sort [T](T []數組,Int32索引,Int32長度,IComparer`1比較器) 在System.Array.Sort [T ](T []數組)
在Assignment5.Taxpayer.Main(字串[] args)在Program.cs中:線150

在通知應該是這樣的稅額的線的端部的0? ??

下面是代碼:

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

namespace taxes 
{ 
    class Rates 
    { 
     // Create a class named rates that has the following data members: 
     private int incLimit; 
     private double lowTaxRate; 
     private double highTaxRate; 

     // use read-only accessor 
     public int IncomeLimit 
     { get { return incLimit; } } 
     public double LowTaxRate 
     { get { return lowTaxRate; } } 
     public double HighTaxRate 
     { get { return highTaxRate; } } 

     //A class constructor that assigns default values 
     public Rates() 
     { 
      int limit = 30000; 
      double lowRate = .15; 
      double highRate = .28; 
      incLimit = limit; 
      lowTaxRate = lowRate; 
      highTaxRate = highRate; 
     } 
     //A class constructor that takes three parameters to assign input values for limit, low rate and high rate. 
     public Rates(int limit, double lowRate, double highRate) 
     { 
     } 
     // A CalculateTax method that takes an income parameter and computes the tax as follows: 
     public int CalculateTax(int income) 
     { 
      int limit = 0; 
      double lowRate = 0; 
      double highRate = 0; 
      int taxOwed = 0; 
      // If income is less than the limit then return the tax as income times low rate. 
      if (income < limit) 
       taxOwed = Convert.ToInt32(income * lowRate); 
      // If income is greater than or equal to the limit then return the tax as income times high rate. 
      if (income >= limit) 
       taxOwed = Convert.ToInt32(income * highRate); 
      return taxOwed; 
     } 


    } //end class Rates 

     // Create a class named Taxpayer that has the following data members: 
     public class Taxpayer 
     { 
      //Use get and set accessors. 
      string SSN 
      { get; set; } 
      int grossIncome 
      { get; set; } 

      // Use read-only accessor. 
      public int taxOwed 
      { 
       get { return taxOwed; } 
      } 

      // The Taxpayer class should be set up so that its objects are comparable to each other based on tax owed. 
      class taxpayer : IComparable 
      { 
       public int taxOwed { get; set; } 
       public int income { get; set; } 
       int IComparable.CompareTo(Object o) 
       { 
        int returnVal; 
        taxpayer temp = (taxpayer)o; 
        if (this.taxOwed > temp.taxOwed) 
         returnVal = 1; 
        else 
         if (this.taxOwed < temp.taxOwed) 
          returnVal = -1; 
         else 
          returnVal = 0; 
        return returnVal; 

       } // End IComparable.CompareTo 
      } //end taxpayer IComparable class 
       // **The tax should be calculated whenever the income is set. 
       // The Taxpayer class should have a getRates class method that has the following. 
       public static void GetRates() 
       { 
        // Local method data members for income limit, low rate and high rate. 
        int incLimit = 0; 
        double lowRate; 
        double highRate; 
        string userInput; 
        // Prompt the user to enter a selection for either default settings or user input of settings. 
        Console.Write("Would you like the default values (D) or would you like to enter the values (E)?: "); 
        /* If the user selects default the default values you will instantiate a rates object using the default constructor 
        * and set the Taxpayer class data member for tax equal to the value returned from calling the rates object CalculateTax method.*/ 
        userInput = Convert.ToString(Console.ReadLine()); 
        if (userInput == "D" || userInput == "d") 
        { 
         Rates rates = new Rates(); 
         rates.CalculateTax(incLimit); 
        } // end if 
        /* If the user selects to enter the rates data then prompt the user to enter values for income limit, low rate and high rate, 
        * instantiate a rates object using the three-argument constructor passing those three entries as the constructor arguments and 
        * set the Taxpayer class data member for tax equal to the valuereturned from calling the rates object CalculateTax method. */ 
        if (userInput == "E" || userInput == "e") 
        { 
         Console.Write("Please enter the income limit: "); 
         incLimit = Convert.ToInt32(Console.ReadLine()); 
         Console.Write("Please enter the low rate: "); 
         lowRate = Convert.ToDouble(Console.ReadLine()); 
         Console.Write("Please enter the high rate: "); 
         highRate = Convert.ToDouble(Console.ReadLine()); 
         Rates rates = new Rates(incLimit, lowRate, highRate); 
         rates.CalculateTax(incLimit); 
        } 
       } 

      static void Main(string[] args) 
      { 

       Taxpayer[] taxArray = new Taxpayer[5]; 
       Rates taxRates = new Rates(); 
       // Implement a for-loop that will prompt the user to enter the Social Security Number and gross income. 
       for (int x = 0; x < taxArray.Length; ++x) 
       { 
        taxArray[x] = new Taxpayer(); 
        Console.Write("Please enter the Social Security Number for taxpayer {0}: ", x + 1); 
        taxArray[x].SSN = Console.ReadLine(); 

        Console.Write("Please enter the gross income for taxpayer {0}: ", x + 1); 
        taxArray[x].grossIncome = Convert.ToInt32(Console.ReadLine()); 

       } 

       Taxpayer.GetRates(); 

       // Implement a for-loop that will display each object as formatted taxpayer SSN, income and calculated tax. 
       for (int i = 0; i < taxArray.Length; ++i) 
       { 
        Console.WriteLine("Taxpayer # {0} SSN: {1}, Income is {2:c}, Tax is {3:c}", i + 1, taxArray[i].SSN, taxArray[i].grossIncome, taxRates.CalculateTax(taxArray[i].grossIncome)); 

       } // end for 
       // Implement a for-loop that will sort the five objects in order by the amount of tax owed 
       Array.Sort(taxArray); 
       Console.WriteLine("Sorted by tax owed"); 
       for (int i = 0; i < taxArray.Length; ++i) 
       { 
        Console.WriteLine("Taxpayer # {0} SSN: {1}, Income is {2:c}, Tax is {3:c}", i + 1, taxArray[i].SSN, taxArray[i].grossIncome, taxRates.CalculateTax(taxArray[i].grossIncome)); 

       } 
      } //end main 
     } // end Taxpayer class 

} //end 

任何線索,爲什麼金額就要到了爲0,爲什麼排序不工作?

回答

2
  • 你的第二個Rates構造函數什麼都不做。

  • Taxpayer.taxOwed是無限遞歸的。

  • 我不認爲有必要呼籲Convert.ToString一個字符串:

    userInput = Convert.ToString(Console.ReadLine()); 
    
  • 你永遠不使用電話Rates.CalculateTax的結果。

  • GetRates不會對它實例化的Rates對象做任何事情;稱它看起來什麼都不做。

  • 嵌套類taxpayer似乎沒有用於任何東西。您是否試圖製作TaxpayerIComparable?您將在Taxpayer課中實施IComparable。你不應該定義一個單獨的類。您可能還想實施IComparable<Taxpayer>

  • 您正在使用您的程序類來處理您的數據。你應該把Taxpayer從你的程序類中分離出來。它應該是一個單獨的類,如Rates是。

+0

謝謝,是的,我試圖實現一個IComparable來排序2個數組SSN和收入在一起。我改變納稅人類以包括'公共類納稅人:IComparable ',但現在我得到了「Assignment5.Taxpayer'沒有實現接口成員」爲該行和「int ** rable.CompareTo **(對象o) 「我得到錯誤\t 2'\t'Assignment5.Taxpayer.IComparable.CompareTo(object)':包含類型不實現接口'System.IComparable' – programmerNOOB 2012-04-11 14:01:33

+0

@programmerNOOB您的'CompareTo'方法在嵌套類'納稅人'而不是課稅'納稅人'。你應該完全移除納稅人。您應該將'CompareTo'方法移到'Taxpayer'類,並且您應該將'main'方法提取到它自己的靜態'Program'類中。 – 2012-04-13 02:30:43

+0

@programmerNOOB我也很好奇爲什麼這是至少第三次這個確切的代碼在過去四個月發佈。 – 2012-04-13 02:31:42

1
Rates rates = new Rates(incLimit, lowRate, highRate); 
         rates.CalculateTax(incLimit); 

你傳遞一個值給構造器,在沒有實現

//A class constructor that takes three parameters to assign input values for limit, low rate and high rate. 
     public Rates(int limit, double lowRate, double highRate) 
     { 
     } 

將值賦給這個構造內部的方法,並嘗試運行程序, 嘗試調試,並告訴我們在哪裏你面臨的問題。

+0

謝謝我填寫了這個方法,但我仍然有其他問題。運行時錯誤是:無法從該調用中比較數組中的兩個元素:'Array.Sort(taxArray);'我知道需要給出2個參考,但這是我正在弄清楚的問題。另外我不確定如何獲得在我的執行結果'Console.WriteLine(「納稅人#{0} SSN:{1},收入爲{2:c},稅爲{3:c}}」中返回的實際美元金額「 ,i + 1,taxArray [i] .SSN,taxArray [i] .grossIncome,taxRates.CalculateTax(taxArray [i] .grossIncome));' – programmerNOOB 2012-04-11 13:33:21