Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何使用循环手动对字符进行排序?_Java_Loops_Sorting_For Loop - Fatal编程技术网

Java 如何使用循环手动对字符进行排序?

Java 如何使用循环手动对字符进行排序?,java,loops,sorting,for-loop,Java,Loops,Sorting,For Loop,我需要检查两个字符串是否是字谜,但我不能使用数组。排序。。。我知道我必须使用for循环,但我不知道如何开始,以便按字母顺序对字符进行排序。请帮帮我 import java.util.Scanner; public class Assignement3{ public static void main (String[] args) { Scanner sc=new Scanner(System.in); System.out.println( "Please, type the

我需要检查两个字符串是否是字谜,但我不能使用数组。排序。。。我知道我必须使用for循环,但我不知道如何开始,以便按字母顺序对字符进行排序。请帮帮我

import java.util.Scanner;
public class Assignement3{
public static void main (String[] args) {
    Scanner sc=new Scanner(System.in);
    System.out.println( "Please, type the first word: ");
    String word1=sc.nextLine();
    System.out.println( "Please, type the second word: ");
    String word2=sc.nextLine();
    String word1lower=word1.toLowerCase().replace(" ","");
    String word2lower=word2.toLowerCase().replace(" ","");
    System.out.println("Your First word is: " + word1lower);
    System.out.println("Your Second word is: " + word2lower);
    char[] firstword=word1lower.toCharArray();
    char[] secondword=word2lower.toCharArray();
  }
  }`

我认为,提供确定两个字符串是有意义的还是彼此的实际字谜的代码需要在字典中进行查找。但是,如果我们将一个单词的一个拼字法定义为原始单词中现有字符的某种排列,那么我们可以相当容易地检查这一点

在下面的代码片段中,我将第一个单词中的字符读入一个映射,记录每个字母出现的次数。这张字符地图代表了所有可以用来形成潜在字谜的东西。通过迭代第二个单词并跟踪所使用的每个字符,我们可以判断第二个单词是否是一个字谜。失败的标记是尝试使用未出现或已用尽的字符。否则,第二个单词就是一个潜在的字谜

String word1lower = "hala babel";
String word2lower = "baha label";
char[] firstword = word1lower.toCharArray();
char[] secondword = word2lower.toCharArray();

Map<Character, Integer> m1 = new HashMap<>();
int count = 0;
for (char c : firstword) {
    Integer cnt = m1.get(c);
    m1.put(c, cnt == null ? 1 : cnt.intValue() + 1);
    ++count;
}
boolean isAnagram = true;
for (char c : secondword) {
    Integer cnt = m1.get(c);
    if (cnt == null || cnt.intValue() == 0) {
        isAnagram = false;
        break;
    }
    m1.put(c, cnt.intValue() - 1);
    --count;
}

if (isAnagram && count == 0) {
    System.out.println("Second word is a full anagram of the first word.");
}
else if (isAnagram) {
    System.out.println("Second word is a partial anagram of the first word.");
}
else {
    System.out.println("Second word is not an anagram of the first word.");
}
stringword1lower=“哈拉巴贝尔”;
字符串word2lower=“巴哈标签”;
char[]firstword=word1lower.toCharArray();
char[]secondword=word2lower.toCharArray();
Map m1=新的HashMap();
整数计数=0;
for(字符c:第一个字){
整数cnt=m1.get(c);
m1.put(c,cnt==null?1:cnt.intValue()+1);
++计数;
}
布尔值isAnagram=true;
for(字符c:第二个字){
整数cnt=m1.get(c);
如果(cnt==null | | cnt.intValue()==0){
isAnagram=假;
打破
}
m1.put(c,cnt.intValue()-1);
--计数;
}
if(isAnagram&&count==0){
System.out.println(“第二个单词是第一个单词的完整字谜”);
}
else if(伊萨纳格拉姆){
System.out.println(“第二个单词是第一个单词的部分字谜”);
}
否则{
System.out.println(“第二个单词不是第一个单词的字谜”);
}
再次,我在这里说潜在的字谜,因为要检查字符的随机组合是否对应于实际的英语(或其他语言)单词,需要一本字典。这已经超出了单堆栈溢出问题的范围,但希望我的回答能让您朝着正确的方向思考

此处演示:


。因此,最简单的方法可能是创建自己的排序方法,然后在每个元素长度相同的情况下,对每个元素是否彼此相等进行双循环检查。您可能会发现Ascii表在这种情况下很有用。在这种情况下,我需要有人解释我或编写一个for循环,以便按字母顺序对charArray进行排序。这不起作用:输入如何
“hello”
?这会说这是一个字谜;而且它是非对称的,因为
“hello”
不会。@AndyTurner我做了三行更改,检查第二个单词是否使用了第一个单词中的每个字符。我想这里的问题是OP想要如何处理一个字谜。或者我最初对什么是字谜的假设是错误的。