如何让我的代码输出成为按键?我知道我必须使用Robot类,但是如何为对象输出呢?
import java.util.HashSet;
import java.util.Set;
import java.awt.Robot;
import java.awt.event.KeyEvent;
public class MainClass {
public static Set<String> generatePowerSet(String inputString)
{
Set<String> result = new HashSet<String>();
result.add(""); // empty string is an element of the power set
if (inputString != null && !inputString.isEmpty())
{
int n = 1 << inputString.length(); //2^n - the number of elements in the power set
StringBuilder sb = new StringBuilder();
for (int i = 1; i < n; i++) // skip empty set, i = 0
{
int k = i; // current number to manipulate
sb.delete(0, sb.length()); // clear the StringBuilder
for (int index = 0; index < inputString.length() && k > 0; index++) // break early when k == 0
{
if ((k & 1) == 1) // include char at index if bit at that index is 1
{
sb.append(inputString.charAt(index));
}
k >>= 1;
}
result.add(sb.toString());
}
}
return result;
}
public static void permuteString(String bs, String end) {
if (end.length() <= 1)
System.out.println(bs + end);//I THINK I HAVE TO CHANGE SOMETHING OVER HERE
else
for (int i = 0; i < end.length(); i++) {
try {
String newString = end.substring(0, i) + end.substring(i + 1);
permuteString(bs + end.charAt(i), newString);
} catch (StringIndexOutOfBoundsException exception) {
exception.printStackTrace();
}
}
}
public static void main(String args[]) {
Set<String> powerSet = generatePowerSet("String");
Set<String> allPossibilities = new HashSet<String>();
for(String s : powerSet)
{
permuteString(" ", s );
}
}
}