如何从方法中保存返回值,然后将其传递给另一个方法?

时间:2016-01-14 01:27:27

标签: java

我很困惑如何保存从'makeThrow()'和'makeThrow(int throwType)'构造函数生成的整数,然后将它们传递给'lastThrow()'方法。

import java.util.Random;

public class RochambeauPlayer
extends java.lang.Object
    {
      private String name;
      private int wins;
      private int throwType;

      public java.lang.String lastThrow(){
        if(throwType == 0){
          String rock = "ROCK";
          return rock;
        }
        else if(throwType == 1){
          String paper = "PAPER";
          return paper;
        }
        else{
          String scissors = "SCISSORS";
          return scissors;
        }
      }   

投掷数字要​​么是随机生成的,要么由用户输入并保存到throwType

      public int makeThrow(){
        Random rand = new Random();
        int throwType = rand.nextInt(3);
        this.throwType = throwType;
        return throwType;
      }

      public int makeThrow(int throwType){
        this.throwType = throwType;
        return throwType;
      }

1 个答案:

答案 0 :(得分:3)

这些方法不是构造函数。

您无法向lastThrow方法传递任何内容,因为它不会采用任何参数。

您的makeThrow()方法都已更改throwType的值,因此下次您致电lastThrow()时,它将返回正确的String