Results 1 to 4 of 4
- 12-15-2011, 07:40 PM #1
Member
- Join Date
- Dec 2011
- Posts
- 4
- Rep Power
- 0
Variables from another class won't assign to main class
Hey guys. Simple program and my variables are not recognized in the main even after calling them for assignment. When I try to print out the assigned Health, Strength etc variables in Line 20 it says they are not assigned. What am i doing wrong?
Java Code:package rpgmain; import java.util.Scanner; public class Main { public static void main(String args[]) { System.out.println("Welcome to Earthbound!"); Scanner chooser = new Scanner(System.in); System.out.println("Do you want to be a fighter (1) or mage (2)?"); int Charpick; Charpick = chooser.nextInt(); if (Charpick == 1){ Fighter Fighter1 = new Fighter(); Fighter1.Fighterstats(); System.out.println("You have chosen: Fighter"); }else{ Mage Mage1 = new Mage(); Mage1.Magestats(); System.out.println("You have chose: Mage"); System.out.println("Health: "+ Health +" Magic: "+Magic+" Strength: "+Strength+" Heal: "+Heal); } } } package rpgmain; public class Mage { public void Magestats(){ int Health, Strength, Magic, Heal; Health = 50; Strength = 5; Magic = 25; Heal = 10; } } package rpgmain; public class Fighter { public void Fighterstats(){ int Strength, Health, Magic, Heal; Strength = 20; Health = 100; Magic = 10; Heal = 5; } }
- 12-15-2011, 09:08 PM #2
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,609
- Rep Power
- 5
Re: Variables from another class won't assign to main class
The compiler has no clue what those variables are, as they loose scope once the called methods exit. Suggested reading:
Classes (The Java™ Tutorials > Learning the Java Language > Classes and Objects)
- 12-15-2011, 09:45 PM #3
Member
- Join Date
- Dec 2011
- Posts
- 4
- Rep Power
- 0
Re: Variables from another class won't assign to main class
dowhile thanks for the reply. I am still not clear after reading that site what to do. How do I bring the scope of the variables into the main?
- 12-15-2011, 10:07 PM #4
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,609
- Rep Power
- 5
Similar Threads
-
Can't Get variable calculated in another class to function in main class
By hkp30 in forum New To JavaReplies: 0Last Post: 10-23-2011, 10:49 PM -
Could not find or load main class BubbleSort.class
By blaqkout in forum New To JavaReplies: 5Last Post: 09-12-2011, 07:54 PM -
Running main method class from another main class
By tlrocketman in forum New To JavaReplies: 3Last Post: 12-06-2010, 08:30 AM -
How to assign unique id to each object created from same class
By srisar in forum New To JavaReplies: 2Last Post: 02-18-2010, 05:26 PM -
Exception Class for class that compares objects variables. Help Please.
By darkblue24 in forum New To JavaReplies: 1Last Post: 01-03-2010, 09:48 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks