GitOPEN's Home.

《Monkey Java》课程3.2之练习课

Word count: 414 / Reading time: 2 min
2015/07/10 Share

本节课程将学习以下知识点:

  • 练习1:将学生的分数按照标准分为优、良、中、差四个级别。
  • 练习2:猜拳游戏。

练习1:

  • 创建一个名为Demo01的类;
  • 在Demo类中加入主函数;
  • 在主函数中定义一个整型变量,用于表示学生的分数;
  • 使用if…else…结构对分数进行分级。

源码:(请动手)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public class Demo01 {
public static void main(String[] args) {

int score = 60;

if (score >= 85 && score <= 100) {
System.out.println("优秀");
} else if (score > 75 && score <= 85) {
System.out.println("良好");
} else if (score >= 60 && score <= 75) {
System.out.println("中等");
} else if (score < 60) {
System.out.println("不及格");
} else if (score > 100 || score < 0) {
System.out.println("成绩不在正常范围内");
}
}
}

练习2:

  • 创建一个名为Demo02的类;
  • 在Demo类中加入主函数;
  • 在主函数中定义两个char类型的变量,分别代表两个玩家的出拳;
  • 使用if…else…结构对结果进行判断。

源码:(请动手)

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
public class Demo02 {
public static void main(String[] args) {

/**
* 'a' 代表 石头; 'b' 代表 剪刀; 'c' 代表 布;
*/

char play01 = 'a';
char play02 = 'b';

if (play01 == 'a' && play02 == 'a') {
System.out.println("平");
} else if (play01 == 'a' && play02 == 'b') {
System.out.println("play01赢");
} else if (play01 == 'a' && play02 == 'c') {
System.out.println("play02赢");
} else if (play01 == 'b' && play02 == 'a') {
System.out.println("play02赢");
} else if (play01 == 'b' && play02 == 'b') {
System.out.println("平");
} else if (play01 == 'b' && play02 == 'c') {
System.out.println("play01赢");
} else if (play01 == 'c' && play02 == 'a') {
System.out.println("play01赢");
} else if (play01 == 'c' && play02 == 'b') {
System.out.println("play02赢");
} else if (play01 == 'c' && play02 == 'c') {
System.out.println("平");
}

}
}


欣慰帮到你 一杯热咖啡
【奋斗的Coder!】企鹅群
【奋斗的Coder】公众号
CATALOG
  1. 1. 练习1:
  2. 2. 练习2: