What I'd like to do is reference a non-static variable in a static method. I'm doing this so I can have a sort of master control that I just have to run the individual methods in to make my code alot more neat. If there's any other ways to do this easily and/or efficiently. Thanks for your time.
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!");
}
}
}
Console error log:
----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.