从静态方法[Java]访问非静态变量

从静态方法[Java]访问非静态变量,java,static-methods,Java,Static Methods,我想做的是在静态方法中引用一个非静态变量。我这样做是为了有一种主控件,我只需要在其中运行各个方法,使我的代码更加整洁。如果有任何其他方法可以轻松和/或有效地做到这一点。谢谢你抽出时间 import static java.lang.System.*; import java.util.Scanner; public class InteractiveStory { private int health, sanity, pumpkinPies, dexte, stren, chari,

我想做的是在静态方法中引用一个非静态变量。我这样做是为了有一种主控件,我只需要在其中运行各个方法,使我的代码更加整洁。如果有任何其他方法可以轻松和/或有效地做到这一点。谢谢你抽出时间

import static java.lang.System.*; 
import java.util.Scanner;

public class InteractiveStory
{
   private int health, sanity, pumpkinPies, dexte, stren, chari, intel;
   private double money;
   private char Gender;
   public static void main(String args[]) // Main chunk of programming, preferably /only/ method refrences. TL;DR, Master Controller
    {
      genPlayer();
      //By the by, need to figure out how to access non-static variables from a static context
    }
   public static void genPlayer() //Begins the character setup bit, gender, name, class, ect.
   {
      Scanner keyboard = new Scanner(System.in); // Initalises the keyboard intepreter
      System.out.println("You are a random bystandard. You are currently sitting in a swively chair infront of a computer. What is your gender?");
      System.out.print("> ");
      String Gender = keyboard.next();
      System.out.println("");

      if(Gender.equalsIgnoreCase("female")) // need to do female first because fe'male'. recognises that even in the middle of strings
      {
         System.out.println("Okay ma'am, what're your skills?");
         Gender = "f";
      }
      else if(Gender.equalsIgnoreCase("male"))
      {
         System.out.println("Okay sir, what're your skills?");
         Gender = "m";
      }
      else if(Gender != "male")
      {
         System.out.println("Oops! Invalid gender. Sorry.");
         System.out.println();
         genPlayer();
      }
      System.out.println("Are you,");
      System.out.println("1. Dextrious?");
      System.out.println("2. Strong?");
      System.out.println("3. Charasmatic?");
      System.out.println("4. Intelligent?");
      System.out.print("> ");
      int answer = keyboard.nextInt();
      System.out.println("");

      switch(answer){
         case 1:
            dexte = dexte+1; //Errors begin here, trying to reference a private variable that is nonstatic above.
            System.out.println("You are now slightly faster than your average turtle!");
            break;

         case 2:
            stren = stren+1;
            System.out.println("You can now lift 3 pounds extra! Now a whopping 4 pounds!");
            break;

         case 3:
            chari = chari+1;
            System.out.println("You no longer scare small children!");
            break;

         case 4:
            intel = intel+1;
            System.out.println("You now have a basis for flaunting your itnelligence to rocks!");
            break;

         default:
            System.out.println("Sorry, not a valid answer!");

      }
   }
}
控制台错误日志:

 ----jGRASP exec: javac -g InteractiveStory.java

InteractiveStory.java:59: error: non-static variable dexte cannot be referenced from a static context
            dexte = dexte+1;
            ^
InteractiveStory.java:59: error: non-static variable dexte cannot be referenced from a static context
            dexte = dexte+1;
                    ^
InteractiveStory.java:64: error: non-static variable stren cannot be referenced from a static context
            stren = stren+1;
            ^
InteractiveStory.java:64: error: non-static variable stren cannot be referenced from a static context
            stren = stren+1;
                    ^
InteractiveStory.java:69: error: non-static variable chari cannot be referenced from a static context
            chari = chari+1;
            ^
InteractiveStory.java:69: error: non-static variable chari cannot be referenced from a static context
            chari = chari+1;
                    ^
InteractiveStory.java:74: error: non-static variable intel cannot be referenced from a static context
            intel = intel+1;
            ^
InteractiveStory.java:74: error: non-static variable intel cannot be referenced from a static context
            intel = intel+1;
                    ^
8 errors

 ----jGRASP wedge2: exit code for process is 1.
 ----jGRASP: operation complete.

使您的
getPlayer
方法
非静态
并像这样调用它

    public static void main(String args[]) 
    {
      InteractiveStory interactiveStory = new InteractiveStory();
      interactiveStory.genPlayer();
    }
另见

请参考此内容,谢谢,这真的帮了大忙!