我正在编写一个用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。这是我的开始。我要为它添加一个命令,但是直到我弄清楚它才能得到它
答案 0 :(得分:2)
您只能通过class
来直接访问static
的成员变量。 Om使变量static
更多的方法来访问它是class_name.variable_name
。
否则您必须object
class
,然后您可以通过该对象访问该变量。
示例:强>
要么改变
int fast=0;
至static int fast = 0;
或者你这样做
JavaApplication5 j5 = new JavaApplication5();
现在按j5.fast
访问变量并进行进一步计算。
答案 1 :(得分:1)
将int fast = 0;
更改为static int fast = 0;
您在main方法中使用变量fast
,这是一种静态方法。在任何静态方法中使用的所有变量都应该是静态的。原因是静态方法对于一个类来说很常见,它不依赖于类的实例。因此,它不能使用任何实例变量(除非您指定要使用的特定实例),因为该方法不知道要使用哪个实例变量。