Java 置换发生器

Java 置换发生器,java,arrays,input,arraylist,java.util.scanner,Java,Arrays,Input,Arraylist,Java.util.scanner,我正试图将用户的输入添加到我的排列列表中,但当我接受用户输入时,程序只是继续运行。当我在第三次输入后按enter键时,我没有得到任何排列。这是我的密码: import java.util.ArrayList; import java.util.Scanner; /** This program demonstrates the permutation generator. */ public class PermutationGeneratorDemo { public s

我正试图将用户的输入添加到我的排列列表中,但当我接受用户输入时,程序只是继续运行。当我在第三次输入后按enter键时,我没有得到任何排列。这是我的密码:

  import java.util.ArrayList;
  import java.util.Scanner;

 /**
 This program demonstrates the permutation generator.
 */
 public class PermutationGeneratorDemo
 {
 public static void main(String[] args)
 {
   Scanner scan = new Scanner(System.in);

   System.out.println("Please enter a 4 letter word: ");
   String word1 = scan.next();
   System.out.println("Enter a 5 letter word: ");
   String word2 = scan.next();
   System.out.println("Enter a 6 letter word: ");
   String word3 = scan.next();


  PermutationGenerator generator = new PermutationGenerator(word1 + word2 + word3);
  ArrayList<String> permutations = generator.getPermutations();
  for (String s : permutations)
  {         
     System.out.println(s);
    }
   }
  }
import java.util.ArrayList;
导入java.util.Scanner;
/**
这个程序演示了置换生成器。
*/
公共类置换生成器emo
{
公共静态void main(字符串[]args)
{
扫描仪扫描=新扫描仪(System.in);
System.out.println(“请输入一个4个字母的单词:”);
String word1=scan.next();
System.out.println(“输入一个5个字母的单词:”);
String word2=scan.next();
System.out.println(“输入一个6个字母的单词:”);
String word3=scan.next();
置换生成器=新的置换生成器(word1+word2+word3);
ArrayList permutations=generator.getPermutations();
for(字符串s:排列)
{         
系统输出打印项次;
}
}
}
排列的代码:

import java.util.ArrayList;

/**
This class generates permutations of a word.
*/
 public class PermutationGenerator
 {
  private String word;

 /**
  Constructs a permutation generator.
  @param aWord the word to permute
 */
 public PermutationGenerator(String aWord)
{
    word = aWord;
}

PermutationGenerator(String[] wordList) {
    throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}

 /**
    Gets all permutations of a given word.
   */
 public ArrayList<String> getPermutations()
 {
  ArrayList<String> permutations = new ArrayList<String>();

  // The empty string has a single permutation: itself
  if (word.length() == 0) 
  { 
     permutations.add(word); 
     return permutations; 
  }

  // Loop through all character positions
  for (int i = 0; i < word.length(); i++)
  {
     // Form a simpler word by removing the ith character
     String shorterWord = word.substring(0, i)
           + word.substring(i + 1);

     // Generate all permutations of the simpler word
     PermutationGenerator shorterPermutationGenerator 
           = new PermutationGenerator(shorterWord);
     ArrayList<String> shorterWordPermutations 
           = shorterPermutationGenerator.getPermutations();

     // Add the removed character to the front of
     // each permutation of the simpler word, 
     for (String s : shorterWordPermutations)
     {
        permutations.add(word.charAt(i) + s);
     }
  }
  // Return all permutations
  return permutations;
 }
}
import java.util.ArrayList;
/**
此类生成单词的排列。
*/
公共类置换生成器
{
私有字符串字;
/**
构造一个置换生成器。
@param用词来排列
*/
公共置换生成器(字符串aWord)
{
单词=aWord;
}
置换生成器(字符串[]字列表){
抛出新的UnsupportedOperationException(“尚未受支持”);//若要更改生成的方法体,请选择“工具”“模板”。
}
/**
获取给定单词的所有排列。
*/
公共ArrayList getPermutations()
{
ArrayList置换=新的ArrayList();
//空字符串只有一个排列:它本身
if(word.length()==0)
{ 
排列。添加(单词);
返回置换;
}
//循环遍历所有角色位置
for(int i=0;i
扫描仪将始终等待输入。因此,您必须设置要读取的特定数量的值,或者必须使用特殊的字符/字符串标记输入的结束


我之前以为问题是无限递归,但经过进一步检查,您的程序确实正确终止了。但是,您必须认识到,生成排列列表是一种阶乘复杂性。4+5+6 = 15. 15! 是一个非常大的数字,1.3076744e+12 从谷歌计算器,这就是为什么你的程序似乎永远不会结束

生成这么多字符串需要一段时间


试着用输入运行程序,
a
b
c
,你会发现它可以工作,因为它只需要生成3!=6个字符串。

如果您包含实际生成排列的代码,这会有所帮助…@AmirAfghani是正确的,我想我应该添加:
Scanner#next
读取下一个
字符串
默认值,由
空格
分隔<代码>扫描器#下一行读取由
新行
字符分隔的下一行。排列的代码现在也在那里。我对Java相当陌生。当我将next()更改为nextLine()时,我仍然遇到同样的问题。您的算法不会生成置换。修改程序以接受1个单词并输入“aaaa”。看看你得到的东西……考虑使用番石榴的收藏2置换法,如果这不是一个学术活动,那是我最初想到的,但是,当他的代码进入他的<代码> GETPrime方法时,他的代码运行到一个无限循环。我没有投反对票,不知道是谁投的。很明显,一个用户对自己一无所知,无法在团队中讨论和解决问题;)