继续使用此代码出错

时间:2014-03-26 01:03:25

标签: java palindrome

如何输入else语句输入我输入的数字是否是回文?第一部分工作nad我只是卡在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 :(得分: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;

}