Methods Else无If错误(回文程序)
该程序旨在查找用户输入是否为回文。然而,在isAlindrome方法中,我不断得到声明的错误。当我修复它时,我得到24个错误,说它不能定位多个其他变量。请帮忙 代码如下:Methods Else无If错误(回文程序),methods,palindrome,Methods,Palindrome,该程序旨在查找用户输入是否为回文。然而,在isAlindrome方法中,我不断得到声明的错误。当我修复它时,我得到24个错误,说它不能定位多个其他变量。请帮忙 代码如下: import java.util.Scanner; public class Project4 { public static void main (String [] args) { String line = getInputLine(); while (!isEmptyLine
import java.util.Scanner;
public class Project4
{
public static void main (String [] args) {
String line = getInputLine();
while (!isEmptyLine (line)) {
if (isPalindrome (line))
System.out.println ("\"" + line +
"\" is a palindrome and a " + getPalType (line));
else
System.out.println ("\"" + line +
"\" is not a palindrome");
line = getInputLine();
}
System.out.println ("End of prgram");
}
public static String getInputLine () //Ask user for input and then returns the line
{
Scanner scan = new Scanner(System.in);
System.out.print("Enter your possible Palindrome: ");
String getInputLine = scan.nextLine();
return getInputLine;
}
public static boolean isEmptyLine (String str) // Return TRUE if the paramater is empty or False otherwise
{
if (line != null)
{
isEmptyLine = true;
}
else
{
isEmptyLine = false;
}
}
public static boolean isPalindrome (String str)// Return TRUE if the str is a palindrome or FALSE otherwise
{
int left = 0;
int right = line.length() - 1;
boolean okay = true;
while (okay && left < right){
char ch1 = line.charAt(left);
if (!(Character.isDigit(ch1) || Character.isLetter(ch1))){
left ++;
else{
ch2 = line.charAt(right);
}
if (!(Character.isDigit(ch2) || Character.isLetter(ch2))){
right --;
else{
ch1 = Character.toUpperCase(ch1);
ch2 = Character.toUpperCase(ch2);
}
if (ch1 == ch2){
left ++;
right --;
else{
okay = false;
}
}
}
}
}
return okay;
}
public static String getPalType (String str)//Determine the type of the palindrome and return either word/phrase/number.
{
int num = 0;
int let = 0;
int length = line.length();
for (int i = 0; i <= length; i++)
{
if (Character.isDigit(line.charAt(i)))
num++;
else if (Character.isLetter(line.charAt(i)))
let++;
}
if (num > 0 && let == 0)
{
getPalType = number;
return getPalType;
}
else if (let > 0 && num == 0)
{
getPalType = word;
return getPalType;
}
else
getPalType = phrase;
return getPalType;
}
}
import java.util.Scanner;
公共类项目4
{
公共静态void main(字符串[]args){
字符串行=getInputLine();
而(!isEmptyLine(line)){
if(isPalindrome(线路))
System.out.println(“\”+行+
“\”是回文,是“+getPalType(line));
其他的
System.out.println(“\”+行+
“\”不是回文“);
line=getInputLine();
}
System.out.println(“程序结束”);
}
公共静态字符串getInputLine()//请求用户输入,然后返回该行
{
扫描仪扫描=新扫描仪(System.in);
System.out.print(“输入可能的回文:”);
字符串getInputLine=scan.nextLine();
返回getInputLine;
}
public static boolean isEmptyLine(String str)//如果参数为空则返回TRUE,否则返回False
{
如果(行!=null)
{
isEmptyLine=真;
}
其他的
{
isEmptyLine=false;
}
}
公共静态布尔值isAlindrome(String str)//如果str是回文,则返回TRUE,否则返回FALSE
{
int左=0;
int right=line.length()-1;
布尔值OK=true;
while(OK&&左<右){
char ch1=line.charAt(左);
if(!(Character.isDigit(ch1)| | Character.islitter(ch1))){
左++;
否则{
ch2=行字符(右);
}
if(!(Character.isDigit(ch2)| | Character.islitter(ch2))){
对--;
否则{
ch1=字符.toUpperCase(ch1);
ch2=字符。toUpperCase(ch2);
}
如果(ch1==ch2){
左++;
对--;
否则{
好=假;
}
}
}
}
}
返回OK;
}
public static String getPalType(String str)//确定回文的类型并返回单词/短语/数字。
{
int num=0;
int let=0;
int length=line.length();
对于(int i=0;i 0&&let==0)
{
getPalType=编号;
返回getPalType;
}
else if(设>0&&num==0)
{
getPalType=word;
返回getPalType;
}
其他的
getPalType=短语;
返回getPalType;
}
}
缺少最上面的起始括号
if (isPalindrome (line))
如果有一种或另一种线条样式,则不能使用“一行闭合”样式。只需添加开始括号
需要这样做:
while (!isEmptyLine (line)) {
if (isPalindrome (line)){
System.out.println ("\"" + line +
"\" is a palindrome and a " + getPalType (line));
}else{
System.out.println ("\"" + line +
"\" is not a palindrome");
line = getInputLine();
} //since you have two lines not one in the else.
}
System.out.println ("End of prgram");
您混淆了括号-例如
if(!(Character.isDigit(ch2)| | Character.isliter(ch2)){right--;否则{
-您没有关闭if
语句。