【发布时间】:2015-07-06 12:36:18
【问题描述】:
我正在编写一个程序来使用 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 以加快速度。这是我的开始。我要为它添加一个命令,但在我弄清楚之前我不能这样做
【问题讨论】:
-
首先,试着清理一下你的代码;除了格式问题,您可能还想将一些代码拆分成几个方法
-
不要留下空的捕获部分。至少打印堆栈跟踪。
-
@Pshemo 声明应该是“不要留下空的捕获部分,至少评论他们的存在/目的”。要求至少一个堆栈跟踪将否定完全可接受的异常使用,例如用于终止转换循环的 NoSuchElement。
-
只是添加到其他一些答案:问题是
static方法不使用任何实例来运行(我们通过YourClass.staticMethod()从类运行它)。换句话说在静态方法中没有有效的this(在它的上下文中)所以像this.fast这样的代码(这就是你想要做的,因为fast是非静态字段,这意味着它需要一些实例,如果您不指定任何this会自动添加)没有意义,因为没有this。
标签: java variables arduino-yun