所以我正在做一個在線編碼挑戰(zhàn),遇到了這個讓我難過的問題:這是我的代碼: static void Main(String[] args) { int noOfRows = Convert.ToInt32(Console.ReadLine()); for (int i = 0; i < noOfRows; i++) { string odds = ""; string evens = ""; //get the input word from console string word = Console.ReadLine(); for (int j = 0; j < word.Length; j++) { //if the string's current char is even-indexed... if (word[j] % 2 == 0) { evens += word[j]; } //if the string's current char is odd-indexed... else if (word[j] % 2 != 0) { odds += word[j]; } } //print a line with the evens + odds Console.WriteLine(evens + " " + odds); } }本質(zhì)上,這個問題要我從控制臺行獲取字符串并在左側(cè)打印偶數(shù)索引字符(從 index=0 開始),后跟一個空格,然后是奇數(shù)索引字符。所以當我嘗試“Hacker”這個詞時,我應(yīng)該看到打印為“Hce akr”的行。當我調(diào)試它時,我看到代碼成功地將字母'H'放在左邊(因為它是index = 0,因此是偶數(shù)),并將字母'a'放在右邊(奇數(shù)索引)。但是當它到達字母“c”時,它不是通過第一個 IF 路徑(偶數(shù)索引),而是跳過它并轉(zhuǎn)到奇數(shù)索引路徑,并將其放在右側(cè)?有趣的是,當我嘗試“Rank”這個詞時,它工作正常并打印出正確的語句:“Rank”,但其他詞卻沒有。我得到不同的結(jié)果真是奇怪。我錯過了什么?
3 回答
- 3 回答
- 0 關(guān)注
- 139 瀏覽
添加回答
舉報
0/150
提交
取消