Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

C# - I would like to output the results of the dice roll to theconsole however I am getting the error An object reference isrequired

C# - I would like to output the results of the dice roll to theconsole however I am getting the error "An object reference isrequired for the non-static field, method, or property'Die.GetFaceValue()"

I have already asked this question three times and no one hasuploaded code that works. Please check the full code works in youranswer.

__________________________

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DiceRoller
{
public class Die
{
public static void Main(string[]args)
{
Console.WriteLine("{0}",Die.GetFaceValue());
Console.ReadLine();
}
private const int SIX_SIDED = 6;
private const int DEFAULT_FACE_VALUE =1;
private const int MIN_FACES = 3;
///


///
///

private int numFaces; //number of sideson die
private int faceValue; // which side isshowing
private static Random randomNumber =new Random();


public Die()
{
numFaces =SIX_SIDED;
faceValue =DEFAULT_FACE_VALUE;
}

///


/// Allows user to specify the numberof sides on a Die.
/// If "faces" is less than 3, asix-sided die is instantiated.
///

/// thenumberr of sides
public Die(int faces)
{

if (faces >=MIN_FACES)
{
numFaces =faces;
}
else
{
numFaces =SIX_SIDED;
}

RollDie();
}

///


/// Simulates the rolling of aDie.
///

public void RollDie()
{
faceValue =randomNumber.Next(1, numFaces + 1);
} // end RollDie

///


/// Die accessor
///

/// The current face ofthe Die
public int GetFaceValue()
{
return faceValue;
} //end GetFaceValue

}// end Class Die
}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Design Operation And Evaluation Of Mobile Communications

Authors: Gavriel Salvendy ,June Wei

1st Edition

3030770249, 978-3030770242

More Books

Students also viewed these Programming questions