-3
我如何使else语句输入是否我输入的数字是回文或不是?第一部分工作,我只是卡在else语句试图找出如何使其工作。我的继承人代码不断收到此代码错误
import java.util.*;
public class Lab6
{
public static void main (String [] args)
{
String pal1, pal2="";
int choice;
Scanner in = new Scanner(System.in);
System.out.println("Word(w) or Number(n)?");
choice = in.nextLine().charAt(0);
if (choice == 'w') {
System.out.println("Enter a word: ");
pal1= in.nextLine();
int length = pal1.length();
for (int i = length - 1 ; i >= 0 ; i--)
pal2 = pal2 + pal1.charAt(i);
if (pal1.equals(pal2))
System.out.println("The word you entered is a palindrome.");
else
System.out.println("The word you entered is not a palindrome.");
}
else{
System.out.println("Enter a bunch of numbers: ");
pal1 = in.nextLine();
pal1 = String.valueOf(in.nextInt());
int numLength = pal1.length();
for (int j = numLength - 1 ; j >= 0 ; j--)
pal2 = pal2 + pal1.charAt(j);
if (pal1.equals(pal2))
System.out.println("The numbers you entered is a palindrome.");
else
System.out.println("The numbers you entered is not a palindrome.");
}
}
}
你得到什么错误?什么不按预期工作? –
什么“第一部分”?哪个'else'语句? (其中有三个)。您需要更具体地了解您要解决的问题。 – Wyzard
第一个if else声明。而不是if else语句中的if else语句。 @Wyzard – programmingnoob