2015-06-09 54 views
2

這是我做的運動:分割字符串,然後解析到整數

寫一個程序作爲輸入格式ABCD(如2011)一個四位數字,並執行以下操作:

•計算數字的總和(在我們的示例中爲2 + 0 + 1 + 1 = 4)。

•在控制檯上打印相反順序的編號:dcba(在我們的示例1102中)。

•將最後一位放在第一個位置:dabc(在我們的示例1201中)。

•交換第二個和第三個數字:acbd(在我們的示例2101中)。

這裏是我的代碼:

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

namespace ConsoleApplication6 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      string FourDigitNum = Console.ReadLine(); 
      string[] digits = FourDigitNum.Split(); 
      int firstDigit = int.Parse(digits[0]); 
      int secondDigit = int.Parse(digits[1]); 
      int thirdDigit = int.Parse(digits[2]); 
      int fourthDigit = int.Parse(digits[3]); 
      int sum = firstDigit + secondDigit + thirdDigit + fourthDigit; 
      string reversed = digits[3] + digits[2] + digits[1] + digits[0]; 
      string lastCharFirst = digits[3] + digits[0] + digits[1] + digits[0]; 
      string exchanged = digits[0] + digits[2] + digits[1] + digits[3]; 
      Console.WriteLine("The Sum is: {0}", sum); 
      Console.WriteLine("The Reversed number is: {0}", reversed); 
      Console.WriteLine("The Last Digit is First: {0}", lastCharFirst); 
      Console.WriteLine("The Second and Third Digit Exchanged: {0}", exchanged); 
     } 
    } 
} 

我得到的錯誤,當我用1100作爲輸入:

未處理的異常:System.IndexOutOfRangeException:索引 的奔DS外陣列。在 ConsoleApplication6.Program.Main在 C(字串[] args):\用戶\用戶1 \文件S \的Visual Studio 2013 \項目\ ConsoleApplication6 \的Program.cs:行16

編輯:謝謝這麼多,我誤解了Split();工作。這是我最後的工作代碼:

namespace ConsoleApplication6 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      string digits = Console.ReadLine(); 
      int firstDigit = (int)Char.GetNumericValue(digits[0]); 
      int secondDigit = (int)Char.GetNumericValue(digits[1]); 
      int thirdDigit = (int)Char.GetNumericValue(digits[2]); 
      int fourthDigit = (int)Char.GetNumericValue(digits[3]); 
      int sum = firstDigit + secondDigit + thirdDigit + fourthDigit; 
      Console.WriteLine("The Sum is: {0}", sum); 
      Console.WriteLine("The Reversed number is: {3}{2}{1}{0}", firstDigit, secondDigit, thirdDigit, fourthDigit); 
      Console.WriteLine("The Last Digit is First: {3}{0}{1}{2}", firstDigit, secondDigit, thirdDigit, fourthDigit); 
      Console.WriteLine("The Second and Third Digit Exchanged: {0}{2}{1}{3}", firstDigit, secondDigit, thirdDigit, fourthDigit); 
     } 
    } 
} 
+1

那麼你做了什麼調試?提示:在'int firstDigit = ...'處放置一個斷點並查看'digits' ... –

+0

Your FourDigitNum.Split()沒有做你認爲它會做的事 – Shar1er80

+0

我使用的測試輸入是1100 –

回答

5

沒有必要爲Split,我們假定你是通過做Split()它會分裂每個字符爲一個字符串數組的元素。它不是,Split()沒有任何參數將分裂在空白字符,並且因爲在你的字符串1102你沒有任何空格字符,你會得到一個單一元素的數組。因此例外。

使用字符串作爲字符數組,您可以訪問每個索引並通過連接字符創建反轉字符串。還要學會調試和逐步執行代碼,這將有助於您排除代碼故障。

作爲提示使用:

int firstDigit = (int) Char.GetNumericValue(FourDigitNum[0]); 
2

的問題是線

string[] digits = FourDigitNum.Split(); 

假設你有1100的輸入。你認爲這會給[1,1,0,0],但它會給你[1100]

你應該將其更改爲

string digits = Console.ReadLine(); 

string.Split()將分割字符串假設空格作爲分隔符。

-1

您正在編碼正在輸入的字符串的長度,但使用.Split()只會在數組中創建一個項目,而不是四個。

您可以將字符串拆分爲char數組。ToCharArray()創建一個數組,每個數字作爲數組中的自己的項目。

+0

不完全正確。 .Split()實際上分成了一個數組。然而,在他們以'1102'作爲輸入的情況下,它只會給出'digits [0] = 1102',其餘的都不會。 – Emz

+0

在這個例子中做split()是在數組中創建一個單獨的項目,這是怎麼回事? – bill

+0

單個項目不是單個項目數組。 –

0

當你

string[] digits = FourDigitNum.Split(); 

它只是返回只有一個項目的數組。如你預期的分裂不會發生,這就是爲什麼你越來越Index was outside the bounds of the array錯誤,當您試圖

int secondDigit = int.Parse(digits[1]); 
0

您在這裏誤解字符串使用。其核心字符串也是一個字符集合。您可以通過索引直接訪問這些字符。

您的代碼應該是這樣的:

static void Main(string[] args) 
    { 
     string digits = Console.ReadLine(); 
     int firstDigit = Convert.ToInt32(digits[0]); 
     int secondDigit = Convert.ToInt32(digits[1]); 
     int thirdDigit = Convert.ToInt32(digits[2]); 
     int fourthDigit = Convert.ToInt32(digits[3]); 
     int sum = firstDigit + secondDigit + thirdDigit + fourthDigit; 
     string reversed = digits[3] + digits[2] + digits[1] + digits[0]; 
     string lastCharFirst = digits[3] + digits[0] + digits[1] + digits[0]; 
     string exchanged = digits[0] + digits[2] + digits[1] + digits[3]; 
     Console.WriteLine("The Sum is: {0}", sum); 
     Console.WriteLine("The Reversed number is: {0}", reversed); 
     Console.WriteLine("The Last Digit is First: {0}", lastCharFirst); 
     Console.WriteLine("The Second and Third Digit Exchanged: {0}", exchanged); 
    } 

Look here for more information on the String class

0

你FourDigitNum.Split()並沒有這樣做,你以爲你想要的。不是將每個數字拆分成一個數組,而是最終得到一個數組,其中有一個是四位數的元素。

變化

string[] digits = FourDigitNum.Split(); 

string[] digits = FourDigitNum.Select(digit => digit.ToString()).ToArray(); 

請確保您有

using System.Linq; 

用另一隻using statments,和你的代碼的其餘部分可以保持不變。

結果:

2011 
The Sum is: 4 
The Reversed number is: 1102 
The Last Digit is First: 1202 
The Second and Third Digit Exchanged: 2101 
3

「IndexOutOfRangeException」意味着你要訪問一個集合,是超出範圍的元素。在這種情況下,索引1超出了數組digits的範圍。

您可以使用調試器查看陣列中的內容。方便的是,當您在Visual Studio中遇到未捕獲的異常時,它通常會爲您制定一個斷點。

debugger

*的「當地人」選項卡當前選擇,但如果你想編寫自己的表達,你可以選擇「監視」標籤看到Visual Studio Debugging Tutorial

正如你所看到的, digits數組只包含1個元素,digits[0],其中包含您的整個輸入。

至於如何實際獲得字符串數字列表,有很多方法可以做到這一點,這是一個簡單的方法。

string[] digits = FourDigitNum.ToCharArray().Select(c => c.ToString()).ToArray(); 

現在,如果你這樣做是爲學校,老師可能會懷疑你使用Select聲明的,但我會離開盤算的另一種方法作爲練習。

+1

我已經看到整天編碼問題的最佳解釋'string [ ] digits = FourDigitNum.ToCharArray()。Select(c => c.ToString())。ToArray();'在我看來也有這個訣竅..很好的解釋@SamIam – MethodMan

0

沒有定義的字符要分割。你可以去通的每個字符在字符串中象下面這樣:

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

namespace ConsoleApplication6 
{ 
class Program 
{ 
    static void Main(string[] args) 
    { 
     string FourDigitNum = Console.ReadLine(); 
     List<string> digitsList = new List<string>(); 
     foreach (char c in FourDigitNum) 
     { 
      digitsList.Add(c.ToString()); 
     } 
     string[] digits = digitsList.ToArray(); 
     int firstDigit = int.Parse(digits[0]); 
     int secondDigit = int.Parse(digits[1]); 
     int thirdDigit = int.Parse(digits[2]); 
     int fourthDigit = int.Parse(digits[3]); 
     int sum = firstDigit + secondDigit + thirdDigit + fourthDigit; 
     string reversed = digits[3] + digits[2] + digits[1] + digits[0]; 
     string lastCharFirst = digits[3] + digits[0] + digits[1] + digits[0]; 
     string exchanged = digits[0] + digits[2] + digits[1] + digits[3]; 
     Console.WriteLine("The Sum is: {0}", sum); 
     Console.WriteLine("The Reversed number is: {0}", reversed); 
     Console.WriteLine("The Last Digit is First: {0}", lastCharFirst); 
     Console.WriteLine("The Second and Third Digit Exchanged: {0}", exchanged); 

    } 
} 

}

0
using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 

namespace ConsoleApplication6 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      string FourDigitNum = Console.ReadLine(); 
      int firstDigit = int.Parse(FourDigitNum.SubString(0,1)); 
      int secondDigit = int.Parse(FourDigitNum.SubString(1,1)); 
      int thirdDigit = int.Parse(FourDigitNum.SubString(2,1)); 
      int fourthDigit = int.Parse(FourDigitNum.SubString(3,1)); 
      int sum = firstDigit + secondDigit + thirdDigit + fourthDigit; 
      string reversed = fourthDigit.ToString() + thirdDigit.ToString() + secondDigit.ToString() + firstDigit.ToString(); 
      string lastCharFirst = fourthDigit.ToString() + firstDigit.ToString() + secondDigit.ToString() + firstDigit.ToString(); 
      string exchanged = firstDigit.ToString() + thirdDigit.ToString() + secondDigit.ToString() + fourthDigit.ToString(); 
      Console.WriteLine("The Sum is: {0}", sum); 
      Console.WriteLine("The Reversed number is: {0}", reversed); 
      Console.WriteLine("The Last Digit is First: {0}", lastCharFirst); 
      Console.WriteLine("The Second and Third Digit Exchanged: {0}", exchanged); 
     } 
    } 
} 

我沒有檢查這臺機器上,但我認爲它應該工作。嘗試執行一次。