用空白java翻转

时间:2015-10-17 12:01:04

标签: java

如果不打印"same",我想取一个字符串然后翻转字符串,如果它们是相同的打印"not same"。这些代码对我来说很有用,但如果我拿一个像"r a d a r"这样的空白的字符串,那个程序会问"continue?",但它没有等我的答案..直接退出..

import java.util.Scanner;

public class sormak {

        public static void main(String[]args) {

            Scanner input = new Scanner(System.in);;
            String yes = "yes";
            String cont = ""; 

            do {
                String reserve = "";
                String input1 = "";

                System.out.print("give me string");
                input1 = input.next();

                for(int i=input1.length(); i>0; i--)
                {
                    reserve = reserve + input1.substring(i-1,i);
                }
                if(input1.equalsIgnoreCase(reserve))
                    System.out.println("same!!");
                else
                    System.out.println("not same");

                System.out.print("continue?");
                cont = input.next();

            } while (cont.equalsIgnoreCase(yes) == true);

        }
    }

1 个答案:

答案 0 :(得分:0)

尝试以下代码,您需要阅读整行以获得答案。

System.out.print("continue?");
while (!cont.equals("\n")) {
   cont = input.next();
   answer += cont;
}
} while (answer.equalsIgnoreCase(yes));