高尔夫球返还输出逻辑问题

huangapple go评论58阅读模式
英文:

Golf return output logic issue

问题

以下是翻译好的内容:

嘿,大家好,我正在尝试解答一个Java课程的作业问题,但我在理解逻辑方面遇到了一些困难。以下是他们向我提问的问题:

高尔夫分数记录了将球送入洞内所用的击球数。预期击球数因洞而异,被称为标准杆(即3、4或5杆)。每个分数都有一个基于实际击球数与标准杆的比较而得的可爱名称。如果击球数比标准杆少两杆,则返回“老鹰”。如果击球数比标准杆少一杆,则返回“小鸟”。如果标准杆与击球数完全匹配,则返回“标准杆”。如果击球数比标准杆多一杆,则返回“柏忌”。如果标准杆不是3、4或5,则返回“错误”。

以下是我的代码:

public class Main {
   
   public String golfScore(int par, int strokes){
      
      if(strokes <= par - 2){
         return "Eagle";
      }
      else if(strokes <= par - 1){
         return "Birdie";
      }
      else if(strokes == par){
         return "Par";
      }
      else if(strokes > par + 1){
         return "Bogey";
      }
      else{
         return "Error";
      }
   }
   
   // 未使用但用于测试的方法
   public static void main(String[] args) {
   }
}

当我运行测试时,除了最后一个测试外,其他测试都得到了正确的结果。

但这个逻辑对我来说不太合理,因为它应该使我的条件语句无效。因为1 > 2 + 1是假的,所以它应该输出一个错误。为什么我的程序输出柏忌呢?

英文:

Hey guys so I am trying to do a HW question for a java class and I am having some trouble understanding the logic. This is the question they asked of me:

Golf scores record the number of strokes used to get the ball in the hole. The expected number of strokes varies from hole to hole and is called par (i.e. 3, 4 or 5). Each score has a cute name based on the actual strokes taken compared to par. Return "Eagle" if strokes is two less than par. Return "Birdie" if strokes is one less than par. Return "Par" if par matches strokes exactly. Return "Bogey" if strokes is one more than par. Return "Error" if par is not 3, 4 or 5.

This is my code:

public class Main {
   
   public String golfScore(int par, int strokes){
      
      if(strokes &lt;= par - 2){
         return &quot;Eagle&quot;;
      }
      else if(strokes &lt;= par -1){
         return &quot;Birdie&quot;;
      }
      else if(strokes == par){
         return &quot;Par&quot;;
      }
      else if(strokes &gt; par + 1){
         return &quot;Bogey&quot;;
      }
      else{
         return &quot;Error&quot;;
      }
   }
   
   // this method not used but needed for testing
   public static void main(String[] args) {
   }
}

When I run the results I get correct for everything except for the last test.

But this logic doesn't make sense to me cause it should be invalidating my if statement. Cause 1 > 2 + 1 is false therefore it should be outputting an error. Why is my program outputting bogey?

答案1

得分: 1

SOLVED!感谢ChrisForrence!

if ((par != 3) && (par != 4) && (par != 5)){
     return "错误";
  }
  else if(strokes <= par - 2){
     return "老鹰";
  }
  else if(strokes <= par - 1){
     return "小鸟";
  }
  else if(strokes == par){
     return "标准杆";
  }
  else{
     return "柏忌";
  } 
}
英文:

SOLVED! Thanks to ChrisForrence!

if ((par != 3) &amp;&amp; (par != 4) &amp;&amp; (par != 5)){
     return &quot;Error&quot;;
  }
  else if(strokes &lt;= par - 2){
     return &quot;Eagle&quot;;
  }
  else if(strokes &lt;= par -1){
     return &quot;Birdie&quot;;
  }
  else if(strokes == par){
     return &quot;Par&quot;;
  }
  else{
     return &quot;Bogey&quot;;
  } 

}

答案2

得分: 0

如果有人在寻找这个问题的解决方案,但是用 C++ 编写,那么以下是解决方案。

#include <iostream>
using namespace std;

int main() {

    int par;
    int strokes;

    cin >> par >> strokes;

    if ((par != 3) && (par != 4) && (par != 5)){
        cout << "Error" << endl;
    }
    else if (strokes == par - 2){
        cout << "Eagle" << endl;
    }
    else if (strokes == par - 1){
        cout << "Birdie" << endl;
    }
    else if (strokes == par){
        cout << "Par" << endl;
    }
    else if (strokes == par + 1){
        cout << "Bogey" << endl;
    }
    else{
        cout << "None" << endl;
    }

    return 0;
}
英文:

If anyone was looking for the solution for this problem but written in C++, here it is.

#include &lt;iostream&gt;
using namespace std;

int main() {

	int par;
	int strokes;

	cin &gt;&gt; par &gt;&gt; strokes;

	if ((par != 3) &amp;&amp; (par != 4) &amp;&amp; (par != 5)){
        cout &lt;&lt; &quot;Error&quot; &lt;&lt; endl;
	}
    else if (strokes == par - 2){
        cout &lt;&lt; &quot;Eagle&quot; &lt;&lt; endl;
    }
    else if (strokes == par - 1){
        cout &lt;&lt; &quot;Birdie&quot; &lt;&lt; endl;
    }
    else if (strokes == par){
        cout &lt;&lt; &quot;Par&quot; &lt;&lt; endl;
    }
    else if (strokes == par + 1){
        cout &lt;&lt; &quot;Bogey&quot; &lt;&lt; endl;
    }
    else{
        cout &lt;&lt; &quot;None&quot; &lt;&lt; endl;
    }


    return 0;
}

huangapple
  • 本文由 发表于 2020年10月6日 06:19:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/64216960.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定