英文:
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("努力加油")
}
}
这段代码存在一个问题,即条件判断部分的符号错误。应该使用 <=
表示小于等于,而不是 <=
。同样,>=
应该用 <=
替代。请将这些符号进行修正后再次尝试运行程序。
英文:
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("Enter your marks")
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("Work harder")
}
}
答案1
得分: 24
你有一个逻辑问题。
将
if (100 <= x) && (x<=75){
改为
if 75 <= x && x <= 100 { // 这里的数字按从小到大的顺序排列
因为一个数字不能同时大于100和小于75。
其他行也是一样的道理。
请注意,你可以减少比较次数。假设你最初测试数字是否小于100,那么你就不必在测试它是否小于75之后再测试一次。
一个典型的Go代码可能会使用switch
而不是所有这些if/else
。请参阅文档中的switch。下面是使用switch
的示例代码:
switch {
case x > 100:
fmt.Println("恭喜!") // 你忘记了这个
case x >= 75:
fmt.Println("D1")
case x >= 70:
fmt.Println("D2")
case x >= 65:
fmt.Println("C3")
case x >= 60:
fmt.Println("C4")
case x >= 55:
fmt.Println("C5")
case x >= 50:
fmt.Println("C6")
case x >= 45:
fmt.Println("P7")
default:
fmt.Println("努力工作")
}
最后一点说明:这种类型的切换代码很少出现,因为通常阈值和相关的注释是存储为数据的,例如在一个struct
中。
英文:
You have a logic problem.
Change
if (100 <= x) && (x<=75){
to
if 75 <= x && x <= 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 > 100:
fmt.Println("Congrats!") // you forgot this one
case x >= 75:
fmt.Println("D1")
case x >= 70:
fmt.Println("D2")
case x >= 65:
fmt.Println("C3")
case x >= 60:
fmt.Println("C4")
case x >= 55:
fmt.Println("C5")
case x >= 50:
fmt.Println("C6")
case x >= 45:
fmt.Println("P7")
default:
fmt.Println("Work harder")
}
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 <= x) && (x<=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("Enter your marks")
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("Work harder")
}
答案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 "fmt"
func main(){
var x int
fmt.Println("Enter your marks")
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("Work harder")
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论