Java:没有逗号的参数列表

时间:2015-04-29 14:57:49

标签: java methods parameters optional-parameters

我遇到了一个带参数列表的方法,其中参数没有用逗号分隔,也没有变量类型的声明:

public int compareWith(Choice anotherChoice){

后来这些参数在if-else语句中与body中的任何进一步声明一起被调用,并与另一个方法结合使用:

if ((this.type == 0)&&(anotherChoice.getType() == 1)){
        return -1;

这是整个班级的简短摘要:

public class Choice
{

private int type; 


public Choice(int type)
{
    //initialize the "type" instance varialble
    this.type = type;
}

  public int getType()
{
    return type;
}

 public int compareWith(Choice anotherChoice)
{
   Choice choice1 = new Choice(0);
   Choice choice2 = new Choice(1);
   if ((this.type == 0)&&(anotherChoice.getType() == 1)){
        return -1;

程序还在继续。我真的没有得到anotherChoice,getType()和choice2之间的链接。这是一个在线课程的任务,该程序按预期工作,但我不知道为什么。

1 个答案:

答案 0 :(得分:-1)

这是我对该课程的解决方案。可能不是最好的解决方案,但它有效,我收到了我的成绩。我现在正坚持使用LAB 04 TASK 3。

int player=this.getType();//get value of choice1
int computer=anotherChoice.getType();// this get the value choise2 out of anotherChoise
int som;// this INT i made to make a sum
int result;
result = 0;//i already gave a value on the INT result
som = player - computer;//this is the sum of de values choice1 and choise2

if(player == computer)  //all ties
  result = 0;     
else if(som == -1) //player ROCK and comp. PAPER or PAPER and SCISSOR
  result = -1;
else if(som == 2) //player SCISSOR and comp. ROCK
  result = -1;
else if(som == 1)//player SCISSOR and comp PAPER or PAPER and ROCK
  result = 1;
else if(som == -2)//player ROCK and comp. SCISSOR
  result = 1;
return result; // this line should be modified/removed upon finishing