gpt4 book ai didi

Java永无止境的切换循环(有时)eclipse

转载 作者:搜寻专家 更新时间:2023-11-01 03:54:32 27 4
gpt4 key购买 nike

我对这个完全不解,我在循环中有一个 switch 语句:

package com.example.project

import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class Keyserver {
static ServerSocket server;
static Socket soc;
static ObjectInputStream in;
static int key;
static Robot bot;
static boolean shift = false;

public static void main(String[] args) throws AWTException{
bot = new Robot();
while(true){
try {
server = new ServerSocket(4321, 10);
soc = server.accept();
System.out.println("Accepted port");
in = new ObjectInputStream(soc.getInputStream());
} catch (IOException e) {
e.printStackTrace();
}
while(soc.isConnected()){
try {
key = in.readInt();
System.out.println(key);
switch(key){
case(7):
bot.keyPress(KeyEvent.VK_0);
bot.keyRelease(KeyEvent.VK_0);
break;
case(8):
bot.keyPress(KeyEvent.VK_1);
bot.keyRelease(KeyEvent.VK_1);
break;
case(9):
bot.keyPress(KeyEvent.VK_2);
bot.keyRelease(KeyEvent.VK_2);
break;
case(10):
bot.keyPress(KeyEvent.VK_3);
bot.keyRelease(KeyEvent.VK_3);
break;
....
case(53):
bot.keyPress(KeyEvent.VK_Y);
bot.keyRelease(KeyEvent.VK_Y);
break;
case(54):
bot.keyPress(KeyEvent.VK_Z);
bot.keyRelease(KeyEvent.VK_Z);
break;
case(4):
bot.keyPress(KeyEvent.VK_BACK_SPACE);
bot.keyRelease(KeyEvent.VK_BACK_SPACE);
break;
case(62):
bot.keyPress(KeyEvent.VK_SPACE);
bot.keyRelease(KeyEvent.VK_SPACE);
break;
case(59):
if(!shift){
bot.keyPress(KeyEvent.VK_SHIFT);
}else{
bot.keyRelease(KeyEvent.VK_SHIFT);
}
shift = !shift;
break;
case(60):
if(!shift){
bot.keyPress(KeyEvent.VK_SHIFT);
}else{
bot.keyRelease(KeyEvent.VK_SHIFT);
}
shift = !shift;
break;

case(90):
bot.keyPress(KeyEvent.VK_1);
bot.keyRelease(KeyEvent.VK_1);
break;
case(91):
bot.keyPress(KeyEvent.VK_2);
bot.keyRelease(KeyEvent.VK_2);
break;
case(92):
bot.keyPress(KeyEvent.VK_0);
bot.keyRelease(KeyEvent.VK_0);
break;
case(93):
bot.keyPress(KeyEvent.VK_3);
bot.keyRelease(KeyEvent.VK_3);
break;
case(104):
bot.keyPress(KeyEvent.VK_ENTER);
bot.keyRelease(KeyEvent.VK_ENTER);
break;
case(105):
bot.keyPress(KeyEvent.VK_BACK_SPACE);
bot.keyRelease(KeyEvent.VK_BACK_SPACE);
break;
case(106):
bot.keyPress(KeyEvent.VK_MINUS);
bot.keyRelease(KeyEvent.VK_MINUS);
break;
case(107):
bot.keyPress(KeyEvent.VK_EQUALS);
bot.keyRelease(KeyEvent.VK_EQUALS);
break;
case(108):
bot.keyPress(KeyEvent.VK_TAB);
bot.keyRelease(KeyEvent.VK_TAB);
break;
case(109):
bot.keyPress(KeyEvent.VK_ESCAPE);
bot.keyRelease(KeyEvent.VK_ESCAPE);
break;
}

} catch (IOException e) {
try {
e.toString();
in.close();
soc.close();
server.close();
break;
} catch (IOException e1) {
e1.toString();
}
}
}
System.out.println("PORT CLOSED");
}

}

}

除 90-93 外一切正常,它们导致程序永远循环,直到我单击 eclipse 然后它停止。这不是 eclipse 问题,因为当我将它构建到 .jar 中时,我得到了相同的结果并且它不是服务器问题,因为当我制作发送 90 发送 7 的按钮时它工作正常

似乎只有 90-93 会导致无限循环,我已经尝试将数字从 90 更改为 200 之类的值,但没有成功

有什么启发吗?

最佳答案

听起来像这样的代码:

bot.keyPress(KeyEvent.VK_1);
bot.keyRelease(KeyEvent.VK_1);

由于某种原因挂起。与 switch 语句无关。

关于Java永无止境的切换循环(有时)eclipse,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12558357/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com