我正在编写一个程序以使用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的情况。这是我的开始。我要为其添加一个命令,但是直到我弄清楚了之后才能这样做
更改int fast = 0;
为static int fast = 0;
您fast
在主要方法(静态方法)中使用了变量。在任何静态方法中使用的所有变量都应该是静态的。原因是静态方法对于一个类是通用的,它不依赖于该类的实例。因此,它不能在其中使用任何实例变量(除非您指定要使用哪个特定实例),因为该方法不知道要使用哪个实例变量。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句