嗨,我正在使用Eclipse编程游戏。基本上,用户选择1。偶数或2。古怪的随机生成的骰子滚动。如果用户降落在他所做的选择上(偶数或奇数),他会停留在其他位置,他会后退5步。用户正在对计算机进行vsing。所以,如果用户是偶数,计算机是奇数,反之亦然
我的问题是我不知道如何调用这种方法。请查看我的代码并给我建议。如果可以更好的话,也让我知道。
package Testing;
import java.util.Scanner;
import java.util.Random;
public class hundredsteps {
public static int playerChoice(){
System.out.println("Please choose an option below");
System.out.println("1. Even");
System.out.println("2. Odd");
Scanner input = new Scanner(System.in);
int choice = input.nextInt();
return choice;
// player set EVEN by default
}
public static int playerRoll(){
System.out.println("Press enter to roll");
Scanner input = new Scanner(System.in);
String roll = input.nextLine();
Random generator = new Random();
int dice1 = generator.nextInt(6)+1;
int dice2 = generator.nextInt(6)+1;
int sumOfDice = dice1 + dice2;
return sumOfDice;
}
public static int cpuRoll(){
Random generator = new Random();
int dice1 = generator.nextInt(6)+1;
int dice2 = generator.nextInt(6)+1;
int sumOfDice = dice1 + dice2;
return sumOfDice;
}
public static String gameWinner(int player, int cpu){
String winner = " ";
if (player == 100 && cpu == 100){
winner = " TIE ";
System.out.println("TIE GAME!!");
}else if (player == 100){
System.out.println();
winner = " player ";
System.out.println("Congratulations! You've won!");
}else if (cpu == 100){
winner = " cpu ";
System.out.println("Sorry, you lost. The computer won this round");
}
return winner;
}
public static void main(String[] args) {
int playerPosition = 0, cpuPosition = 0;
int playerRoll = 0, cpuRoll = 0;
do{
System.out.println();
playerRoll = playerRoll();
System.out.println();
System.out.println("You rolled " +playerRoll);
playerPosition = playerPosition + playerRoll;
if (playerPosition % 2 == 0){
System.out.println("You are now on step number " +playerPosition);
} else if (playerPosition % 2 != 0){
System.out.println("You landed on an odd number! You must move 5 steps back");
playerPosition = playerPosition - 5;
System.out.println("You are now on step number " +playerPosition);
}
cpuRoll = cpuRoll();
System.out.println("The computer rolled "+cpuRoll);
cpuPosition = cpuPosition + cpuRoll;
if (cpuPosition % 2 != 0){
System.out.println("The computer is now on step number " +cpuPosition);
} else if(cpuPosition % 2 == 0){
System.out.println("The computer landed on an even number! The computer moved 5 steps back");
cpuPosition = cpuPosition - 5;
System.out.println("The computer is now on step number " +cpuPosition);
System.out.println();
}
if (playerPosition > 100){
System.out.println("You rolled too high!");
System.out.println("You moved 20 steps back");
playerPosition = playerPosition - 20;
} if (cpuPosition > 100){
System.out.println("The computer rolled too high");
System.out.println("The computer moves 20 steps back");
cpuPosition = cpuPosition - 20;
}
} while (playerPosition <= 99 && cpuPosition <= 99);
gameWinner(playerPosition, cpuPosition);
}
}
你的意思是,如果main()中调用的所有方法都不是静态的,那么它们就会不断抛出编译错误,所以你"不能"调用它们吗?
如果这就是你的意思,你需要制作一些代码来运行/测试你的代码。你需要一些物品。Java是面向对象的(OO),所以总是考虑"事物"而不是"命令的过程/序列"
例如,制作一个测试类,它将"包含"你的"游戏对象"
public class GameTest {
public static void main(String[] args) {
DiceGame game = new DiceGame();
}
}
把你的游戏放在一个单独的类(一个全新的文件):
public class DiceGame {
//all your primitive/object references
//add a constructor
public DiceGame() {
//initialize primitives/objects
play();
}
public void play() {
//game logic goes here
//the stuff you have in main() right now
//this isn't a static method, so call any other methods you want
}
//put your other methods here
}
然后运行文件GameTest,而不是DiceGame。这只是您可以用来运行代码的几种模式之一。
如果这不是你的意思,你能澄清你的问题吗?
你一定让这件事对自己来说太难了。如果你想把main()的所有代码都放在另一个方法中,那么就这样吧。
public static void main(String[] args) {
newMethod();
}
public static void newMethod() {
// All the stuff from your old main goes in here
}
我只能想象你不理解static
方法,也不理解为什么你从main()
调用的方法必须是静态的。
或者,按照@next_2006所说的做,把你的游戏放在它自己的类中,然后在你的main()
中创建该类的一个实例,然后从那里开始游戏。
public static void main(String[] args) {
DiceGame game = new DiceGame();
game.play();
}
上面的main()
甚至可以进入DiceGame.java。无论哪种方式都可以。