-2
下面是一個迴文程序。如果它是迴文,它將打印YES否則NO。 我無法理解有什麼區別呼籲:掃描儀中的錯誤
> int n=Integer.parseInt(in.nextLine());
> or int n=in.nextInt();
因爲無論是做同樣的工作。 第一個是將String
作爲輸入,然後將其轉換爲int
第二個是直接取int
。
當第一個被採取時沒有錯誤。 但是當第二個存儲在n中時,它會打印YES(調試時我發現它存儲在n中,但是它跳過了輸入字符串str,然後它與str和s進行比較並打印YES)。
所以任何人都可以解釋這背後的邏輯。
public class Test1 {
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
//int n=Integer.parseInt(in.nextLine());
int n=in.nextInt();
while(n!=0){
String s="";
String str=in.nextLine();
for(int i=str.length()-1;i>=0;i--){
s=s+str.charAt(i);
}
if(str.equals(s)){
System.out.println("YES");
}
else{
System.out.println("NO");
}
n--;
}
}
}