2014-03-26 77 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; 

}