简体   繁体   English

为掷硬币创建类和方法

[英]Creating a class and Method for a coin flip

I am doing a practice problem that deals with the flipping of a coin. 我正在做一个练习问题,涉及掷硬币。 I understand every other step, but I am confused at how to first setup the method and class. 我理解所有其他步骤,但是对于如何首先设置方法和类感到困惑。 I am confused with the variables that they have given me. 我对他们给我的变量感到困惑。 The question aks, 问题又来了,

Define a class Coin with 2 instance data, a string face, which can be “heads” or “tails” and an int value that represents the monetary value of the coin. 用两个实例数据定义一个硬币类,一个字符串面(可以是“正面”或“尾部”)和一个表示硬币货币价值的整数值。 The following method should be included in the class: - a default constructor - a constructor that takes 2 parameters to initialize instance data 该类中应包含以下方法:-默认构造函数-需要2个参数来初始化实例数据的构造函数

So my question to everyone is how would I go about creating this? 所以我对每个人的问题是我将如何去创建它?

This is what I have so far: 这是我到目前为止的内容:

public class Coin
{
    private int Value;
    private String Face;

    public Coin(int a)
    {
        Value = a;
        Face = "heads";
    }

    public Coin(int b, String faceTails)
    {
        Value = b;
        Face = "tails";
    }

From your requirements: 根据您的要求:

Define a class Coin with 2 instance data, a string face, which can be “heads” or “tails” and an int value that represents the monetary value of the coin. 用两个实例数据定义一个硬币类,一个字符串面(可以是“正面”或“尾部”)和一个表示硬币货币价值的整数值。 The following method should be included in the class: 该类中应包含以下方法:

  • a default constructor: 默认构造函数:

An empty bean constructor needs to be defined in Java if there is a constructor that takes an argument, like so: 如果存在带有参数的构造函数,则需要用Java定义一个空的bean构造函数,如下所示:

public Coin(){...}
  • a constructor that takes 2 parameters to initialize instance data 一个带有2个参数的初始化实例数据的构造函数

You have this defined properly, but you may not need the constructor with 1 argument. 您已正确定义了此定义,但可能不需要带有1参数的构造函数。

public Coin(int b, String face)
{
    this.value = b;
    this.face = face;
}

You may also want to add some getters for the field values so you can access the values from outside the class. 您可能还想为字段值添加一些吸气剂,以便可以从类外部访问这些值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM