【发布时间】:2014-09-25 21:52:41
【问题描述】:
每个人。我的 Java 编程课作业有问题,希望得到您的意见。
pA 方法的赋值是创建一个仅使用 String 和 Character 方法的方法。因此,我编写了以下方法,当我尝试调用方法 pA 时,它会自动返回一个 false 值。我在想我的 for 循环可能有问题,但我不确定,我想我会在这里问你要说什么。
提前感谢您的宝贵时间。
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in); //Creates Scanner object to take user input
String pal; //String to contain user input
boolean test = false; //boolean to test if user input is a palindrome.
System.out.println("This program determines if your input is a palindrome!");
System.out.println("Please enter your input to see if it's a palindrome.");
pal = keyboard.nextLine();
pA(pal, test);
if (test == true)
{
System.out.println("Congratulations! You have a palindrome!");
}
else if (test == false)
{
System.out.println("Unfortunately, you do not have a palindrome.");
}
}
public static boolean pA(String pal, boolean test) //Part A of Assignment 7.
{
int l = pal.length(); //integer to contain length of string
int i; //Loop control variable
test = false;
for (i = 0; i < l/2; i++) //for loop that tests to see if input is a palindrome.
{
if (pal.charAt(i) == pal.charAt(l-i-1)) //Compares character at point i with respective character at the other end of the string.
{
test = true; //Sets boolean variable to true if the if statement yields true.
} else
test = false; //Otherwise, a false value is returned.
break;
}
return test; //Return statement for boolean variable
} //End pA
从这里开始,当我尝试运行程序时,使用输入“tubbut”时收到以下消息:
运行:
此程序确定您的输入是否为回文!
请输入您的输入以查看是否为回文。
管子
很遗憾,你没有回文。
构建成功(总时间:2 秒)
【问题讨论】:
标签: java boolean palindrome