[Java in NetBeans] Lesson 09. Switch / If-Else Ladder
这个课程的参考视频和图片来自youtube。
主要学到的知识点有:
1. Nested If-else statement (if-else ladder)
/**
* 90 and above ==> A
* 80 up to 90 ==> B
* 70 up to 80 ==> C
* 60 up to 70 ==> D
* below 60 ==> F
*/ if (grade >= 90)
{
gradeLetter = "A";
}
else if (grade >= 80)
{
gradeLetter = "B";
}
else if (grade >= 70)
{
gradeLetter = "C";
}
else if (grade >= 60)
{
gradeLetter = "D";
}
else
{
gradeLetter = "F";
}
2. Switch
switch (month)
{
case 1:
result = "January";
break;
case 2:
result = "February";
break;
case 3:
result = "March";
break;
default:
result = "Unknown;
break;
}
3. Enumerations
- An enumeration custom data type that enables a variable to be a set of predefined constants of specific value
- Enums are declared like a class
Cannot assign to enum class. Grade g = 78.0; is not allowed.
Cannot create an object from enum class. Grade g = new Grade(88); is not allowed.
This is a enum class of grades.
/**
*
* Java enum representation for grades, based on the following scale:
* 90 and above ==> A
* 80 up to 90 ==> B
* 70 up to 80 ==> C
* 60 up to 70 ==> D
* below 60 ==> F
*/
public enum Grade { A (90.0), B(80.0), C(70.0), D(60.0), F(0.0); // here will call the constructor private final double minimumScore; /**
* Write a constructor for grade with minimum score for that grade
* @param minimumScore lowest acceptable score for that grade
*/
Grade (double minimumScore) {
this.minimumScore = minimumScore;
} /**
* Return the minimum score for the letter grade
* @return lowest score fort achieving that grade
*/
public double Min() {
return this.minimumScore;
} }
If we have the enum class above, then we can rewrite the example in No.1 at beginning.
if (grade >= Grade.A.Min())
{
gradeLetter = Grade.A;
}
else if (grade >= Grade.B.Min())
{
gradeLetter = Grade.B;
}
else if (grade >= Grade.C.Min())
{
gradeLetter = Grade.C;
}
else if (grade >= Grade.D.Min())
{
gradeLetter = Grade.D;
}
else
{
gradeLetter = Grade.F;
}
[Java in NetBeans] Lesson 09. Switch / If-Else Ladder的更多相关文章
- [Java in NetBeans] Lesson 07. JavaDoc and Unit Tests
这个课程的参考视频和图片来自youtube. 主要学到的知识点有: 1. organize code into packages Create a package if want to make th ...
- [Java in NetBeans] Lesson 00. Getting Set-up for Learning Java
这个课程的参考视频在youtube. 主要学到的知识点有: set up needs Java SE JDK, NetBeans IDE class name should be the same l ...
- [Java in NetBeans] Lesson 17. File Input/Output.
这个课程的参考视频和图片来自youtube. 主要学到的知识点有: We want to handle the bad Error. (e.g bad input / bugs in program) ...
- [Java in NetBeans] Lesson 16. Exceptions.
这个课程的参考视频和图片来自youtube. 主要学到的知识点有: We want to handle the bad Error. (e.g bad input / bugs in program) ...
- [Java in NetBeans] Lesson 15. Sorting and Searching.
这个课程的参考视频和图片来自youtube. 主要学到的知识点有: Build in functions in java.util.Collections Need to implement a co ...
- [Java in NetBeans] Lesson 06. Custom classes
这个课程的参考视频和图片来自youtube. 主要学到的知识点有: Constructors: A special method called when an object of the class ...
- [Java in NetBeans] Lesson 05. Method/function
这个课程的参考视频和图片来自youtube. 主要学到的知识点有: Define a method:(motivation: write one time, but use it many times ...
- [Java in NetBeans] Lesson 04. Class / Objects
这个课程的参考视频和图片来自youtube. 主要学到的知识点有: Class: Blueprint for an object. (e.g. dog is a class) Object: cust ...
- [Java in NetBeans] Lesson 01. Java Programming Basics
这个课程的参考视频在youtube. 主要学到的知识点有: Create new project, choose Java Application. one .jar file/ package(.j ...
随机推荐
- ThinkPHP框架 AJAX方法返回 AJAX实现分页例子:
在模块控制器Controller文件夹里创建一个 FenyeController.class.php控制器 <?php namespace Admin\Controller; use Think ...
- Fis3构建迁移Webpack之路
Webpack从2015年9月第一个版本横空初始至今已逾2载.它的出现,颠覆了一大批主流构建如Ant.Grunt和Gulp等等.腾讯NOW直播IVWEB团队之前一直采用Fis构建,本篇文章主要介绍从F ...
- This function has none of Deterministic,no sql,or reads sql data in its declaration and binary logging is enabled(you *might* want to use the less safe log_bin_trust_function_creators variable
This function has none of Deterministic,no sql,or reads sql data in its declaration and binary loggi ...
- webuploader 跨域上传demo(还没有写记录一下)
webuploader 跨域上传demo(还没有写记录一下)
- 转载:浅析@PathVariable 和 @RequestParam
在网上看了一篇很好的文章,讲的很清楚明了,说到了点子上(转自:https://blog.csdn.net/chuck_kui/article/details/55506723): 首先 上两个地址: ...
- vue中的iviewUI导出1W条列表数据每次只导出2000条的逻辑
导出弹窗的html <template> <Modal v-model="exportModal" width=400 :closable="false ...
- imu_tk标定算法原理
imu_tk代码地址 https://bitbucket.org/alberto_pretto/imu_tk II. S ENSOR E RROR M ODEL 对于理想的IMU,加速度计三元组的3个 ...
- [DPI][TCP] linux API的接口如何控制urgent包的收发
做DPI,写协议栈的时候,处理到了urgent数据包.突然好奇应用层是如何控制发出urgent包的呢?而接收端又是如何知道,接受到了urgent包的呢? man 7 tcp,中有如下一段: TCP s ...
- sqlserver binary varbinary image 的区别
sqlserver binary varbinary image 的区别 binary 固定长度的二进制数据,其最大长度为 8,000 个字节. varbinary 可变长度的二进制数 ...
- ipv6的校验格式
ipv6的校验格式: ^(?:[0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}$