【问题标题】:Why are my code not working ?.This a code to check whether a number is a palindrome or not for positive number [duplicate]为什么我的代码不起作用?。这是一个检查数字是否为正数的回文的代码[重复]
【发布时间】:2021-10-30 01:28:11
【问题描述】:

导入 java.util.Scanner; 公共类 CheckPalindrome3 {

public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    System.out.println("Enter your number :");
    String num = input.nextLine();
    String arr = "";
    for(int i = num.length() - 1 ; i >= 0;i--)
    {
    
        arr = arr + num.charAt(i);
    }
System.out.println(arr+"\n"+num);
    if(arr == num) {
        System.out.println("Yes that's a Palindrome !!");
    }
    else
    {
        System.out.println("No that's not a Palindrome !!");
    }

}

}

当我输入 121 时,它打印出来了 不,那不是回文,但 arr 与 num 相同

【问题讨论】:

    标签: java palindrome


    【解决方案1】:

    问题是测试arr == num。对于字符串,== 测试两个字符串在内存中是否具有相同的地址(这通常不正确,即使字符串看起来相同)。请改用arr.equals(num)。这将测试arr 中的所有字符是否与num 中的字符相同。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-09-30
      • 1970-01-01
      • 2013-02-15
      • 2013-01-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-19
      相关资源
      最近更新 更多