我将操作符从<更改为<=,将语句从“else”更改为“if”,没有任何东西可以将其输出。我是一个非常新的编程员,只有几个星期的课在我的腰带下,真的需要一些帮助,我在这里错过了什么。谢谢您能提供的任何帮助。我已经提供了指令和代码。
**Instructions扩展模板代码以分析学生的考试成绩并给出建议,如下所示:
(1)在分数变量中添加读取测试分数的代码。
然后对输入的分数进行评估,并使用决策语句显示一些输出。当输入的分数为80分或以上时,输出:做得好,否则,输出:用换行符学习更多的输出。
(2)扩展程序,增加一次检查。
当输入的分数低于60分时,输出:并在下一行到办公时间寻求帮助,并以换行符结束。
我的CODE***************************************
// Author: Chelsie McFall
import java.util.Scanner;
public class ScoreInterpreter {
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
int score = 0;
System.out.println("Enter your test score:");
score = scnr.nextInt();
if (score >= 80) {
System.out.println("Doing well");
}
if (score < 80) {
System.out.println("Study more");
}
else if (score < 60) {
System.out.println("and go to office hours for help");
}
}
}发布于 2020-09-13 01:57:55
按照你写的方式,这个程序会打印“多学习”或者“去办公室寻求帮助”。
编写代码的更好方法是:
if (score < 80) {
System.out.println("Study more");
}
if (score < 60) {
System.out.println("and go to office hours for help");
}这样,如果分数<80分,它就会打印“学习更多”,如果分数也是<60分,它就会打印“上班时间求助”。
如果您想使用else,可以这样做:
if (score >= 80) {
System.out.println("Doing well");
} else {
/* If we've gotten here, we know the score is < 80 */
System.out.println("Study more");
if (score < 60) {
System.out.println("and go to office hours for help");
}
}发布于 2020-09-13 12:38:47
您的代码将不会被执行,因为您已经在'if‘语句中有一个条件,满足所有低于80的值。你可以试着跟着
if ($score <=80 && $score >= 60) {
// Do something
} elseif($Score < 60 ) {
// Do something
}https://stackoverflow.com/questions/63864984
复制相似问题