繁体   English   中英

BoxCars Java 程序 - 初学者。 奇怪的问题:程序不在控制台中运行,但它在 Eclipse Java 中没有列出任何错误

[英]BoxCars Java Program - Beginner. Weird Issue: Program doesn't run in the console but, it lists no errors in Eclipse Java

我在 Eclipse Java 中编写了这段代码,由于某种原因,它没有运行。 它没有说它有任何错误,并且代码中的任何地方都没有出现红色标记。 我不确定它有什么问题,请帮忙。

以下是我需要编写的内容的描述: 设计并实现一个名为 PairOfDice 的 class,由两个六面 Die 对象组成。 创建一个名为 BoxCars 的驱动程序 class,其主要方法是滚动 PairOfDice object 1000 次,计算发生的 boxcars 的数量(两个六)。

public class dieGames {

    public class PairOfDice {

       private int die1; 
       private int die2;

       public PairOfDice() {
           roll();
       }

       public void roll() {
          die1 = (int)(Math.random()*6) + 1;
          die2 = (int)(Math.random()*6) + 1;
       }

       public int getValueDie1() {
          return die1;
       }

       public int getValueDie2() {
          return die2;
       }

       public String toString() {
          return "Die 1: " + die1 + ", Die 2: " + die2;
       }
    }

    public class BoxCars
    {
       public void main(String[] args)
       {
          final int numRolls = 1000;
          int numBoxCars = 0;

          PairOfDice twoDice = new PairOfDice();

          for (int i = 0; i < numRolls; i++)
          {
             twoDice.roll();
             if (twoDice.die1 == 6 && twoDice.die2 == 6)
             {
                numBoxCars++;
             }
          }

          System.out.println("Number of Box Cars in " + numRolls +
                             " rolls is " + numBoxCars);
       }
    }
}

我可以在这里看到几件事:

  1. 您永远不会创建 diceGames class 的实例

  2. 您的主要方法不是 static,这是必需的

  3. 您的主要方法在另一个 class 定义中,该定义也从未实例化

对您的代码进行一些调整使其运行良好(在下面的代码中注释):

// this class remains unchanged, except for making the PairOfDice class static
// so that it's accessible to the main method
public class dieGames {

    public static class PairOfDice {

       private int die1; 
       private int die2;

       public PairOfDice() {
           roll();
       }

       public void roll() {
          die1 = (int)(Math.random()*6) + 1;
          die2 = (int)(Math.random()*6) + 1;
       }

       public int getValueDie1() {
          return die1;
       }

       public int getValueDie2() {
          return die2;
       }

       public String toString() {
          return "Die 1: " + die1 + ", Die 2: " + die2;
       }
    }


    // removed containing class, which was unnecessary
    // made main method static
    public static void main(String[] args)
       {
          // create an instance of the class
          dieGames game = new dieGames();  

          final int numRolls = 1000;
          int numBoxCars = 0;

          PairOfDice twoDice = new PairOfDice();

          for (int i = 0; i < numRolls; i++)
          {
             twoDice.roll();
             if (twoDice.die1 == 6 && twoDice.die2 == 6)
             {
                numBoxCars++;
             }
          }

          System.out.println("Number of Box Cars in " + numRolls +
                             " rolls is " + numBoxCars);

    }
}

由此,我得到 output 的Number of Box Cars in 1000 rolls is x ,其中 x 是取决于运行的某个值。

如果您希望在发布时满足分配,“驱动程序类”BoxCars 可以是一个单独的.java 文件,其中包含主要方法:

public class BoxCars {

    public static void main(String[] args)
       {

          final int numRolls = 1000;
          int numBoxCars = 0;

          PairOfDice twoDice = new PairOfDice();

          for (int i = 0; i < numRolls; i++)
          {
             twoDice.roll();
             if (twoDice.die1 == 6 && twoDice.die2 == 6)
             {
                numBoxCars++;
             }
          }

          System.out.println("Number of Box Cars in " + numRolls +
                             " rolls is " + numBoxCars);       
    }
}

在这种情况下,您需要为 die1 和 die2 创建一个 getter 方法,或者将它们公开在第一个文件中,现在应该只是 PairOfDice class 没有封闭 dieGames class。 请注意,此主要方法现在实例化 PairOfDice 而不是 dieGames,因为不需要 class。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM