0

我正在编写一个程序来使用 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 以加快速度。这是我的开始。我要为它添加一个命令,但在我弄清楚之前我不能

4

2 回答 2

2

class您只能通过 make 直接访问 a 的成员变量static。Om 制作变量的static另一种方法是通过class_name.variable_name.

否则,您必须创建一个objectclass然后您才能通过该对象访问该变量。

例子:

  • 要么你改变

    int fast=0;static int fast = 0;

  • 或者你这样做

    JavaApplication5 j5 = new JavaApplication5();现在通过访问变量j5.fast并进行进一步的计算。

于 2015-07-06T12:38:27.247 回答
1

更改int fast = 0;static int fast = 0;

fast在 main 方法中使用变量,这是一个静态方法。任何静态方法中使用的所有变量都应该是静态的。原因是类的静态方法是通用的,它不依赖于类的实例。因此它不能在其中使用任何实例变量(除非您指定要使用的特定实例),因为该方法不知道要使用哪个实例变量。

于 2015-07-06T12:38:55.763 回答