如何通过为我的代码创建新方法来减少main方法中的代码?

时间:2013-11-23 21:43:18

标签: java eclipse

您好我正在使用Eclipse编写游戏程序。 基本上,用户选择1.偶数或2.奇数 随机生成的骰子卷。如果用户登陆他做出的选择(偶数或奇数)他保持不变,他向前移动5步。 用户正在使用计算机。因此,如果用户是偶数,则计算机为奇数,反之亦然

我的问题是我不知道如何调用这种方法。请查看我的代码并给我建议。如果可以更好的让我知道。

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);

}

}

2 个答案:

答案 0 :(得分:1)

你的意思是你在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。这只是您可以用来运行代码的几种模式之一。

如果这不是您的意思,您能澄清一下您的问题吗?

答案 1 :(得分:0)

你必须为自己制造这么难。如果你想在另一个方法中使用main()的所有代码,那么就这样做。

public static void main(String[] args) {

    newMethod();
}

public static void newMethod() {

    // All the stuff from your old main goes in here
}

我只能想象您不理解static方法以及为什么您从main()调用的方法必须是静态的。

或者,完全按照@ nexus_2006的说法将游戏放入自己的班级,然后在main()中创建该班级的实例并从那里开始游戏。

public static void main(String[] args) {

    DiceGame game = new DiceGame();
    game.play();
}

上面的main()甚至可以进入DiceGame.java。无论哪种方式都可以。