2014-03-26 75 views
-3

我如何使else語句輸入是否我輸入的數字是迴文或不是?第一部分工作,我只是卡在else語句試圖找出如何使其工作。我的繼承人代碼不斷收到此代碼錯誤

import java.util.*; 
public class Lab6 
{ 
    public static void main (String [] args) 
    { 
     String pal1, pal2=""; 
     int choice; 
     Scanner in = new Scanner(System.in); 


     System.out.println("Word(w) or Number(n)?"); 
     choice = in.nextLine().charAt(0); 

     if (choice == 'w') { 

      System.out.println("Enter a word: "); 
      pal1= in.nextLine(); 

      int length = pal1.length(); 


      for (int i = length - 1 ; i >= 0 ; i--) 
      pal2 = pal2 + pal1.charAt(i); 


     if (pal1.equals(pal2)) 
      System.out.println("The word you entered is a palindrome."); 
     else 
      System.out.println("The word you entered is not a palindrome."); 
     } 
     else{ 

      System.out.println("Enter a bunch of numbers: "); 
      pal1 = in.nextLine(); 

      pal1 = String.valueOf(in.nextInt()); 
      int numLength = pal1.length(); 

      for (int j = numLength - 1 ; j >= 0 ; j--) 
      pal2 = pal2 + pal1.charAt(j); 

     if (pal1.equals(pal2)) 
      System.out.println("The numbers you entered is a palindrome."); 
     else 
      System.out.println("The numbers you entered is not a palindrome."); 
     } 
    } 
} 
+1

你得到什麼錯誤?什麼不按預期工作? –

+0

什麼「第一部分」?哪個'else'語句? (其中有三個)。您需要更具體地瞭解您要解決的問題。 – Wyzard

+0

第一個if else聲明。而不是if else語句中的if else語句。 @Wyzard – programmingnoob

回答

0

你提的問題是非常模糊的,如果你試圖告訴輸入的字符串不是迴文用戶,然後看到波紋管......

您是否嘗試過把if語句括號內的?如果沒有他們的話,你必須小心謹慎。

public class Lab6 
{ 
    public static void main (String [] args) 
    { 
     String pal1, pal2=""; 
     int choice; 
     Scanner in = new Scanner(System.in); 


     System.out.println("Word(w) or Number(n)?"); 
     choice = in.nextLine().charAt(0); 

     if (choice == 'w') { 

      System.out.println("Enter a word: "); 
      pal1= in.nextLine(); 

      int length = pal1.length(); 


      for (int i = length - 1 ; i >= 0 ; i--) 
      pal2 = pal2 + pal1.charAt(i); 


     if (pal1.equals(pal2)){ 
      System.out.println("The word you entered is a palindrome."); 
     } else{ 
      System.out.println("The word you entered is not a palindrome."); 
     } 
     } 
     else{ 

      System.out.println("Enter a bunch of numbers: "); 
      pal1 = in.nextLine(); 

      pal1 = String.valueOf(in.nextInt()); 
      int numLength = pal1.length(); 

      for (int j = numLength - 1 ; j >= 0 ; j--) 
      pal2 = pal2 + pal1.charAt(j); 

     if (pal1.equals(pal2)) 
      System.out.println("The numbers you entered is a palindrome."); 
     else 
      System.out.println("The numbers you entered is not a palindrome."); 
     } 
    } 
} 

祝你的實驗室;)

而且這樣的事情可能更有效:

boolean isPal(String input) { 
    // go through half the string length 
    for (int i = 0; i < input.length()/2; i++) { 
     // match first half to second half (regardless if odd or even) 
     // -1 because strings starta at a 0 index 
     if (input.charAt(i) != input.charAt(input.length() - 1 - i)) { 
      return false; 
     } 
    } 
    return true; 

}