如何解决我根本无法输出第二行否则,如果少于60行
我有;将运算符从
** 说明 扩展模板代码以分析学生的考试成绩并提供建议,如下所示:
(1)添加代码以将测试分数读入分数变量。
然后评估输入的分数,并使用决策语句显示一些输出。 输入的分数等于或大于80时,输出: 做得好 否则,输出: 研究更多 用换行符结束输出。
(2)扩展程序以添加更多支票。
输入的分数低于60时,输出: 去办公时间寻求帮助 在下一行,再次以换行符结束。
我的密码 *********** ********************************
// 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");
}
}
}
解决方法
按照您编写的方式,程序将打印“进一步研究” 或“并在办公时间寻求帮助”。
更好的编码方式是:
if (score < 80) {
System.out.println("Study more");
}
if (score < 60) {
System.out.println("and go to office hours for help");
}
这样,如果分数
如果您想使用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");
}
}
,
您在'elseif'中的代码将不会执行,因为您在'if'语句中已经有一个条件,该条件满足所有低于80的值。您可以尝试遵循
if ($score <=80 && $score >= 60) {
// Do something
} elseif($Score < 60 ) {
// Do something
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。