怎样才能打印出正确的输出?

huangapple go评论61阅读模式
英文:

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 ( &quot;Sum is:&quot; + 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 ( &quot;Die rolled:&quot; + 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 ( &quot;Die rolled:&quot; + 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 ( &quot;Sum is:&quot; + sum);
	}
}

答案1

得分: 0

只需将 System.out.println(&quot; &quot;) 替换为 System.out.print(&quot; &quot;)

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(&quot; &quot;) with System.out.print(&quot; &quot;):

public static void foo(int rows) {
    for (int i = 1; i &lt;= rows; i++) {
        if (i &gt; 1)
            System.out.print(&#39; &#39;);

        for (int j = 0; j &lt; i; j++)
            System.out.print(&#39;X&#39;);
    }

    System.out.println();
}

huangapple
  • 本文由 发表于 2020年10月10日 18:50:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/64292576.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定