简体   繁体   English

我正在尝试检查字谜

[英]I am trying to check for Anagram

Why the code is showing me the error for missing return statement?为什么代码向我显示缺少 return 语句的错误?

What I am trying to do is check the length of the string followed by its content and compare both of them.我想要做的是检查字符串的长度,然后是其内容并比较它们。

import java.util.Scanner;

public class Solution {
    static boolean isAnagram(String a, String b) {
        char[] arr1 = a.toLowerCase().toCharArray();
        char[] arr2 = b.toLowerCase().toCharArray();
        java.util.Arrays.sort(arr1);
        java.util.Arrays.sort(arr2);
        if (arr1.length != arr2.length) {
            if (!arr1.equals(arr2)) {
                System.out.println("Not Anagrams");
            }
            return false;
        } else if (arr1.equals(arr2)) {
            System.out.println("Anagrams");
            return true;
        }
    }

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        String a = scan.next();
        String b = scan.next();
        scan.close();
        boolean ret = isAnagram(a, b);
        System.out.println((ret) ? "Anagrams" : "Not Anagrams");
    }
}

PRIMARY QUESTION主要问题

You need to have a "return" outside if statements.您需要在 if 语句之外有一个“返回”。

else if(arr1.equals(arr2))
        {
             System.out.println("Anagrams");
             return true;
        }  

return false; //add this here. I tested, error goes after this.
}

Tested here - https://onlinegdb.com/S188-FdGL在这里测试 - https://onlinegdb.com/S188-FdGL

EXTRA额外的

Your Anagram logic is wrong.你的字谜逻辑是错误的。

The correct logic should be like this (follow comments to understand).正确的逻辑应该是这样的(按照评论来理解)。

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

 public class Solution {

 static boolean isAnagram(String a, String b) 
    {
        char[] arr1 = a.toLowerCase().toCharArray();
        char[] arr2 = b.toLowerCase().toCharArray();

        int n1 = arr1.length; 
        int n2 = arr2.length; 

        // If length is different, then they cannot be Anagram 
        if (n1 != n2) return false; 

        // Sort the Character Arrays 
        Arrays.sort(arr1); 
        Arrays.sort(arr2); 

        // Compare each characters of sorted Character Arrays,
        // if any character mismatches, it is not an Anagram
        for (int i = 0; i < n1; i++) 
            if (arr1[i] != arr2[i]) 
                return false; 

        // If all characters of both character Arrays are same,
        // only then, the String are Anagrams, so return true
        return true;

        // Also, remove the print statements from this method,
        // as you are already printing in the main method,
        // so it creates duplicate prints
    }


    public static void main(String[] args) {

        Scanner scan = new Scanner(System.in);
        String a = scan.next();
        String b = scan.next();
        scan.close();

        boolean ret = isAnagram(a, b);
        System.out.println( (ret) ? "Anagrams" : "Not Anagrams" );
    }
}

Tested here - https://onlinegdb.com/rJe-OEtuGL在这里测试 - https://onlinegdb.com/rJe-OEtuGL

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM