2013-10-05 42 views
-1

我是一個試圖學習編程的業餘愛好者。簡單迴文問題

我正在嘗試做迴文。但是,我在代碼中出現錯誤。

public class Palindrome { 

    public static void main(String args[]) 
    { 
    String pal = "abc"; 

    public static void check(String pal) 
    { 
     if(pal==null) 
     { 
      System.out.println("Null Value..Exit"); 
     } 
     else 
     { 
      StringBuilder str= new StringBuilder(pal); 
      str.reverse(); 
      System.out.println(str.reverse()); 
     } 
    } 
    } 

} 

我哪裏錯了?對不起,我對編程很陌生。只是想學習!

+5

您不能在Java中嵌套函數。 –

+1

首先,在你編寫任何代碼之前,停下來想一想程序應該做什麼。它應該採取哪些步驟?結果應該是什麼?它應該如何構建?在開始編寫代碼之前有一個計劃。 – Jesper

+1

當您發生錯誤時,首先要做的是讀取它,因爲該消息指出什麼是錯誤的以及在哪裏。如果你不理解它,然後發佈它,因爲我們不是清醒的嚮導,錯誤消息也幫助我們。 –

回答

3

你需要在你的代碼下面的變化。

public static void main(String args[]) { 
    String pal = "abc"; 
    check(pal); // Nested methods are not allowed, thus calling the check 
       // method, which is now placed outside main 
} 

public static void check(String pal) { 
    if (pal == null) { 
     System.out.println("Null Value..Exit"); 
    } else { 
     StringBuilder str = new StringBuilder(pal); 

     // I think I confused you by doing the below. 
     // str = str.reverse(); // commenting this 

     str.reverse(); // adding this 
     // str.reverse(); reverse will affect the str object. I just assigned it back to make it easier for you 
     // That's why if you add a SOP with str.reverse, it'll reverse the string again, which will give the original string back 
     // Original string will always be equal to itself. that's why your if will always be true 
     // give a SOP with str.toString, not reverse. 

     // str.toString is used because str is a StringBuilder object and not a String object. 
     if (pal.equals(str.toString())) { // if the string and its reverse are equal, then its a palindrome 
      System.out.println("Palindrome"); 
     } else { 
      System.out.println("Not a Palindrome"); 
     } 
    } 
} 
+0

哇。這是儘可能詳細的! 你可以建議我一個學習java基礎知識的好地方嗎? –

+0

@RockyBalBoa - 你走了。 [Java基礎教程](http://docs.oracle.com/javase/tutorial/java/nutsandbolts/)。 – SudoRahul

+1

@RockyBalBoa - 我也建議你開始使用IDE。它將幫助您修復小問題,避免編譯問題。對於邏輯問題,你總是可以回到這裏:) – SudoRahul

2

你不能在另一個方法內寫入一個方法。

public class Palindrome { 

    public static void main(String args[]) 
    { 
    String pal = "abc"; 
check(pal); 

    } 
    public static void check(String pal) 
    { 
     if(pal==null) 
     { 
      System.out.println("Null Value..Exit"); 
     } 
     else 
     { 
      StringBuilder str= new StringBuilder(pal); 
      str.reverse(); 
      System.out.println(str.reverse()); 
     } 
    } 
} 
+0

非常感謝! –

+1

@javaBeginner給出這個你應該添加函數調用爲'check(pal);'以及 – exexzian

0
public static boolean checkPalindrome(String str) { 
    return str.equalsIgnoreCase(new StringBuilder(str).reverse()); 
} 
0
private static boolean isPalindrome(String str) { 
    if (str == null) 
     return false; 
    StringBuilder strBuilder = new StringBuilder(str); 
    strBuilder.reverse(); 
    return strBuilder.toString().equals(str); 
} 

你或許應該這樣做。