String F = Key.nextLine();
它阻止程序工作(当我把它拿出来时程序继续工作)
为什么会这样?以及如何解决它? 这是阻止机器人的正确方法吗?
import java.awt.AWTException;
import java.awt.Color;
import java.awt.Robot;
import java.util.Scanner;
public class MyBot {
public static void main(String[] args)
throws AWTException{
Robot myRobot = new Robot();
StopRobot();
for(int i=0;i<300;i++){//the robot works just fine
Color color = myRobot.getPixelColor(i,190);
if((color.getRed()==0 && color.getGreen()==0 && color.getBlue()==0)){
myRobot.mouseMove(i,190);
}
}
}
public static void StopRobot() {
Scanner Key = new Scanner(System.in);
String F = Key.nextLine(); // this line stops my program
if (F.equals(" "))
System.exit(0);// 'Space' is the button to stop the program
}
}
提前致谢。
答案 0 :(得分:0)
进入空格键时是否要停止方法?
我认为你必须等待一段时间遇到一个空格字符或者声明你的StopRobot方法是否返回true标志。
import java.awt.AWTException;
import java.awt.Color;
import java.awt.Robot;
import java.util.Scanner;
public class MyBot {
public static void main(String[] args)
throws AWTException{
Robot myRobot = new Robot();
while(!StopRobot())
{
for(int i=0;i<300;i++){//the robot works just fine
Color color = myRobot.getPixelColor(i,190);
if((color.getRed()==0 && color.getGreen()==0 && color.getBlue()==0)){
myRobot.mouseMove(i,190);
}
}
}
}
public static boolean StopRobot() {
boolean isStop = false;
Scanner Key = new Scanner(System.in);
System.out.println("A"); // A prints
String F = Key.nextLine();
System.out.println("B"); // B does not print
if (F.equals(" "))
{
System.out.println("C"); // B does not print
//System.exit(0);// 'Space' is the button to stop the program
isStop = true;
}
return isStop;
}
}