我正在try 让Java将"a"和"zzzzzz"之间的每个字母序列输出到鼠标光标的位置.我已经让它成功地与int一起工作,但是Java不能很容易地转换成字符串或字符.

import java.awt.AWTException;
import java.awt.event.InputEvent;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import java.awt.event.KeyEvent;
import java.util.concurrent.TimeUnit;

public class robot {
    static int i;

    public static void main(String[] args) throws AWTException, InterruptedException {
        // TODO Auto-generated method stub
        Thread.sleep(500);
        for (int i=0; i< 30; i+=1) {
            String str = " " + i + " ";
            Robot r = new Robot();
            Thread.sleep(200);
            String text = str;
            StringSelection stringSelection = new StringSelection(text);
            Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
            clipboard.setContents(stringSelection, stringSelection);
            Thread.sleep(200);
            Robot robot = new Robot();
            robot.keyPress(KeyEvent.VK_CONTROL);
            robot.keyPress(KeyEvent.VK_C);
            robot.keyRelease(KeyEvent.VK_CONTROL);
            robot.keyRelease(KeyEvent.VK_C);
            robot.keyPress(KeyEvent.VK_CONTROL);
            robot.keyPress(KeyEvent.VK_V);
            robot.keyRelease(KeyEvent.VK_CONTROL);
            robot.keyRelease(KeyEvent.VK_V);
        }
    }
}

有没有可能很容易地将

for (int i=0; i< 30; i+=1) {
    String str = " " + i + " ";

改为使用字符串序列?我试了一段时间,但什么都没有奏效.Even==不允许编译它.

推荐答案

'... I'm trying to get Java to output every sequence of letters between "a" and "zzzzzz" ...'

Use a for-loop to iterate from a to z.
Here is an example for a through zzz, which generates 18,278 values.

List<String> list = new ArrayList<>();
for (char c1 = 'a'; c1 <= 'z'; c1++) {
    list.add(String.valueOf(c1));
    for (char c2 = 'a'; c2 <= 'z'; c2++) {
        list.add("%s%s".formatted(c1, c2));
        for (char c3 = 'a'; c3 <= 'z'; c3++)
            list.add("%s%s%s".formatted(c1, c2, c3));
    }
}
list.sort(Comparator.comparing(String::length));

"... I've gotten this to work successfully with int, but Java doesn't convert easily to String or char. ..."

转换很简单,下面是如何将int转换为char.

int a = 97;
char c = (char) a;

以及如何将int转换为char,然后再转换为String.

int b = 98;
String s = String.valueOf((char) b);

"... Is it possible to easily convert ..."

在生成列表之后,您可以只迭代每一项.

public static void main2(String[] args) throws AWTException, InterruptedException{
    // TODO Auto-generated method stub
    Thread.sleep(500);
    List<String> list = new ArrayList<>();
    for (char c1 = 'a'; c1 <= 'z'; c1++) {
        list.add(String.valueOf(c1));
        for (char c2 = 'a'; c2 <= 'z'; c2++) {
            list.add("%s%s".formatted(c1, c2));
            for (char c3 = 'a'; c3 <= 'z'; c3++)
                list.add("%s%s%s".formatted(c1, c2, c3));
        }
    }
    list.sort(Comparator.comparing(String::length));
    for (String str : list) {
        Robot r = new Robot();
        Thread.sleep(200);
        String text = str;
        StringSelection stringSelection = new StringSelection(text);
        Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
        clipboard.setContents(stringSelection, stringSelection);
        Thread.sleep(200);
        Robot robot = new Robot();
        robot.keyPress(KeyEvent.VK_CONTROL);
        robot.keyPress(KeyEvent.VK_C);
        robot.keyRelease(KeyEvent.VK_CONTROL);
        robot.keyRelease(KeyEvent.VK_C);
        robot.keyPress(KeyEvent.VK_CONTROL);
        robot.keyPress(KeyEvent.VK_V);
        robot.keyRelease(KeyEvent.VK_CONTROL);
        robot.keyRelease(KeyEvent.VK_V);

    }
}

"... Is there a way to shift the window of values? For example, if I wanted to skip the single letters and go straight to triple letters ..."

是的,正如@OldDogProgrammer提到的,您可以删除前两个list.add呼叫.

List<String> list = new ArrayList<>();
for (char c1 = 'a'; c1 <= 'z'; c1++)
    for (char c2 = 'a'; c2 <= 'z'; c2++)
        for (char c3 = 'a'; c3 <= 'z'; c3++)
            list.add("%s%s%s".formatted(c1, c2, c3));
list.sort(Comparator.comparing(String::length));

"... or if I wanted to focus it in on a smaller part of the list?"

您可以使用100方法来仅捕获列表的一部分.

list = list.subList(list.indexOf("zz") + 1, list.size());

Java相关问答推荐

Gmail Javi API批量处理太多请求

Saxon 9:如何从Java扩展函数中的net.sf.saxon.expr. XPathContent中获取声明的变量

try 使用Java 9或更高版本对特殊对象图进行解析时出现NullPointerException

方法没有用正确的值填充数组—而是将数组保留为null,'

同时运行JUnit测试和Selenium/Cucumber测试时出现问题

@ IdClass with @ Inheritance(策略= InheritanceType. SINGLE_TABLE)

试着做一个2x2的魔方求解算法,我如何找到解路径(DFS)?

有没有办法让扩展变得多态?

在Spring Boot应用程序中导致";MediaTypeNotSupportdException&qot;的映像上载

如何让JVM在SIGSEGV崩溃后快速退出?

Spring Boot&;Docker:无法执行目标org.springframework.boot:spring-boot-maven-plugin:3.2.0:build-image

如何用内置Java从JavaFX应用程序中生成.exe文件?

在使用具有不同成本的谓词调用allMatch之前对Java流进行排序会带来什么好处吗?

如何在不作为类出现的表上执行原生查询?

为了安全起见,有必要复制一份 list 吗?

在整数列表中查找和可被第三个整数整除的对时出现无法解释的RunTimeError

HBox内部的左对齐按钮(如果重要的话,在页码内)

ExecutorService:如果我向Executor提交了太多任务,会发生什么?

Vaadin Flow:设置密码显示按钮属性

一条Java记录可以保存多少个字段?