作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
首先,我并不是要告诉任何人“做我的功课”。我只需要一些关于如何不断重复一个过程的帮助。这是我在下面做的程序,它有一个测试类。
类(class):
class RecursivePalindrome {
public static boolean isPal(String s)
{
if(s.length() == 0 || s.length() == 1)
return true;
if(s.charAt(0) == s.charAt(s.length()-1))
return isPal(s.substring(1, s.length()-1));
return false;
}
}
然后是具有主要方法的测试类:
public class RecursivePalindromeTester {
public static void main(String[] args)
{
RecursivePalindrome Pal = new RecursivePalindrome ();
boolean quit = true;
Scanner in = new Scanner(System.in);
System.out.print("Enter a word to test whether it is a palindrome or not(press quit to end.): ");
String x = in.nextLine();
while(quit) {
boolean itsPal = Pal.isPal(x);
if(itsPal == true){
System.out.println(x + " is a palindrome.");
quit = false;
}
else if (x.equals("quit")) {
quit = false;
}
else {
quit = false;
System.out.println(x + " is not a palindrome.");
}
}
}
}
这个程序判断字母是否为回文。我得到了所有的计算和东西,但我该怎么做才能不断询问用户输入,每次用户输入时它都会说它是否是回文词。
最佳答案
只需移动要求用户输入并阅读的行:
System.out.print("Enter a word to test whether it is a palindrome or not(press quit to end.): ");
String x = in.nextLine();
...进入你的循环,例如,就在
之后while (quit) {
...行。
旁注:quit
似乎是一个 boolean 值的奇怪名称,当 true
时,意味着您继续。 :-)
关于java - 递归回文,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14799784/
我想开发一个 Skype 机器人,它将用户名作为输入,并根据用户输入以相反的字符大小写表示hello username。简而言之,如果用户输入他的名字 james,我的机器人会回复他为 Hello J
我是一名优秀的程序员,十分优秀!