Java Do-While循环没有退出

时间:2015-04-22 17:50:01

标签: java loops while-loop

您好我是编程的新手,我正在学习Java上的数据结构,我正在制作一个工作正常的简单Palindrome程序,但我唯一想到的是,当我输入字符串退出程序时,它继续到循环。当用户输入任何东西时,我试图让它继续循环,而不是"!"

package Palindrome;
import java.util.Stack;
import java.util.Scanner;
public class Palindrome {
static String input;
static String reverseInput = "";
public Palindrome() {
    // TODO Auto-generated constructor stub
}

public static void main(String[] args) {

    Scanner keyboard = new Scanner(System.in);
    String check = null;
    System.out.println("Enter a string that you want to check (or enter ! to exit): ");
            input = keyboard.nextLine();
    do
    {       
            input = keyboard.nextLine();
             Stack<Character> stack = new Stack<Character>();
             for (int i = 0; i < input.length(); i++) {
                    stack.push(input.charAt(i));
             }

             while (!stack.isEmpty()) {
                    reverseInput += stack.pop();
             }
             isPalindrome(reverseInput);


        System.out.println("Enter a string that you want to check (or enter ! to exit): ");
            input = keyboard.nextLine();

    } while (input != "!");
}

public static boolean isPalindrome(String aString){
    if (input.equals(reverseInput))
        System.out.println("Yes! that is a palindrome.");
    else
        System.out.println("No! that isn't a palindrome.");
    return false;
}

}

0 个答案:

没有答案