林通過這一個我在循環中有一個switch語句完全不解:的Java永無止境的開關回路(有時)日食
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然後停止。這不是一個日食問題,因爲當我把它構建成一個.jar時,我得到了相同的結果,它不是服務器問題,因爲當我發送90發送7的按鈕時它工作正常
它似乎只有90-93導致無限循環,我已經嘗試將數字從90改爲200,但沒有運氣
任何啓蒙傢伙?
這是件好事想法記錄/打印有關try-catch中拋出的異常的信息。 'e.toString()'只會返回帶有關於異常的信息的String,但你必須在某處使用它。 – Pshemo
你的'System.out.println(key);'行是否顯示正確的'readInt()'值? – doublesharp
@doublesharp這是瘋狂的事情,它輸出一個正確的數字,然後開始輸出另一個數字 - 繼承人當我發送90時,它按下1然後開始連續讀取8作爲輸入也按1 – MrFuzzles