英文:
How Can I print out the right output?
问题
CS101: 实验 #12
编写类 II
在这个实验中,您将编写三个类:Die(骰子)、PairOfDice(两个骰子)和Player(玩家)。
Die类模拟骰子的掷骰过程。具体而言,这个类将实现以下方法:
- 一个默认构造函数,将骰子的面数初始化为6。
- 一个重载的构造函数,接受一个整数面数(假设大于1)。
- roll方法生成并返回一个介于1和面数之间的随机数(包括1和面数)。
- 一个访问器方法来读取骰子上的点数。
- 一个toString方法,返回点数的字符串表示。
最大面数应该作为Die类中的私有常量存储。同时使用Random类进行随机数生成。
PairOfDice类模拟掷两个骰子。具体而言,这个类将实现以下方法:
- 一个默认构造函数,创建并将每个骰子的面数初始化为6。
- 一个重载的构造函数,创建并分别接受两个整数面数,一个用于每个骰子。
- roll方法掷每个骰子并返回它们的和。
- 一个访问器方法来读取骰子的和。
Player类实现了主方法,该方法创建骰子对并多次掷骰子并报告结果。
以下是您提供的代码:
public class Player {
public static void main(String[] args) {
Die die1 = new Die();
System.out.println(die1);
PairOfDice sum = new PairOfDice();
System.out.println(sum);
}
}
import java.util.Random;
public class PairOfDice {
private int maxSides = 6;
private int sides = 1;
private int maxSides2 = 6;
private int sides2 = 1;
private Random randNum;
private Random randNum2;
private int sum;
public int die1;
public int die2;
public int MaxSides() {
maxSides = 6;
maxSides2 = 6;
return maxSides + maxSides2;
}
public PairOfDice(int roll) {
randNum.nextInt();
randNum2.nextInt();
}
public int roll() {
sides = randNum.nextInt(maxSides) + 1;
sides2 = randNum2.nextInt(maxSides2) + 1;
return ((randNum.nextInt(maxSides) + 1) + (randNum2.nextInt(maxSides2) + 1));
}
public int getfaceOfDie() {
sum = sides + sides2;
return sum;
}
public String toString() {
return ("Sum is:" + sum);
}
}
import java.util.Random;
public class Die {
private int maxSides = 6;
public int sides = 1;
private Random randNum;
public int MaxSides() {
maxSides = 6;
return maxSides;
}
public Die(int roll) {
randNum.nextInt(6);
}
public int roll() {
sides = randNum.nextInt(maxSides) + 1;
return randNum.nextInt(maxSides) + 1;
}
public int getfaceOfDie() {
return sides;
}
public String toString() {
return ("Die rolled:" + sides);
}
}
请注意,您提供的代码中存在一些问题,例如缺少必要的导入语句和变量初始化。我已经帮您将代码整理成了类的形式,以便更好地理解和阅读。如果您有任何问题或需要进一步的帮助,请随时提问。
英文:
CS101: Lab #12
Writing Classes II
In this lab you will write three classes: Die, PairOfDice, and Player.
The Die class mimics the rolling of a die. Specifically, this class will implement the following methods:
A default constructor initializing the number of sides of a die to 6.
An overloaded constructor that takes an integer number of sides (assume greater than 1).
roll which generates and returns a random number between 1 and the number of sides
(inclusive).
An accessor method to read the value of the face on the die.
A toString method returning the string representation of the face value.
The maximum number of sides should be stored as a private constant in the Die class.
Also use the
Random class for the random number generator.
The PairOfDice class mimics the rolling of two dice. Specifically, this class will implement the
following methods:
A default constructor that creates and initializes the number of sides of each die to 6.
An overloaded constructor that creates and takes two integer number of sides, one for each die.
roll rolls each die and returns the sum.
An accessor method to read the sum of the dice.
The Player class implements the main method which creates the dice pair and rolls them several
times reporting the results.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
public class Player {
public static void main(String[] args) {
// TODO Auto-generated method stub
Die die1 = new Die ();
System.out.println ( die1);
PairOfDice sum = new PairOfDice ( );
System.out.println (sum );
}
}
<!-- language: lang-css -->
import java.util.Random;
public class PairOfDice
{
private int maxSides = 6;
private int sides = 1;
private int maxSides2 = 6;
private int sides2 = 1;
private Random randNum;
private Random randNum2;
private int sum;
public int die1;
public int die2;
public int MaxSides ()
{
maxSides = 6;
maxSides2 = 6;
return maxSides + maxSides2;
}
public PairOfDice (int roll)
{
randNum.nextInt ();
randNum2.nextInt ();
}
public int roll ()
{
sides = randNum.nextInt ( maxSides) + 1;
sides2 = randNum2.nextInt ( maxSides2) + 1;
return ((randNum.nextInt (maxSides) + 1) + ( randNum2.nextInt (maxSides2) + 1)) ;
}
public int getfaceOfDie ( )
{
sum = sides + sides2;
return sum;
}
public String toString ()
{
return ( "Sum is:" + sum);
}
}
<!-- language: lang-html -->
import java.util.Random;
public class Die
{
private int maxSides = 6;
public int sides = 1;
private Random randNum;
// A default constructor initializing the number of sides of a die to 6.
public int MaxSides ()
{
maxSides = 6;
return maxSides;
}
// An overloaded constructor that takes an integer number of sides (assume greater than 1).
public Die(int roll)
{
randNum.nextInt (6);
}
//roll which generates and returns a random number between 1 and the number of sides
public int roll ()
{
sides = randNum.nextInt ( maxSides) + 1;
return randNum.nextInt (maxSides) + 1;
}
//An accessor method to read the value of the face on the die.
public int getfaceOfDie()
{
return sides;
}
// A toString method returning the string representation of the face value.
public String toString ()
{
return ( "Die rolled:" + sides);
}
}
<!-- end snippet -->
I can't print out. This is what I get
Picked up _JAVA_OPTIONS: -Xmx512m
Exception in thread "main" java.lang.Error: Unresolved compilation
problems:
The constructor PairOfDice() is undefined
Syntax error, insert "VariableDeclarators" to complete
LocalVariableDeclaration
roll cannot be resolved or is not a field
The constructor Die() is undefined
at Player.main(Player.java:12)
enter code here
import java.util.Random;
public class Die
{
private int maxSides = 6;
public int sides = 1;
private Random randNum;
// A default constructor initializing the number of sides of a die to 6.
public int MaxSides ()
{
maxSides = 6;
return maxSides;
}
// An overloaded constructor that takes an integer number of sides (assume
greater than 1).
public Die(int roll)
{
randNum.nextInt (6);
}
//roll which generates and returns a random number between 1 and the
number of sides
public int roll ()
{
sides = randNum.nextInt ( maxSides) + 1;
return randNum.nextInt (maxSides) + 1;
}
//An accessor method to read the value of the face on the die.
public int getfaceOfDie()
{
return sides;
}
// A toString method returning the string representation of the face value.
public String toString ()
{
return ( "Die rolled:" + sides);
}
}
import java.util.Random;
public class PairOfDice
{
private int maxSides = 6;
private int sides = 1;
private int maxSides2 = 6;
private int sides2 = 1;
private Random randNum;
private Random randNum2;
private int sum;
public int die1;
public int die2;
public int MaxSides ()
{
maxSides = 6;
maxSides2 = 6;
return maxSides + maxSides2;
}
public PairOfDice (int roll)
{
randNum.nextInt ();
randNum2.nextInt ();
}
public int roll ()
{
sides = randNum.nextInt ( maxSides) + 1;
sides2 = randNum2.nextInt ( maxSides2) + 1;
return ((randNum.nextInt (maxSides) + 1) + ( randNum2.nextInt
(maxSides2) + 1)) ;
}
public int getfaceOfDie ( )
{
sum = sides + sides2;
return sum;
}
public String toString ()
{
return ( "Sum is:" + sum);
}
}
答案1
得分: 0
只需将 System.out.println(" ")
替换为 System.out.print(" ")
:
public static void foo(int rows) {
for (int i = 1; i <= rows; i++) {
if (i > 1)
System.out.print(' ');
for (int j = 0; j < i; j++)
System.out.print('X');
}
System.out.println();
}
英文:
Just replace System.out.println(" ")
with System.out.print(" ")
:
public static void foo(int rows) {
for (int i = 1; i <= rows; i++) {
if (i > 1)
System.out.print(' ');
for (int j = 0; j < i; j++)
System.out.print('X');
}
System.out.println();
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论