非静态变量不能从静态上下文Java中引用

施马克先生

我正在编写一个程序以使用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] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

java:不能从静态上下文中引用非静态变量错误

java:非静态变量,不能从静态上下文中引用

不能从静态上下文中引用非静态变量 [JAVA]

非静态变量s不能从静态上下文中引用

非静态变量,不能从静态上下文中引用

“不能从静态上下文中引用的非静态变量”?

创建对象时,“不能从静态上下文引用的非静态变量”

不能从静态上下文中引用非静态变量?

非静态变量 this 不能从静态上下文中引用 Dormitory dormitory = new Dormitory();

Rectangle.java:35:错误:非静态变量,不能从静态上下文中引用

Java Blackjack 程序创建错误:不能从静态上下文中引用非静态变量

从静态上下文引用非静态变量

从静态上下文引用非静态变量

非静态变量,不能从静态内容Java引用

的java - 非静态方法“getLogger”不能从静态上下文中引用

非静态方法不能从静态上下文中的java 8流引用

JAVA - 不能从静态上下文中引用非静态方法 add(E)

错误:无法从Java的静态上下文中引用非静态变量扫描

无法从静态上下文Java引用非静态变量

无法从静态上下文引用Java JTextField和非静态变量

Java语言| 错误:无法从静态上下文引用非静态变量扫描

非静态方法不能从静态上下文中引用

非静态方法不能从静态上下文中引用

非静态方法getIntent()不能从静态上下文中引用

不能从静态上下文中引用非静态方法 matcher(CharSequence)

不能从静态上下文引用非静态字段mFirebaseAnalytics

非静态方法count(int)不能从静态上下文中引用

非静态字段不能从静态上下文引用-Main方法

Flutter:不能从静态上下文引用非静态方法registerWith(Registrar)