public class ACRE {
/**
*
* @param args not used.
*/
public static void main(String[] args) {
int dineros;
int rep;
char optionKey;
dineros = 1500;
rep = 100;
optionKey = '0';
ACRE game = new ACRE();
game.startTrain(dineros, rep);
}
/**
* Start of the game
*
* @param dineros an integer number for the user's current money
* @param rep an integer number for the user's current reputation
*
*/
public void startTrain(int dineros, int rep) {
printStats(dineros, rep);
System.out.print("ur on a train. get off or nah etc etc");
//show options here
}
/**
* Print current money and reputation
*/
public void printStats(int dineros, int rep) {
System.out.print("Dinero: " + dineros + "D");
System.out.print(" * ");
System.out.print("Reputación: " + rep + "\n");
}
}
对我来说,dineros 和rep 必须跳来跳去似乎有点疯狂,我确信有更好、更有效的方法来做到这一点(就像 printStats 直接从 main 获取变量一样),但我无法想象知道如何做。谢谢
public class ACRE {
private int dineros;
private int rep;
public static void main(String[] args) {
ACRE game = new ACRE(1500, 100);
game.startTrain();
}
/**
* Create a new game
*
* @param dineros an integer number for the user's current money
* @param rep an integer number for the user's current reputation
*
*/
public ACRE(int dineros, int rep) {
// use this. to make clear you're setting the object's variables
this.dineros = dineros;
this.rep = rep;
}
public void startTrain() {
printStats();
System.out.print("ur on a train. get off or nah etc etc");
}
public void printStats() {
System.out.print("Dinero: " + dineros + "D");
System.out.print(" * ");
System.out.print("Reputación: " + rep + "\n");
}
}