Hello World

Hello Young


  • 首页

  • 分类

  • 归档

  • 标签

  • 关于

  • 搜索

Java 学习指南_学习Java:基础-类-声明类

发表于 2017-09-28 | 分类于 java

声明类

你已经看到的类按照下面的方式定义:

1
2
3
4
class MyClass {
// field, constructor, and
// method declarations
}

这就是类的声明。类体class body(两个花括号之内的区域)包含了用于对象生命周期中由类创建时所需的所有代码:用于初始化一个对象的构造函数,用来提供类以及其实例对象的状态的字段/域,以及实现了类和其实例对象行为的方法。

之前是课程声明了一个最基本的类 。只包含了声明类所必须的组件。你可以为类提供更多信息,例如在开始定义类的时候声明类的父类,以及是否实现了那些接口等等。例如,

1
2
3
4
class MyClass extends MySuperClass implements YourInterface {
// field, constructor, and
// method declarations
}

意味着类MyClass是MySuperClass 的子类并且它实现了YourInterface接口。

你同样可以在最开始的比方增加限定修饰符public或者private-因此你会发现定义类的起始行可以变得相当复杂。限定修饰符public和private定义了其他类可以访问Myclass的什么内容,将会在本科后续内容讨论。关于接口和继承的课程会解释如何以及为什么使用extends以及implements关键字类定义类。现在你不需要担心这些复杂的问题。

通常情况的,类的声明包含了以下部分,按照顺序排列为:

  1. 限定修饰符public,private以及后续你会碰到的其他内容。
  2. 类名,按照约定需要大写首字母
  3. 类的父类,前置一个extends关键字。一个类只可以扩展自一个父类。
  4. 由逗号分割的实现接口的列表,前置一个implements关键字。一个类可以implement实现多个接口。
  5. 类体body,由{},花括号包围。

Java 学习指南_学习Java:基础-类与对象

发表于 2017-09-27 | 分类于 java

课程:类与对象

根据你已经掌握的java编程语言基础知识,你可以学习写你自己的类。本课程中你会找到定义类的相关信息。包括定义成员变量,方法,以及构造函数。

你会学习如何使用你的类创建对象,以及如何使用创建的对象。

本课程同样覆盖了嵌套类以及枚举类。

类

这一部分将为你剖析一个类的结构,展示如定义成员变量,方法,以及构造函数。

对象

这一部分包括了如何创建以及使用对象。你会学习如何实例化一个对象,以及实例化之后如何使用点操作符号访问对象的实例变量以及方法。

类详细介绍

这一部分依据上一节的类使用参考以及点操作符号的知识介绍类的更多方面:从方法中返回值,this关键字,类与实例成员,以及访问控制。

嵌套类(内部类)

静态嵌套类,内部类,匿名内部类,局部类,以及lambda表达式都包含在内,同时也包含了何时使用某种方法的讨论。

枚举类型

本节包括了枚举类型,一种语序你定义和使用常量集合的特殊类。

Java 学习指南_学习Java:基础-类

发表于 2017-09-27 | 分类于 java

类

在标题为 面向对象编程概念 的课程中介绍面向对象编程概念时使用了一个bicycle自行车类作为例子,包含赛车,山地车作为子类。这里有一个简单的代码示例作为Bicycle类的一种可能实现,让你对类的定义有一个大致的了解。接下来的课程中将会一步一步的复习和解释类的定义,现在先不要关注太多的细节。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
public class Bicycle {
// the Bicycle class has
// three fields
public int cadence;
public int gear;
public int speed;
// the Bicycle class has
// one constructor
public Bicycle(int startCadence, int startSpeed, int startGear) {
gear = startGear;
cadence = startCadence;
speed = startSpeed;
}
// the Bicycle class has
// four methods
public void setCadence(int newValue) {
cadence = newValue;
}
public void setGear(int newValue) {
gear = newValue;
}
public void applyBrake(int decrement) {
speed -= decrement;
}
public void speedUp(int increment) {
speed += increment;
}
}

定义一i个Bicycle类的子类MountainBikel类可能如下所示:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public class MountainBike extends Bicycle {
// the MountainBike subclass has
// one field
public int seatHeight;
// the MountainBike subclass has
// one constructor
public MountainBike(int startHeight, int startCadence,
int startSpeed, int startGear) {
super(startCadence, startSpeed, startGear);
seatHeight = startHeight;
}
// the MountainBike subclass has
// one method
public void setHeight(int newValue) {
seatHeight = newValue;
}
}

MountainBike 类继承了自行车类的所有fields字段/域 以及方法,并且添加了field seatHeight,以及一个设置它的方法(山地车拥有可以根据地形调整高度的座位)。

Java 学习指南_学习Java:基础-控制流程-问答与练习

发表于 2017-09-27 | 分类于 java

控制流程为题与练习

问题

  1. java编程语言支持的最基本的控制流程语句是 ? 语句.
  2. ?语句允许一定数量的可能执行路径。
  3. ?语句与while语句相似,但是在执行代码块的?计算控制表达式的布尔值.
  4. 问题: 如何使用for语句写一个无限循环?
  5. 问题:如何用while语句写一个无限循环?

练习

  1. 观察以下代码片段.

    1
    2
    3
    4
    5
    6
    if (aNumber >= 0)
    if (aNumber == 0)
    System.out.println("first string");
    else
    System.out.println("second string");
    System.out.println("third string");
    1. 练习:如果aNumber的值为3输出的结果可能是什么?

    2. 练习:如果aNumber的值为3,程序的输出是什么?你的预测是什么?解释为什么输出这个结果,换句话说,代码片段的控制流程是什么?

    3. 练习:

      仅使用空格和换行符重新格式化代码使代码放容易理解.

    4. 练习:

      使用{}使代码更加清晰易读,已减少在后期维护过程中可能出现的错误.

阅读全文 »

Java 学习指南_学习Java:基础-控制流程-总结

发表于 2017-09-27 | 分类于 java

控制流程总结

if-then语句时所有控制流程中最基本的语句。当一个特定的测试验证为true时告诉程序执行确定的某一部分代码。 if-then-else 语句为if引用计算结果为false时提供第二个执行选择。switch语句允许一定数量的可能执行路径。while与do-while语句当特定的条件为true循环执行某一代码块,区别为while在执行前计算控制表达式,do-while在执行后计算控制表达式,因此在do代码块之内的代码至少被执行一次。for语句为迭代一定范围内的值提供了一种简介的方法,for语句有两种形式,其中增强for语句用来迭代一个集合或者数组。

Java 学习指南_学习Java:基础-控制流程-分支语句

发表于 2017-09-27 | 分类于 java

分支语句

break 语句

break 语句有两种形式,带标签以及不带标签,你之前在switch语句的讨论中已经见到国不带标签的break语句。你同样可以使用不带标签break语句来终止一个 for, while, 或者 do-while 循环,就像下面示例程序展示的一样 BreakDemo :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
class BreakDemo {
public static void main(String[] args) {
int[] arrayOfInts =
{ 32, 87, 3, 589,
12, 1076, 2000,
8, 622, 127 };
int searchfor = 12;
int i;
boolean foundIt = false;
for (i = 0; i < arrayOfInts.length; i++) {
if (arrayOfInts[i] == searchfor) {
foundIt = true;
break;
}
}
if (foundIt) {
System.out.println("Found " + searchfor + " at index " + i);
} else {
System.out.println(searchfor + " not in the array");
}
}
}

这个程序在一个数组中搜索数字12。break语句,当找到这个值时终止循环。控制流程跳转至for循环结束以后的语句。程序的输出如下:

1
Found 12 at index 4

一个无标签的break语句终止最内层的 switch, for, while, 或者 do-while 语句,但是一个带标签的 break 语句可以终止外部的语句。下面的程序, BreakWithLabelDemo,和前一个程序相似,但是使用了嵌套for循环在二维数组中搜索一个值.当找到这个值时,一个带标签的break语句终止外部的循环(标签’search’):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
class BreakWithLabelDemo {
public static void main(String[] args) {
int[][] arrayOfInts = {
{ 32, 87, 3, 589 },
{ 12, 1076, 2000, 8 },
{ 622, 127, 77, 955 }
};
int searchfor = 12;
int i;
int j = 0;
boolean foundIt = false;
search:
for (i = 0; i < arrayOfInts.length; i++) {
for (j = 0; j < arrayOfInts[i].length;
j++) {
if (arrayOfInts[i][j] == searchfor) {
foundIt = true;
break search;
}
}
}
if (foundIt) {
System.out.println("Found " + searchfor + " at " + i + ", " + j);
} else {
System.out.println(searchfor + " not in the array");
}
}
}

输出如下.

1
Found 12 at 1, 0

break语句终止标记的语句;并不是控制流程跳转至标签,而是跳转至标签语句(块)之后的语句。

continue 语句

continue跳过一个 for, while , 或者 do-while 循环的当前迭代.无标签的continue语句跳至最内层循环体的结尾,然后球的控制循环boolean表达式的值。下面的程序, ContinueDemo , 检查字符串String的每一个字符,统计遇到的字母”p”的数量。如果遇到的字符不是’’p”,continue语句控制流程掠过剩下的循环直接进行下一个字符的检测。如果该字符是”p”,程序为基数增加1.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class ContinueDemo {
public static void main(String[] args) {
String searchMe = "peter piper picked a " + "peck of pickled peppers";
int max = searchMe.length();
int numPs = 0;
for (int i = 0; i < max; i++) {
// interested only in p's
if (searchMe.charAt(i) != 'p')
continue;
// process p's
numPs++;
}
System.out.println("Found " + numPs + " p's in the string.");
}
}

以下为程序的输出:

1
Found 9 p's in the string.

为了是效果更加明显,试着删掉continue语句然后重新编译运行,计数就会出错,输出35而不是9。

一个带标签的continue语句掠过一个有对应标签的外部循环的当前一次迭代。下面的示例程序,, ContinueWithLabelDemo, 使用嵌套循环搜索一个子字符串在另一个字符串中出现的位置。使用两个嵌套循环是必要的:一个用来遍历子字符串,另一个用来遍历被搜索的字符串。以下程序,ContinueWithLabelDemo, 使用带标签的continue语句来掠过外部循环的迭代。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
class ContinueWithLabelDemo {
public static void main(String[] args) {
String searchMe = "Look for a substring in me";
String substring = "sub";
boolean foundIt = false;
int max = searchMe.length() -
substring.length();
test:
for (int i = 0; i <= max; i++) {
int n = substring.length();
int j = i;
int k = 0;
while (n-- != 0) {
if (searchMe.charAt(j++) != substring.charAt(k++)) {
continue test;
}
}
foundIt = true;
break test;
}
System.out.println(foundIt ? "Found it" : "Didn't find it");
}
}

以下为程序的输出:

1
Found it

return 语句

最后一个分支语句是return语句。return语句存在与当前的方法中,控制流程返回到方法被调用的地方。return语句有两种形式:一种返回一个值,一种不返回。返回值的时候只需要把返回的值(或者是用来计算返回值的表达式)放在return关键字之后。

1
return ++count;

返回值的数据类型必须有方法声明的返回值的数据类型匹配。当方法的声明为void时,需要使用不带返回值的return形式。

1
return;

类与对象 Classes and Objects 课程将会覆盖所有你需要知道的写方法的知识。

Java 学习指南_学习Java:基础-控制流程-for

发表于 2017-09-26 | 分类于 java

for 语句

for语句为迭代一个范围内的值提供了一种简介的方法。开发者经常称之为’for 循环’,因为它重复循环直到满足一个特定条件的方式。for循环的一般形式展示如下:

1
2
3
4
for (initialization; termination;//初始化,终止条件
increment) {//增加
statement(s)
}

当你使用这种版本的for语句时,注意以下几点:

  • 初始化表达式初始化整个循环;它只随着循环开始执行一次.
  • 当终止条件的表达式球的false时,循环终止。
  • 增加表达式在循环的每一次迭代之后被调用,表达式的只增加或减少都时支持的。

下面的程序, ForDemo, 使用常规的for语句输出了1到10:

1
2
3
4
5
6
7
class ForDemo {
public static void main(String[] args){
for(int i=1; i<11; i++){
System.out.println("Count is: " + i);
}
}
}

程序 输出如下:

1
2
3
4
5
6
7
8
9
10
Count is: 1
Count is: 2
Count is: 3
Count is: 4
Count is: 5
Count is: 6
Count is: 7
Count is: 8
Count is: 9
Count is: 10

注意代码时如何在初始化表达式中声明一个变量的。变量的使用范围包含从它声明的地方起到for语句掌控的代码块结尾,因此它也可以在终止条件以及增减表达式中使用。如果控制for语句的变量没有必要出现在循环之外,最好在初始化表达式中声明它。变量名 i j k都是经常用来控制for循环的;在初始化条件中声明控制变量可以限制它的使用范围并减少错误。

for循环的三个表达式时可选的;如下代码可以创建一个死循环:

1
2
3
4
5
// infinite loop
for ( ; ; ) {
// your code goes here
}

for循环还有另外一种设计,用来迭代集合 Collections 与数组 arrays 。这种形式有时候也被成为增强for循环,可以用来使循环更加简洁和易读。为了展示,关注如下的数组,持有1到10十个数字:

1
int[] numbers = {1,2,3,4,5,6,7,8,9,10};

下面的程序 EnhancedForDemo, 使用增强for循环迭代并输出以上数组:

1
2
3
4
5
6
7
8
9
class EnhancedForDemo {
public static void main(String[] args){
int[] numbers =
{1,2,3,4,5,6,7,8,9,10};
for (int item : numbers) {
System.out.println("Count is: " + item);
}
}
}

在这个例子中,变量item持有数字数组中的当前值,代码的输出与之前的程序使一致的:

1
2
3
4
5
6
7
8
9
10
Count is: 1
Count is: 2
Count is: 3
Count is: 4
Count is: 5
Count is: 6
Count is: 7
Count is: 8
Count is: 9
Count is: 10

我们推荐在可能的情况下使用这种形式的for循环代替普通for循环。

Java 学习指南_学习Java:基础-控制流程-while & do-while

发表于 2017-09-26 | 分类于 java

while 以及 do-while 语句

当一个特定的条件为true时,while语句持续的执行一段代码块.它的语法形式如下:

The while statement continually executes a block of statements while a particular condition is true. Its syntax can be expressed as:

1
2
3
while (expression) {
statement(s)
}

while语句计算一个必须返回布尔值的表达式的值。如果这个表达式的结果为true,while语句,执行其代码块中的语句,while语句会持续验证表达式的值,并执行它的代码块直到表达式的结果返回false。使用while语句打印出1到10的值可以通过如下的示例程序 WhileDemo 完成:

1
2
3
4
5
6
7
8
9
class WhileDemo {
public static void main(String[] args){
int count = 1;
while (count < 11) {
System.out.println("Count is: " + count);
count++;
}
}
}

你可以像下面的代码一样实现一个无限循环(死循环):

1
2
3
while (true){
// your code goes here
}

Java编程语言同样提供了do-while语句,形式如下:

1
2
3
do {
statement(s)
} while (expression);

do-while 与 while 语句的区别在于 do-while 在代码块的底部计算表达式的布尔值结果而不是在顶部。因此,do代码块中的语句至少执行一次,就像下面的示例代码展示的一样 DoWhileDemo :

1
2
3
4
5
6
7
8
9
class DoWhileDemo {
public static void main(String[] args){
int count = 1;
do {
System.out.println("Count is: " + count);
count++;
} while (count < 11);
}
}

Java 学习指南_学习Java:基础-控制流程-switch 语句

发表于 2017-09-20 | 分类于 java

switch 语句

与 if-then 和 if-then-else 语句不通, switch语句可以有一定数量的可能执行路径。 switch 可以对 byte, short, char, 以及 intp基本数据类型使用. 同样可以对枚举类型使用 (参考 Enum Types), 字符串 String 类使用, 以及基本数据类型的包装类: Character,Byte, Short, 和 Integer (参考 Numbers and Strings).

以下的代码示例中 SwitchDemo, 声明了一个名为mongth的int。代码使用switch语句,根据month的值显示出月份的名字.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
public class SwitchDemo {
public static void main(String[] args) {
int month = 8;
String monthString;
switch (month) {
case 1: monthString = "January";
break;
case 2: monthString = "February";
break;
case 3: monthString = "March";
break;
case 4: monthString = "April";
break;
case 5: monthString = "May";
break;
case 6: monthString = "June";
break;
case 7: monthString = "July";
break;
case 8: monthString = "August";
break;
case 9: monthString = "September";
break;
case 10: monthString = "October";
break;
case 11: monthString = "November";
break;
case 12: monthString = "December";
break;
default: monthString = "Invalid month";
break;
}
System.out.println(monthString);
}
}

本例中, August 被输出.

switch语句的代码体也叫做 switch blocks (switch代码块). switch 代码块中的语句可以用一个或者多个case或者 default标签分类。switch语句运算表达式的值,然后执行匹配的case标签后的所有语句。你同样可以使用if-then-else语句实现以上代码的功能:

1
2
3
4
5
6
7
int month = 8;
if (month == 1) {
System.out.println("January");
} else if (month == 2) {
System.out.println("February");
}
... // and so on

决定使用if-then-else语句还是switch语句取决于可读性以及语句所要测试的表达式。 if-then-else 语句可以用来检验基于值的范围或者条件的表达式,switch语句可以用来检验基于整数,枚举值,以及字符串String对象。

另外一个有趣的点时break语句.每一个break语句终结闭合的 switch 语句,控制流程继续跟随switch代码块的下一语句。break语句时必须的,如果为没有break语句,switch代码块 将会fall through跌穿(整段垮掉):所有匹配的case标签之后的语句都会按照顺序执行,不管随后的case标签后的表达式是否匹配,直到遇见一个break语句。下面的程序 SwitchDemoFallThrough 展示了一个争端垮掉的 switch代码块. 程序显示了整数对应的月份以及一年当中随后的月份:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
public class SwitchDemoFallThrough {
public static void main(String[] args) {
java.util.ArrayList<String> futureMonths =
new java.util.ArrayList<String>();
int month = 8;
switch (month) {
case 1: futureMonths.add("January");
case 2: futureMonths.add("February");
case 3: futureMonths.add("March");
case 4: futureMonths.add("April");
case 5: futureMonths.add("May");
case 6: futureMonths.add("June");
case 7: futureMonths.add("July");
case 8: futureMonths.add("August");
case 9: futureMonths.add("September");
case 10: futureMonths.add("October");
case 11: futureMonths.add("November");
case 12: futureMonths.add("December");
break;
default: break;
}
if (futureMonths.isEmpty()) {
System.out.println("Invalid month number");
} else {
for (String monthName : futureMonths) {
System.out.println(monthName);
}
}
}
}

代码输出结果如下:

1
2
3
4
5
August
September
October
November
December

技术上来说,最后一个break并不时必须的因为流程执行跌出switch语句。推荐使用一个break语句,这样可以使修改代码更加容易并且不容出错。default部分代码处理么有被任何一个case标签表明的其他任何值。

以下的示例代码中 SwitchDemo2, 展示了如何在语句中使用多个case标签,代码计算指定月份的天数:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
class SwitchDemo2 {
public static void main(String[] args) {
int month = 2;
int year = 2000;
int numDays = 0;
switch (month) {
case 1: case 3: case 5:
case 7: case 8: case 10:
case 12:
numDays = 31;
break;
case 4: case 6:
case 9: case 11:
numDays = 30;
break;
case 2:
if (((year % 4 == 0) &&
!(year % 100 == 0))
|| (year % 400 == 0))
numDays = 29;
else
numDays = 28;
break;
default:
System.out.println("Invalid month.");
break;
}
System.out.println("Number of Days = "
+ numDays);
}
}

以下为代码的输出:

1
Number of Days = 29

在switch语句中使用字符串

Java SE 7以及之后的版本中,你可以在switch语句的表达式中使用字符串String对象。以下代码, StringSwitchDemo,根据字符串month的String值显示出对应月份的数字:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
public class StringSwitchDemo {
public static int getMonthNumber(String month) {
int monthNumber = 0;
if (month == null) {
return monthNumber;
}
switch (month.toLowerCase()) {
case "january":
monthNumber = 1;
break;
case "february":
monthNumber = 2;
break;
case "march":
monthNumber = 3;
break;
case "april":
monthNumber = 4;
break;
case "may":
monthNumber = 5;
break;
case "june":
monthNumber = 6;
break;
case "july":
monthNumber = 7;
break;
case "august":
monthNumber = 8;
break;
case "september":
monthNumber = 9;
break;
case "october":
monthNumber = 10;
break;
case "november":
monthNumber = 11;
break;
case "december":
monthNumber = 12;
break;
default:
monthNumber = 0;
break;
}
return monthNumber;
}
public static void main(String[] args) {
String month = "August";
int returnedMonthNumber =
StringSwitchDemo.getMonthNumber(month);
if (returnedMonthNumber == 0) {
System.out.println("Invalid month");
} else {
System.out.println(returnedMonthNumber);
}
}
}

代码的输出结果为 8.

switch 表达式的String 值 与case标签中对应的值进行比较,就像使用 String.equals 方法。为了使StringSwitchDemo 可以接受所有忽略大小写的月份,month被转换为小写形式。(通过 toLowerCase 方法), 并且case标签中对应的字符串值也全部为小写。

注意: 本例中,检查了month的值是否为null,确保任何switch语句中的任何表达式值不为null,方式抛NullPointerException 空指针异常.

Java 学习指南_学习Java:基础-控制流程-if-then&if-then-else

发表于 2017-09-20 | 分类于 java

if-then以及if-then-else 语句

if-then 语句

if-then语句是最基本的控制流程语句。它可以告诉程序只有某个特定的测试运算结果为true时执行确定的某部分代码。例如,在Bicycle类中只有自行车在运行的过程中时,才允许刹车减小自行车的速度。applyBrakes(使用刹车)方法的一种实现代码如下:

1
2
3
4
5
6
7
void applyBrakes() {
// the "if" clause: bicycle must be moving
if (isMoving){
// the "then" clause: decrease current speed
currentSpeed--;
}
}

如果这个测试isMovingj结果为false(意味着自行车并没有在刹车状态),控制跳至 if-then 语句结尾.

说明一下,当if-then then代码块中只有一句代码的时候,可以省略保卫代码块的花括号:

1
2
3
4
5
void applyBrakes() {
// same as above, but without braces
if (isMoving)
currentSpeed--;
}

可以平个人喜好来决定在只有一句代码的情况下是否使用花括号。不适用的话可能不会比较容易犯错,因为如果有第二句代码被加入到then代码块中时,会经常出现忘记添加花括号,编译器不会识别这样的手误;程序就会得到错误的结果。

if-then-else语句

if-then-else语句 当if 字句运算结果为false时提供第二条执行路径。在applyBrakes方法中 可以使用if-then-else语句,如果自行车没有在运行状态下时使用的刹车也可以采取一些措施。这里简单的打印出错误信息声明自行车已经停止了。

1
2
3
4
5
6
7
void applyBrakes() {
if (isMoving) {
currentSpeed--;
} else {
System.err.println("The bicycle has already stopped!");
}
}

下面的程序中, IfElseDemo, 根据score分数的测试结果为grade级别赋值:大于等于90为A,大于等于80为B,等等.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class IfElseDemo {
public static void main(String[] args) {
int testscore = 76;
char grade;
if (testscore >= 90) {
grade = 'A';
} else if (testscore >= 80) {
grade = 'B';
} else if (testscore >= 70) {
grade = 'C';
} else if (testscore >= 60) {
grade = 'D';
} else {
grade = 'F';
}
System.out.println("Grade = " + grade);
}
}

输出结果如下:

1
Grade = C

你可能注意到testScore可以满足不止一个条件:76>=70,76>=60.不过,一旦一个条件满足了,相应的语句就会执行 (grade = 'C';) ,剩下的条件不在运算。

1…456…8
Hello Young

Hello Young

80 日志
1 分类
75 标签
GitHub
  • Phaser
© 2018 Hello Young
由 Hexo 强力驱动
主题 - NexT.Pisces