[英]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.