简体   繁体   English

硬币翻转 Java 程序

[英]Coin Flip Java Program

Here is the Java code from Lewis/Loftus Book.这是来自 Lewis/Loftus Book 的 Java 代码。 I am having difficult in understanding the function of two method and how they are invoked.我很难理解两种方法的 function 以及它们是如何被调用的。 Hope you can elaborate it for me.希望你能为我详细说明。 I am a new to programming and still in learing phase.我是编程新手,仍处于学习阶段。 Below is the code from the book.下面是书中的代码。

My learning is myCoin object is created when the new operation call Coin() construction.我的学习是myCoin object是在新操作调用Coin()构造时创建的。 Invoking Coin() constructor invokes flip() method.调用 Coin() 构造函数调用 flip() 方法。 Which then calculate the face.然后计算人脸。 I guess the code myCoin.flip also does the same as described above.我猜 myCoin.flip 的代码也和上面描述的一样。 Is the myCoin.flip a redundant code here. myCoin.flip 是不是这里的冗余代码。

System.out.println(myCoin) print head or tail. System.out.println(myCoin) 打印头部或尾部。 How is this possible?这怎么可能? Because myCoin is object variable that contain information about head/tail.因为 myCoin 是包含头/尾信息的 object 变量。 It also have different method associated.它也有不同的关联方法。 How does it print only the face.它如何只打印脸部。

How does the codes isHead and toString work.代码 isHead 和 toString 是如何工作的。 In the main program toString method is not invoked and it print Tail.在主程序中没有调用 toString 方法,它打印 Tail。 What is the function of boolean isHead() method. boolean isHead()方法的function是什么。 What it does.它能做什么。

import java.util.Random;

class Coin
{
   private final int HEADS = 0;
   private final int TAILS = 1;

   private int face;


       public Coin ()
   {
      flip();
   }

   //-----------------------------------------------------------------
   //  Flips the coin by randomly choosing a face value.
    //-----------------------------------------------------------------
   public void flip ()
   {
      face = (int) (Math.random() * 2);
   }

   //-----------------------------------------------------------------
   //  Returns true if the current face of the coin is heads.
   //-----------------------------------------------------------------
   public boolean isHeads ()
   {
     return (face == HEADS);
   }

    //-----------------------------------------------------------------
   //  Returns the current face of the coin as a string.
   //-----------------------------------------------------------------
   public String toString()
   {
      String faceName;

      if (face == HEADS)
     faceName = "Heads";
  else
     faceName = "Tails";

  return faceName;
   }
   }



   public class CoinFlip





   {
      //-----------------------------------------------------------------
    // Creates a Coin object, flips it, and prints the results.
    //-----------------------------------------------------------------
     public static void main(String[] args)
   {
    Coin myCoin = new Coin();
     myCoin.flip();
     System.out.println(myCoin);
   if (myCoin.isHeads())
    System.out.println("You win.");
    else
    System.out.println("Better luck next time.");

      }
    }

The "face" variable is an instance variable for your Object. “face”变量是 Object 的实例变量。 Each of your Coin objects will have a "face."您的每个硬币对象都会有一个“面孔”。 The isHeads() checks the value of "face" and compares it to HEADS. isHeads() 检查“face”的值并将其与 HEADS 进行比较。 "face" was assigned either a 0 or a 1 when it was created. “face”在创建时被指定为 0 或 1。 If the method returns true, "face" stores the value of '1'.如果该方法返回 true,“face”将存储 '1' 的值。 The toString() creates a String variable "faceName" and then checks the value of your "face" variable of your coin. toString() 创建一个字符串变量“faceName”,然后检查硬币的“face”变量的值。 If it's '1', faceName with be assigned the word "Heads" and vice versa.如果它是“1”,则为 faceName 分配单词“Heads”,反之亦然。 The method will return a String.该方法将返回一个字符串。

Try this after creating the object to print.在创建要打印的 object 后尝试此操作。 Ex:前任:

Coin myCoin = new Coin();
System.out.println(myCoin.toString());

I hope this helps!我希望这有帮助!

System.out.println(myCoin)

Is actually实际上是

System.out.println(myCoin.toString())

Since your toString() is as follow:由于您的 toString() 如下:

    public String toString()
   {
      String faceName;

      if (face == HEADS)
     faceName = "Heads";
  else
     faceName = "Tails";

  return faceName;
   }

How does this work?这是如何运作的?

It returns the string "Heads" or "Tails" depending on whether face == HEADS or face != HEADS它返回字符串“Heads”或“Tails”,具体取决于 face == HEADS 还是 face != HEADS

What does isHead() do? isHead() 是做什么的?

Lets look at your code让我们看看你的代码

    public boolean isHeads ()
   {
     return (face == HEADS);
   }

So what this means is所以这意味着

is face == HEADS?是脸 == 头吗?

If yes return the boolean true //face == HEADS如果是,则返回 boolean true //face == HEADS

Otherwise return false //face != HEADS否则返回 false //face != HEADS

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

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