我正在尝试制作一个简单的程序,该程序将接收一个包含 4 个单词的字符串,然后将该字符串拆分为 4 个单词,并打印这 4 个单词的所有可能排列
这是源代码,正则表达式在第 21 行,我不确定它是否正确。而且它真的不喜欢我的嵌套 for 循环
/**
* Author: peaceblaster
* Date 9/10/2013
* Title: hw3
* Purpose: To take in 4 words as a single string, then prints all permutations of the four words
*/
//import stuff
import java.util.Scanner;
public class hw3 {
public static void main(String[] args){
//declarations:
Scanner in = new Scanner(System.in);
String input = new String();
String[] wordArray = new String[4];
int a,b,c,d;
//input
System.out.println("Input 4 words: ");
input = in.next();
//regex
wordArray = input.split("^*[^\s]|\s*\s|*$"); //splits string into array containing each words as a string
// ^* finds first word \s*\s finds words surrounded by spaces *$ finds final word
//output
for (a=1; a=<4; a++){
for (b=1; b=<4; b++){
for (c=1; c=<4; c++){
for (d=1; d=<4; d++){
System.out.println(wordArray[%a] + " " + wordArray[%b] + " " + wordArray[%c] + " " + wordArray[%d]); //uses nested for loops to print permutations as opposed to hard-coding
}
}
}
}
}
}