简体   繁体   English

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

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

I wrote this code in Eclipse Java and for some reason, it doesn't run.我在 Eclipse Java 中编写了这段代码,由于某种原因,它没有运行。 It doesn't say it has any errors in it and no red marks appear anywhere in the code.它没有说它有任何错误,并且代码中的任何地方都没有出现红色标记。 I'm not sure what is wrong with it, please help.我不确定它有什么问题,请帮忙。

Here is the description of what I needed to write: Design and implement a class called PairOfDice, composed of two six-sided Die objects.以下是我需要编写的内容的描述: 设计并实现一个名为 PairOfDice 的 class,由两个六面 Die 对象组成。 Create a driver class called BoxCars with a main method that rolls a PairOfDice object 1000 times, counting the number of boxcars (two sixes) that occur.创建一个名为 BoxCars 的驱动程序 class,其主要方法是滚动 PairOfDice object 1000 次,计算发生的 boxcars 的数量(两个六)。

Another issue I have is creating "two six-sided Die objects" in the PairOfDice class.我遇到的另一个问题是在 PairOfDice class 中创建“两个六面模具对象”。 I don't have it written in the code, so if someone could explain how to implement those objects I would appreciate it.我没有把它写在代码中,所以如果有人能解释如何实现这些对象,我将不胜感激。

The last issue I'm having is making a driver class (BoxCars).我遇到的最后一个问题是制作驱动程序 class (BoxCars)。 I have tried to look up what exactly was a driver class but I couldn't find anything that I could understand.我试图查找驱动程序 class 到底是什么,但我找不到任何我能理解的东西。

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

Why is BoxCars inside the PairOfDice class?为什么 BoxCars 在 PairOfDice class 里面? They should be seperated files.它们应该是单独的文件。 Also the assignment says something about 'Die objects', which makes me question where your Die class is.作业还谈到了“模具对象”,这让我怀疑你的模具 class 在哪里。

The code itself looks like it should work and should give the correct answer though (I haven't tried it to run).代码本身看起来应该可以工作并且应该给出正确的答案(我还没有尝试过运行)。

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

相关问题 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 HTML5初学者。 我想要一个Speech-Box来运行Java程序 - HTML5 Beginner. I want a Speech-Box to run Java program IO程序不是从控制台运行的,而是从Eclipse(Java)运行的 - IO program doesn't run from console, but it does from Eclipse (Java) 程序未在Eclipse中运行 - Program doesn't run in Eclipse Java 不会停止程序运行但会返回错误的问题 - Java issue that doesn't stop the program from running but will return errors Java程序无法在Eclipse中作为应用程序运行 - Java program won't run as application in eclipse 如何在没有eclipse的服务器上运行eclipse之外的java程序 - how to run java program outside eclipse on a server that doesn't have eclipse 打开.java文件的程序,就像在Eclipse的控制台中一样运行 - Program to open .java files and run as if it were in Eclipse's console 即使在项目中检测到错误,eclipse 有没有办法运行 Java 程序? - Is there a way for eclipse to run a Java program even if there are errors being detected in the project? 可以编译但无法运行的Java程序 - Java Program that compiles but doesn't run
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM