if else statment in go lang

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

if else statment in go lang

问题

有人可以帮我调试这个程序吗?每次输入后只会执行 else 部分。这是一个用于给学生评分的程序,学生输入一个分数,然后显示对应的等级。

func main(){
    var x int
    fmt.Println("请输入你的分数")

    fmt.Scanf("%d",&x)

    if (100 <= x) && (x<=75){
        fmt.Println("D1")
    }else if (74 <= x)&&(x <= 70){
        fmt.Println("D2")
    }else if (69 <= x )&&(x<=65){
        fmt.Println("C3")
    }else if (64 <= x)&&(x <= 60){
        fmt.Println("C4")
    }else if (59 <= x)&&(x <= 55){
        fmt.Println("C5")
    }else if (54 <= x)&&( x<= 50){
        fmt.Println("C6")
    }else if (49 <= x )&&(x<= 45){
        fmt.Println("P7")
    }else{
        fmt.Println("努力加油")
    }
}

这段代码存在一个问题,即条件判断部分的符号错误。应该使用 <= 表示小于等于,而不是 &lt;=。同样,>= 应该用 <= 替代。请将这些符号进行修正后再次尝试运行程序。

英文:

Could someone help me debug this program, only the else part is processed on every input.
This id a program for grading students. a student inputs a mark and the the grade is displayed

func main(){
	var x int
	fmt.Println(&quot;Enter your marks&quot;)

	fmt.Scanf(&quot;%d&quot;,&amp;x)

	if (100 &lt;= x) &amp;&amp; (x&lt;=75){
		fmt.Println(&quot;D1&quot;)
	}else if (74 &lt;= x)&amp;&amp;(x &lt;= 70){
		fmt.Println(&quot;D2&quot;)
	}else if (69 &lt;= x )&amp;&amp;(x&lt;=65){
		fmt.Println(&quot;C3&quot;)
	}else if (64 &lt;= x)&amp;&amp;(x &lt;= 60){
		fmt.Println(&quot;C4&quot;)
	}else if (59 &lt;= x)&amp;&amp;(x &lt;= 55){
		fmt.Println(&quot;C5&quot;)
	}else if (54 &lt;= x)&amp;&amp;( x&lt;= 50){
		fmt.Println(&quot;C6&quot;)
	}else if (49 &lt;= x )&amp;&amp;(x&lt;= 45){
		fmt.Println(&quot;P7&quot;)
	}else{
		fmt.Println(&quot;Work harder&quot;)
	}
}

答案1

得分: 24

你有一个逻辑问题。

if (100 &lt;= x) &amp;&amp; (x&lt;=75){

改为

if 75 &lt;= x &amp;&amp; x &lt;= 100 { // 这里的数字按从小到大的顺序排列

因为一个数字不能同时大于100和小于75。

其他行也是一样的道理。

请注意,你可以减少比较次数。假设你最初测试数字是否小于100,那么你就不必在测试它是否小于75之后再测试一次。

一个典型的Go代码可能会使用switch而不是所有这些if/else。请参阅文档中的switch。下面是使用switch的示例代码:

switch {
case x &gt; 100:
    fmt.Println("恭喜!") // 你忘记了这个
case x &gt;= 75:
    fmt.Println("D1")
case x &gt;= 70:
    fmt.Println("D2")
case x &gt;= 65:
    fmt.Println("C3")
case x &gt;= 60:
    fmt.Println("C4")
case x &gt;= 55:
    fmt.Println("C5")
case x &gt;= 50:
    fmt.Println("C6")
case x &gt;= 45:
    fmt.Println("P7")
default:
    fmt.Println("努力工作")
}

最后一点说明:这种类型的切换代码很少出现,因为通常阈值和相关的注释是存储为数据的,例如在一个struct中。

英文:

You have a logic problem.

Change

if (100 &lt;= x) &amp;&amp; (x&lt;=75){

to

if 75 &lt;= x &amp;&amp; x &lt;= 100 { // numbers here are ordered from smallest to greatest

because a number can't be greater than 100 and smaller than 75.

And it's the same for the other lines of course.

Note that you could make less comparisons. Suppose you test if the number is smaller than 100 initially, then you don't have to test if it's smaller than 75 just after you tested it's smaller than 75.

A typical Go code would probably have a switch here instead of all those if/else. See switch in the documentation. Here's how it could be written with a switch :

switch {
case x &gt; 100:
	fmt.Println(&quot;Congrats!&quot;) // you forgot this one
case x &gt;= 75:
	fmt.Println(&quot;D1&quot;)
case x &gt;= 70:
	fmt.Println(&quot;D2&quot;)
case x &gt;= 65:
	fmt.Println(&quot;C3&quot;)
case x &gt;= 60:
	fmt.Println(&quot;C4&quot;)
case x &gt;= 55:
	fmt.Println(&quot;C5&quot;)
case x &gt;= 50:
	fmt.Println(&quot;C6&quot;)
case x &gt;= 45:
	fmt.Println(&quot;P7&quot;)
default:
	fmt.Println(&quot;Work harder&quot;)
}

A last comment : This type of switching code rarely occurs because normally the thresholds and related notes are stored as data, for example in a struct.

答案2

得分: 1

你的 IF 语句的条件是:

如果 100 小于 x(这意味着 x 必须大于 100)
并且
x 小于(或等于)75
执行以下操作 -

x 永远不会同时大于 100 和小于 75,因此它总是执行 ELSE ...

英文:

Your IF statement says:

if 100 is less than x (which means x has to be greater than 100)
AND
x less than (or equal to) 75
do this--

x will never be greater than 100 AND less than 75, so it always does the ELSE ...

答案3

得分: -1

这段代码中提供的所有if和if else语句在逻辑上是错误的。

例如:

if (100 <= x) && (x<=75)

在这里,如果(100 <= x)为假,编译器甚至不会考虑下一个条件。这被称为短路,即条件语句的惰性求值。

这也适用于OR条件,即如果第一个条件为真,则不会评估第二个条件。

所以根据你写的代码,理想的解决方案将是:

func main() {
    var x int
    fmt.Println("Enter your marks")

    fmt.Scanf("%d", &x)

    if (x >= 75) && (x <= 100) {
        fmt.Println("D1")
    } else if (x >= 70) && (x <= 74) {
        fmt.Println("D2")
    } else if (x >= 65) && (x <= 69) {
        fmt.Println("C3")
    } else if (x >= 60) && (x <= 64) {
        fmt.Println("C4")
    } else if (x >= 55) && (x <= 59) {
        fmt.Println("C5")
    } else if (x >= 50) && (x <= 54) {
        fmt.Println("C6")
    } else if (x >= 45) && (x <= 49) {
        fmt.Println("P7")
    } else {
        fmt.Println("Work harder")
    }
}

请注意,我已经将条件语句中的<=>=进行了修正,并调整了条件的顺序,以确保正确的逻辑判断。

英文:

all if and if else statements provided in this code are logically incorrect

example:

 if (100 &lt;= x) &amp;&amp; (x&lt;=75) 

here if (100 <= x) is false, the compiler will not even consider the next condition.
This is called SHORT CIRCUIT, the lazy evaluation of condition statements.

this is also applicable to OR conditions i.e in case the first condition is true the second condition will not be evaluated.

So according to the code you have written, the ideal solution will be

func main() {
	var x int
	fmt.Println(&quot;Enter your marks&quot;)

	fmt.Scanf(&quot;%d&quot;, &amp;x)

	if (100 &gt;= x) &amp;&amp; (x &gt;= 75) {
		fmt.Println(&quot;D1&quot;)
	} else if (74 &gt;= x) &amp;&amp; (x &gt;= 70) {
		fmt.Println(&quot;D2&quot;)
	} else if (69 &gt;= x) &amp;&amp; (x &gt;= 65) {
		fmt.Println(&quot;C3&quot;)
	} else if (64 &gt;= x) &amp;&amp; (x &gt;= 60) {
		fmt.Println(&quot;C4&quot;)
	} else if (59 &gt;= x) &amp;&amp; (x &gt;= 55) {
		fmt.Println(&quot;C5&quot;)
	} else if (54 &gt;= x) &amp;&amp; (x &gt;= 50) {
		fmt.Println(&quot;C6&quot;)
	} else if (49 &gt;= x) &amp;&amp; (x &gt;= 45) {
		fmt.Println(&quot;P7&quot;)
	} else {
		fmt.Println(&quot;Work harder&quot;)
	}

答案4

得分: -3

感谢你的帮助,最终我搞定了。希望它能在将来的某个时候帮助到其他人。

package main

import "fmt"

func main() {
    var x int
    fmt.Println("输入你的分数")
    fmt.Scanf("%d", &x)
    if (75 <= x) && (x <= 100) {
        fmt.Println("D1")
    } else if (70 <= x) && (x <= 74) {
        fmt.Println("D2")
    } else if (65 <= x) && (x <= 69) {
        fmt.Println("C3")
    } else if (60 <= x) && (x <= 64) {
        fmt.Println("C4")
    } else if (55 <= x) && (x <= 59) {
        fmt.Println("C5")
    } else if (50 <= x) && (x <= 54) {
        fmt.Println("C6")
    } else if (45 <= x) && (x <= 49) {
        fmt.Println("P7")
    } else {
        fmt.Println("努力学习")
    }
}

以上是代码的翻译部分。

英文:

Thankx finaly got with your help. hope it helps someone else some time in future
> package main

import &quot;fmt&quot;

func main(){
var x int
fmt.Println(&quot;Enter your marks&quot;)
fmt.Scanf(&quot;%d&quot;,&amp;x)
if (75&lt;= x) &amp;&amp; (x&lt;=100){
      fmt.Println(&quot;D1&quot;)
      }else if (70 &lt;= x)&amp;&amp;(x &lt;= 74){
       fmt.Println(&quot;D2&quot;)
      }else if (65 &lt;= x )&amp;&amp;(x&lt;=69){
      fmt.Println(&quot;C3&quot;)
      }else if (60 &lt;= x)&amp;&amp;(x &lt;= 64){
      fmt.Println(&quot;C4&quot;)
      }else if (55 &lt;= x)&amp;&amp;(x &lt;= 59){
      fmt.Println(&quot;C5&quot;)
      }else if (50 &lt;= x)&amp;&amp;( x&lt;= 54){
      fmt.Println(&quot;C6&quot;)
      }else if (45 &lt;= x )&amp;&amp;(x&lt;= 49){
      fmt.Println(&quot;P7&quot;)
      }else{
        fmt.Println(&quot;Work harder&quot;)
      }
      
      }

huangapple
  • 本文由 发表于 2013年10月10日 00:23:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/19277489.html
匿名

发表评论

匿名网友

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

确定