Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/320.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_String_Methods - Fatal编程技术网

Java 合并两种方法

Java 合并两种方法,java,string,methods,Java,String,Methods,我有一段代码,可以按字母顺序排列三个单词。我试图删除底部的方法,并简单地将其添加到上面的代码中。然而,我想不出一个可行的方法来做到这一点。任何指导或帮助都将不胜感激,谢谢 System.out.print("Please enter three words: "); final String words = keyboard.nextLine(); final String[] parts = words.split(" "); if (parts[0].equ

我有一段代码,可以按字母顺序排列三个单词。我试图删除底部的方法,并简单地将其添加到上面的代码中。然而,我想不出一个可行的方法来做到这一点。任何指导或帮助都将不胜感激,谢谢

    System.out.print("Please enter three words: ");
    final String words = keyboard.nextLine();
    final String[] parts = words.split(" ");

    if (parts[0].equals(parts[1]) && parts[1].equals(parts[2])) {
    System.out.println("All three of those words are the same!");

 } else  {

         System.out.print("In alphabetical order those are: ");
         alphabetizing (parts);
         final int three = 3;
         for (int limit = 0;  limit < three;  limit++) {
         System.out.print(parts[limit] + " ");
         }
  }
  }

  public static void alphabetizing (final String[]  parts) {
        int word;
        boolean check = true;
        String temp;

              for (word = 0;  word < parts.length - 1;  word++) {
                      if (parts[ word ].compareToIgnoreCase(parts[word + 1]) > 0) {
                                  temp = parts[word];
                                  parts[word] = parts[word + 1];
                                  parts[word + 1] = temp;
                                  check = true;

                      }
              }
        }
  }
System.out.print(“请输入三个字:”);
最后的字符串字=keyboard.nextLine();
最后一个字符串[]部分=words.split(“”);
如果(部分[0].equals(部分[1])&部分[1].equals(部分[2])){
System.out.println(“这三个词都是一样的!”);
}否则{
System.out.print(“按字母顺序排列为:”);
按字母顺序排列(部分);
最终int 3=3;
对于(int limit=0;limit<3;limit++){
系统输出打印(部件[限制]+“”);
}
}
}
公共静态无效字母排序(最终字符串[]部分){
int字;
布尔检查=真;
字符串温度;
for(word=0;word0){
温度=零件[字];
部分[字]=部分[字+1];
零件[字+1]=温度;
检查=正确;
}
}
}
}

因为您无论如何都想这样做。我看不出它为什么不起作用有什么问题。只需将它添加到调用该方法的else语句中,它就可以工作了

    System.out.print("Please enter three words: ");
    Scanner keyboard = new Scanner(System.in);
    final String words = keyboard.nextLine();
    final String[] parts = words.split(" ");

    if (parts[0].equals(parts[1]) && parts[1].equals(parts[2])) {
        System.out.println("All three of those words are the same!");

    } else {

        System.out.print("In alphabetical order those are: ");

        //boolean check = true; you don't neeed this.
        String temp = "";

        for (int word = 0; word < parts.length - 1; word++) {
            if (parts[word].compareToIgnoreCase(parts[word + 1]) > 0) {
                temp = parts[word];
                parts[word] = parts[word + 1];
                parts[word + 1] = temp;

            }
        }
        final int three = 3;
        for (int limit = 0; limit < three; limit++) {
            System.out.print(parts[limit] + " ");
        }

    }

为什么要这样做?应该将代码提取到更多的方法中,而不是相反的方法。@ScaryWombat这是用于赋值的。我被要求删除该方法。我同意@Tom所说的。谷歌的一个功能应该做一件事是的,在现实生活中,这个特定的功能应该保持独立。然而,有时将功能连接在一起是有意义的,Shibaku的任务在这些时候是实践的。
Please enter three words: a b c
In alphabetical order those are: a b c 
Please enter three words: a a a
All three of those words are the same!