Anagram检查java

时间:2016-05-19 19:22:07

标签: java sorting anagram

我编写了一个程序来检查两个数组的元素并说明它们是否是字谜。我的代码无法正常工作。你可以告诉我,在排序单个数组时,错误是否在for循环中?应该修复什么?我应该为两个数组的循环使用不同的计数名称吗?

/*/ Anagrams:

Example: two sets of numbers: {10, 40, 20, 30} and {20, 10, 30, 40} are anagrams to
         each other

/*/

import static java.lang.System.*;
import java.util.*;

class Anagram_Check{
    public static void main(String[] args){
        Scanner orcho = new Scanner(in);
        out.println("Please enter the amount of numbers for the first array: ");
        int quantity1 = orcho.nextInt();
        out.println("Please enter the amount of numbers for the second array: ");
        int quantity2 = orcho.nextInt();

        if(quantity1 != quantity2){
            out.println("No, the arrays will not be anagrams");
        }

        else{
           int[] myArray1 = new int[quantity1];

            out.println("Please enter the numbers of the first array: ");

            for(int count = 0; count < myArray1.length; count++){
                myArray1[count] = orcho.nextInt();
            }

            int icu;

            for(int count = 0; count < myArray1.length; count++){
                for(int check = 0; check < myArray1.length - 1; check++){
                    if(myArray1[check] > myArray1[check + 1]){
                        icu = myArray1[check]; 
                        myArray1[check] = myArray1[check + 1]; 
                        myArray1[check + 1] = icu; 
                    }
                }
            }

            int[] myArray2 = new int[quantity2];

            out.println("Please enter the numbers of the second array: ");

            for(int count = 0; count < myArray2.length; count++){
                myArray2[count] = orcho.nextInt();
            }

            for(int count = 0; count < myArray2.length; count++){
                for(int check = 0; check < myArray2.length - 1; check++){
                    icu = myArray2[check]; 
                    myArray2[check] = myArray2[check + 1]; 
                    myArray2[check + 1] = icu; 
                }
            }

            int d = 0;

            for(int count = 0; count < myArray1.length; count++){
                if(myArray1[count] == myArray2[count]){
                    d = 1;
                }

                else{
                    d = 5;
                }
            }

            if(d == 1){
                out.println("Yes, they are anagrams");
            }  

            else if (d ==5){
                out.println("No, they are not anagrams");
            }
        }

        orcho.close();
    }
}

3 个答案:

答案 0 :(得分:1)

我认为问题在于以下for循环:

    int d = 0;

    for(int count = 0; count < myArray1.length; count++){
        if(myArray1[count] == myArray2[count]){
            d = 1;
        }
        else{
            d = 5;
        }
    }

当您在稍后检查值时更改每次迭代的'd'值时,您实际上只检查两个数组的最后一个元素是否相等。一个简单的解决方法是添加

break;

在else语句结束之后,一旦你知道两个数组都不相等就没有必要继续检查了。

编辑 - 对d而不是int使用布尔值也会更好,因为它只作为两个可能的值

检查数组是否为字谜时的示例

boolean d = true;

for(int count=0; count < myArray1.length; count++){
    if (myArray[count]!=myArray2[count]){
        d=false;
        break;
    }
}

if (d) {
    System.out.println("Yes, they are anagrams");
}else{
    System.out.println("Yes, they are not anagrams");
}

答案 1 :(得分:0)

使用名为Bubble Sort的简单排序算法。它可用于对小列表进行排序。它也很有效率。通过你的清单,它将完成剩下的工作。

例如bubbleSort(myArray1);

public static void bubbleSort(int[] list) {

    boolean flag = true; //checks if all the values have been compared
                         //default is set to true only to enter the loop

    while (flag) {

        flag = false; //assume all values are compared

        for (int i = 0; i < (list.length - 1); i++) {

            if (list[i] < list[i + 1]) {

                int temp = list[i];
                list[i] = list[i + 1];
                list[i + 1] = temp;
                flag = true; //all values are not compared since
                             // were still able to do comparisons
            }
        }
    }
}

答案 2 :(得分:0)

import java.util.Arrays;
import java.util.Scanner;

//this program checks if the arrays are anagrams to each other; 
//done by Nadim Baraky

public class Anagram_Check {


  public static void main(String[] args) {

     Scanner sc = new Scanner(System.in);

     System.out.print("Please enter the amount of numbers of the first array: ");
     int quantity_1 = sc.nextInt();

     System.out.print("Please enter the amount of numbers of the second array: ");
     int quantity_2 = sc.nextInt();

     if(quantity_1 != quantity_2) {
        System.out.println("No , the arrays will not be anagrams");
     }

     else {

        int[] myArray1 = new int[quantity_1];
        System.out.print("Please enter the numbers of the first array: ");

        //filling the array with numbers
        for(int i = 0; i < myArray1.length; i++) {
            myArray1[i] = sc.nextInt();
        }
        Arrays.sort(myArray1); //this method sorts the array in increasing order 

        int[] myArray2 = new int[quantity_2];
        System.out.print("Please enter the numbers of the second array: ");

        for(int i = 0; i < myArray2.length; i++) {
                myArray2[i] = sc.nextInt();
        }
        sc.close();
        Arrays.sort(myArray2);

        if(anagram_checker(myArray1, myArray2)) {
            System.out.println("Yes, they are anagrams.");
        }
        else {
            System.out.println("No, they are not anagrams.");
        }
    }
}


 //this method returns ture if the two arrays are anagrams and false otherwise  
  public static boolean anagram_checker(int[] myArray1, int[] myArray2) {
      for(int i = 0; i < myArray1.length; i++) {
         //this loop goes over all the elements of the two arrays and checks if the elements are not equal
          if(myArray1[i] != myArray2[i]) {
             return false;
          }
      } 
          //reaching this point means that all elements are equal & it'll return true
             return true;
  }

}