Java,检查两个字符数组是否相等
我在Java中有两个字符数组:Java,检查两个字符数组是否相等,java,Java,我在Java中有两个字符数组: 原始数组和混合数组。我需要检查它们是否不相等 以下是我到目前为止的情况: sample data orig_team=one mix_team=neo while(!Arrays.equals(mix_team, orig_team)) { if (Arrays.equals(mix_team, orig_team)) { System.out.println("congradulations! you did it");
原始数组
和混合数组
。我需要检查它们是否不相等
以下是我到目前为止的情况:
sample data
orig_team=one
mix_team=neo
while(!Arrays.equals(mix_team, orig_team))
{
if (Arrays.equals(mix_team, orig_team))
{
System.out.println("congradulations! you did it");
System.exit(0);
}
else {
System.out.println("enter the index");
Scanner scn = new Scanner(System.in);
int x = scn.nextInt();
int y = scn.nextInt();
char first=mix_team[x];
char second=mix_team[y];
mix_team[x]=second;
mix_team[y]=first;
for (int i = 0; i < mix_team.length; i = i + 1)
{
System.out.print(i);
System.out.print(" ");
}
System.out.println();
System.out.println(mix_team);
}
}
示例数据
原始团队=一个
混合团队=近地天体
而(!array.equals(混合团队、原始团队))
{
if(Arrays.equals(混合团队、原始团队))
{
System.out.println(“恭喜!你做到了”);
系统出口(0);
}
否则{
System.out.println(“输入索引”);
扫描仪scn=新扫描仪(System.in);
int x=scn.nextInt();
int y=scn.nextInt();
char first=mix_团队[x];
char second=mix_团队[y];
mix_团队[x]=第二;
混合团队[y]=第一;
对于(int i=0;i
如何确定两个数组是否相等?while循环的块仅在两个数组不相等时执行,因此使用相同的相等检查启动该块没有意义。换句话说,行:
if (Arrays.equals(mix_team, orig_team))
…将始终为
false
而循环的块仅在两个数组不相等时执行,因此使用相同的相等检查启动该块没有意义。换句话说,行:
if (Arrays.equals(mix_team, orig_team))
…将始终为false
您基本上有以下循环:
while (something) {
if (! something) {
code();
}
}
while
循环中的代码只有在某物
计算结果为true
时才会运行。因此,的值!某些内容
将始终为false,并且不会运行if
语句的内容
相反,请尝试:
while (!Arrays.equals (mix_team, orig_team)) {
System.out.println("enter the index");
Scanner scn = new Scanner(System.in);
int x = scn.nextInt();
int y = scn.nextInt();
char first=mix_team[x];
char second=mix_team[y];
mix_team[x]=second;
mix_team[y]=first;
for (int i = 0; i < mix_team.length; i = i + 1)
{
System.out.print(i);
System.out.print(" ");
}
System.out.println();
System.out.println(mix_team);
}
System.out.println("congratulations! you did it");
System.exit(0);
while(!Arrays.equals(混合团队、原始团队)){
System.out.println(“输入索引”);
扫描仪scn=新扫描仪(System.in);
int x=scn.nextInt();
int y=scn.nextInt();
char first=mix_团队[x];
char second=mix_团队[y];
mix_团队[x]=第二;
混合团队[y]=第一;
对于(int i=0;i
顺便说一下,您不需要每次都创建扫描仪。更好的方法是在while
循环之前声明扫描器(基本上将初始化行上移两行)。基本上有以下循环:
while (something) {
if (! something) {
code();
}
}
while
循环中的代码只有在某物
计算结果为true
时才会运行。因此,的值!某些内容
将始终为false,并且不会运行if
语句的内容
相反,请尝试:
while (!Arrays.equals (mix_team, orig_team)) {
System.out.println("enter the index");
Scanner scn = new Scanner(System.in);
int x = scn.nextInt();
int y = scn.nextInt();
char first=mix_team[x];
char second=mix_team[y];
mix_team[x]=second;
mix_team[y]=first;
for (int i = 0; i < mix_team.length; i = i + 1)
{
System.out.print(i);
System.out.print(" ");
}
System.out.println();
System.out.println(mix_team);
}
System.out.println("congratulations! you did it");
System.exit(0);
while(!Arrays.equals(混合团队、原始团队)){
System.out.println(“输入索引”);
扫描仪scn=新扫描仪(System.in);
int x=scn.nextInt();
int y=scn.nextInt();
char first=mix_团队[x];
char second=mix_团队[y];
mix_团队[x]=第二;
混合团队[y]=第一;
对于(int i=0;i
顺便说一下,您不需要每次都创建扫描仪。一个更好的方法是在循环之前声明扫描仪(基本上将初始化行向上移动两行)。您能为orig_团队和mix_团队提供一些示例输入吗?@Jeremy,我已经将示例数据放入了。mix_team基本上是orig_team.Bah的洗牌版;如果if
条件存在,则不需要else
。您可以为原始团队和混合团队提供一些样本输入吗?@Jeremy,我已经输入了样本数据。mix_team基本上是orig_team.Bah的洗牌版;如果if
条件存在,则无需执行else
。