Please support our Java advertiser: Lunarpages Java Web Hosting
Views: 4908 | Replies: 2
![]() |
•
•
Join Date: Jul 2005
Posts: 1
Reputation:
Rep Power: 0
Solved Threads: 0
How is it to pass "int [] rank" to main method?
public class Card
{
private int [] rank= new int [13];
private String [] suit={"Spades","Hearts","Diamonds","Clubs"};
private static void rankMaker (int []rank)
{
for (int i=1;i<=13;i++)
rank[i]=i;
}
public static void main (String []args)
{
System.out.println("Jack ="+rank[11]);
}
}
Ignoring the way I am making this deck, what I want to know is how generally can I use "int [] rank" (or any other variable declared outside the main) in other methods?
public class Card
{
private int [] rank= new int [13];
private String [] suit={"Spades","Hearts","Diamonds","Clubs"};
private static void rankMaker (int []rank)
{
for (int i=1;i<=13;i++)
rank[i]=i;
}
public static void main (String []args)
{
System.out.println("Jack ="+rank[11]);
}
}
Ignoring the way I am making this deck, what I want to know is how generally can I use "int [] rank" (or any other variable declared outside the main) in other methods?
•
•
Join Date: Nov 2004
Location: Netherlands
Posts: 5,752
Reputation:
Rep Power: 19
Solved Threads: 200
You can't, period.
The normal way is to have the main method do nothing except handle commandline arguments, create a single instance of the application (usually its own class), and start that instance.
The normal way is to have the main method do nothing except handle commandline arguments, create a single instance of the application (usually its own class), and start that instance.
As people are clearly allowed to attack me but I'm not allowed to defend myself, I no longer post to this site.
•
•
Join Date: Jun 2004
Location: H4x0rville
Posts: 2,105
Reputation:
Rep Power: 9
Solved Threads: 18
Just in case, here's kindof what he's talking about:
public class Card
{
private int [] rank= new int [13];
private String [] suit={"Spades","Hearts","Diamonds","Clubs"}; //Not used?
public Card()
{
super();
}
public void setRanks(int []rank)
{
for (int i=1;i<=13;i++)
{
rank[i]=i;
}
}
public int getRankAt(int index)
{
return this.rank[index];
}
}
class TestCard
{
public static void main(String[] args)
{
Card c = new Card();
System.out.println(c.getRankAt(11) + "");
}
}![]() |
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)






Linear Mode