我正在编写一个程序来使用 java 连接到控制台像素示例草图。我还是很新,我收到了这个错误:
不能从静态上下文中引用非静态变量 fast
我不知道错误是什么意思,但我的代码是:
package javaapplication5;
import java.net.*;
import java.io.*;
import java.util.Scanner;
/**
*
* @author preferreduser
*/
public class JavaApplication5 {
int fast = 0;
public static void main(String[] args) throws IOException {
Scanner x = new Scanner(System.in);
System.out.print("Yun ip: ");
String IP = x.nextLine();
System.out.println("Loding...");
try {
// Create a URL for the desired page
URL url = new URL("http://"+ IP +"/arduino/digital/13/1");
// Read all the text returned by the server
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
in.close();
} catch (MalformedURLException e) {
} catch (IOException e) {
}
try {
// Create a URL for the desired page
URL url = new URL("http://"+ IP +"/arduino/digital/13/0");
// Read all the text returned by the server
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
in.close();
} catch (MalformedURLException e) {
} catch (IOException e) {
}
System.out.println("Connected to YUN on "+ IP);
OUTER:
while (true) {
Scanner y = new Scanner(System.in);
System.out.print("> ");
String str = y.nextLine();
switch (str) {
case "on":
try {
// Create a URL for the desired page
URL url = new URL("http://"+ IP +"/arduino/digital/13/1");
// Read all the text returned by the server
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
in.close();
} catch (MalformedURLException e) {
} catch (IOException e) {
} break;
case "off":
try {
// Create a URL for the desired page
URL url = new URL("http://"+ IP +"/arduino/digital/13/0");
// Read all the text returned by the server
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
in.close();
} catch (MalformedURLException e) {
} catch (IOException e) {
} break;
case "help":
System.out.println("");
System.out.println("on exit");
System.out.println("off help");
System.out.println("");
break;
case "exit":
try {
// Create a URL for the desired page
URL url = new URL("http://"+ IP +"/arduino/digital/13/0");
// Read all the text returned by the server
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
in.close();
} catch (MalformedURLException e) {
} catch (IOException e) {
} break OUTER;
}
if ( fast == 1 ){
URL oracle = new URL("http://"+ IP +"/arduino/digital/13");
try (BufferedReader in = new BufferedReader(
new InputStreamReader(oracle.openStream()))) {
String inputLine;
while ((inputLine = in.readLine()) != null)
System.out.println(inputLine);
}
} else {System.out.println("Success");}
}
}
}
我想连接到一个 arduino yun 并键入诸如 on 或 off 之类的命令,并且该部分有效。我想快速添加一个可选选项,以消除每次输入命令时都连接到 http:// * /aruino/digital/13 以加快速度。这是我的开始。我要为它添加一个命令,但在我弄清楚之前我不能