2014-03-26 4 views
-3

입력 한 숫자가 회문인지 아닌지 어떻게 입력합니까? 첫 번째 부분은 방금 작동하게 만드는 방법을 알아 내려고하는 else 문에 막혀있는 부분입니다. 내 코드를 heres이 코드에서 계속 오류가 발생합니다

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

"첫 번째 부분"은 무엇입니까? 어떤 다른 문장? (그 중 세 가지가 있습니다.) 해결하려는 문제에 대해보다 구체적으로 설명해야합니다. – Wyzard

+0

첫 번째 else 문. if else 문 안의 if else 문은 아닙니다. @Wyzard – programmingnoob

답변

0

귀하의 질문은 사용자가 입력 한 문자열이 회문하지 않는 사용자를 말하려고하는 경우, 다음 울부 짖는 소리를 참조 ...

가지고 당신이 if 문을 넣어 시도, 매우 모호 괄호 안에? 그들없이 if/for 문을 작성할 때는주의해야합니다.

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; 

} 
관련 문제