即使我进入回文,也不会打印它是回文。有人可以告诉我有什么问题吗? [英] Even if I enter a palindrome, it does not print that it is a palindrome. Can someone please tell me what is wrong?

查看:91
本文介绍了即使我进入回文,也不会打印它是回文。有人可以告诉我有什么问题吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 / * 
*要更改此许可证标题,请在项目属性中选择许可证标题。
*要更改此模板文件,请选择工具|模板
*并在编辑器中打开模板。
* /
package programmingbasics1;
import java.util.Scanner;

/ **
*
* @author Vedha
* /
公共类Palindrome {
public static void main(String args [ ])
{
Scanner sc = new Scanner(System.in);
int arr [] = new int [5];
int arr_new [] = new int [5];
for(int i = 0; i< 5; i ++)
{
System.out.print(输入数组整数为+ i +:);
arr [i] = sc.nextInt();
}

for(int i = 0,j = 4; i< 5& j> = 0; i ++,j--)
{
arr_new [I] = ARR [J];
}
System.out.print(旧数组:);
for(int k = 0; k <5; k ++)
System.out.print(arr [k] +);
System.out.println();
System.out.print(New array:);
for(int k = 0; k <5; k ++)
System.out.print(arr_new [k] +);
System.out.println();

if(arr == arr_new)
System.out.print(它是回文);
else
System.out.print(它不是回文);
}

}





我尝试过:



我发布了我的代码,我无法在此找到错误。

解决方案

这个

 如果(arr == arr_new)



不是如何比较2个数组的包含。

仅当两个数组在内存中共享相同的地址时才这样。

你必须手动比较每个元素。


数组是Java中的对象,当你使用

 == 

时,你正在寻找指向不同名称的相同对象实例相同的内存位置。要比较两个对象(在本例中为数组)中所有元素的相等性,您必须使用

等于

。将此

 if(arr == arr_new)

更改为

 if(Arrays.equals(arr,arr_new))


/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package programmingbasics1;
import java.util.Scanner;

/**
 *
 * @author Vedha
 */
public class Palindrome {
    public static void main(String args[])
    {
        Scanner sc=new Scanner(System.in);
        int arr[]=new int[5];
        int arr_new[]=new int[5];
        for(int i=0;i<5;i++)
        {
            System.out.print("Enter array integer at "+i+": ");
            arr[i]=sc.nextInt();
        }
        
        for(int i=0,j=4;i<5&&j>=0;i++,j--)
        {
            arr_new[i]=arr[j];
        }
        System.out.print("Old array: ");
        for(int k=0;k<5;k++)
            System.out.print(arr[k]+" ");
        System.out.println();
        System.out.print("New array: ");
        for(int k=0;k<5;k++)
            System.out.print(arr_new[k]+" ");
        System.out.println();
        
        if(arr==arr_new)
           System.out.print(" It is a Palindrome");
        else
            System.out.print("It is not a Palindrome");
    }
    
}



What I have tried:

I have posted my code, I'm not able to find the error in this.

解决方案

This

if(arr==arr_new)


is not how to compare the contain of 2 arrays.
This is true only if both array share the same address in memory.
You must manually compare each element.


Arrays are object in Java, when you use

==

you are looking for the same object instance of different names that point to the same memory location. To compare the equality of all elements in two objects (arrays in this case), you have to use

equals

. Change this

if(arr==arr_new)

to

if(Arrays.equals(arr, arr_new))


这篇关于即使我进入回文,也不会打印它是回文。有人可以告诉我有什么问题吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆