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

go评论56阅读模式

Golf return output logic issue

# 问题

``````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) {
}
}
``````

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

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

``````#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;
}
``````

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

go 58

go 58

go 61

go 53