[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 ...
随机推荐
- A - Cable master
Inhabitants of the Wonderland have decided to hold a regional programming contest. The Judging Commi ...
- xml和json格式输出
<?php class Response{ const JSON ='json'; /* * 按综合方式输出通信数据 * @param integer $ ...
- linux下内核的配置和编译(2017-1-17)
4.1 什么是内核 内核是操作系统内核的简称,内核负责实现操作系统的核心功能,包括资源管理模块,譬如内 存管理.调度系统等等.内核不包括应用程序.对于 linux 内核而言全世界是有一份内核,我们可 ...
- python中的日志,logger用法
python中自带logger模块,实现方法有两种,一般使用第二种,更灵活 方法一: import logging # 通过logging.basicConfig完成 logging.basicCon ...
- RabbitMQ的Vhost,Exchange,Queue原理分析
Vhost分析 RabbitMQ的Vhost主要是用来划分不同业务模块.不同业务模块之间没有信息交互. Vhost之间相互完全隔离,不同Vhost之间无法共享Exchange和Queue.因此Vhos ...
- iOS之分类(category)
1.分类(category)的作用 1.1作用:可以在不修改原来类的基础上,为一个类扩展方法.1.2最主要的用法:给系统自带的类扩展方法. 2.分类中能写点啥? 2.1分类中只能添加“方法”,不能增加 ...
- [administrator][driver] driverctl 是如何在udev上层管理设备驱动的
https://gitlab.com/driverctl/driverctl driverctl 处于 kernel 与 udev做设备与驱动管理的上层. 理解什么叫override是本文的核心内容. ...
- [cloud][sdn] openstack openflow opendaylight openvswitch
https://www.quora.com/What-is-the-relation-between-OpenStack-OpenDaylight-OpenFlow-and-Open-vSwitch- ...
- Flink – JobManager.submitJob
JobManager作为actor, case SubmitJob(jobGraph, listeningBehaviour) => val client = sender() val jobI ...
- 清理solaris /var/mail/下的邮件文件
我服务器上/var/mail下的各个用户的邮件日志非常大,占用空间已经有95%了,我想清除掉,是否可以直接删除/var/mail的各个日志??删除后系统是否可以自动生成? 应该可以直接删除/var/m ...