java - 如何打印出正确的输出?
问题描述
CS101:实验 #12 写作课 II
在本实验中,您将编写三个类:Die、PairOfDice 和 Player。
Die 类模仿骰子的滚动。具体来说,这个类将实现以下方法:
将骰子的面数初始化为 6 的默认构造函数。
具有整数边数的重载构造函数(假设大于 1)。
roll 生成并返回一个介于 1 和边数(含)之间的随机数。
一种读取骰子面值的访问器方法。
返回面值的字符串表示形式的 toString 方法。
最大边数应作为私有常量存储在 Die 类中。还使用 Random 类作为随机数生成器。
PairOfDice 类模拟两个骰子的滚动。具体来说,这个类将实现以下方法:
一个默认构造函数,它创建并将每个骰子的面数初始化为 6。
一个重载的构造函数,它创建并采用两个整数边,每个骰子一个。
roll 掷骰子并返回总和。
读取骰子总和的访问器方法。
Player 类实现了 main 方法,该方法创建骰子对并将它们滚动数次以报告结果。
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 );
}
}
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;
// 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);
}
}
我无法打印出来。这就是我得到的 _JAVA_OPTIONS:-Xmx512m 线程“main”中的异常 java.lang.Error:未解决的编译问题:构造函数 PairOfDice() 未定义 语法错误,插入“VariableDeclarators”以完成 LocalVariableDeclaration roll 无法解决或是不是字段构造函数 Die() 未定义
at Player.main(Player.java:12)
在此处输入代码 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);
}
}
解决方案
只需替换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();
}
推荐阅读
- android-gradle-plugin - 在项目':app'中找不到路径'unzipJacocoAgent'的任务
- python - 为什么我的像素颜色随机变化不起作用?(与 PIL)
- django - 未激活 DRF 的令牌认证
- c# - LINQ - 如何连接多个表并动态排序?
- javascript - 创建包含来自另一个对象列表的特定字段的新 JSON 对象列表
- mysql - Laravel 和 MySQL 连接
- laravel - Route::resource 的附加 PUT/PATCH 路由
- java - 无法使用 Servlet、JSP、MySql 进行 Country、City、State Drop down
- google-apps-script - 有工作脚本,需要添加变量
- c# - 在 C# 中生成 EDI 856