1 关于方法的返回值和return语句,下面说法错误的是:
A. return 语句用于终止当前方法的执行html
B. 若是方法的返回类型为void,则方法中不能出现return语句java
C. return 关键字还会中止方法的执行;若是方法的返回类型为 void,则可以使用没有值的 return 语句来中止方法的执行编程
D. 定义有返回值的方法,必须使用 return 关键字返回值,且 return 关键字的后面必须是与返回类型匹配的值数组
参考答案数据结构
B 选项的说法错误。ide
这是由于,若是方法有返回值,则必须使用 return 关键字向调用方返回值;若是方法所定义的返回类型为 void,则不须要使用 return 关键字来返回数据,可是,可使用 没有值的 return 语句来中止方法的执行。测试
2 请描述类和对象的关系
参考答案this
对象是一个客观存在的实体,是面向对象编程过程当中分析与解决问题的出发点与基础。对象的实质就是内存中的一块数据存储区域,其数据结构由定义它的类来决定。url
类是用于构建对象的模板,对象经过类的实例化产生,一个类能够建立多个对象,每一个对象拥有本身的数据和行为。spa
3 请描述引用类型和基本类型的区别
参考答案
除8种基本类型以外,用类名(接口、数组)声明的变量称为引用类型变量,简称“引用”。引用的功能在于访问对象。
基本类型变量自己就包含了其实例数据,而引用类型变量中存储的是某个对象在内存中的地址信息。当一个引用类型变量指向该类的对象时,就能够经过这个变量访问对象。
4 为 Cell 类添加右移的方法
本案例须要实现格子的右移功能,即须要为 Cell类定义右移的方法。该方法须要使用重载来分别实现两种功能:
功能1:调用右移方法,不须要传入任何参数,则格子向右移动一列,如图-1上中间的图形所示;
功能2:调用右移方法,并传入须要移动的列数,则格子将向右移动相应的列数,如图-1上右边的图形所示。

图-1
本案例首先须要打印出游戏所在的平面(宽10格,高20格),用“-”号表示平面上的每一个单元;而后假设某格子的坐标为(15,3),即,行号为15,列号为3,须要使用“*”号打印显示该格子,如图-1中左图中的蓝色圈内所示。先调用不带参数的右移方法,则格子右移一列,如图-1上中间图形上蓝色圈内所示;而后调用带参数的右移方法使得格子向右移动 4 列,并从新打印效果,如图-1中右图上蓝色圈内所示。
参考答案
为实现格子右移,须要为Cell类定义 moveRight 方法。该方法不接收参数,则右移一列,代码以下所示:
- public void moveRight() {
- col++;
- }
该方法也能够接收一个 int 类型的参数,表示向右移动的列数,而后在该方法中,将格子的列数增长相应的数值。代码以下所示:
- public void moveRight(int d) {
- col += d;
- }
实现此案例须要按照以下步骤进行。
步骤一:为 Cell 类定义 moveRight 方法
在Cell 类中,添加方法 moveRight,实现右移一列。代码以下所示:
- public class Cell {
- int row;
- int col;
- //右移一列
- public void moveRight() {
- col++;
- }
- }
步骤二:为 Cell 类定义重载的 moveRight 方法
在Cell 类中,重载moveRight方法,定义带有参数的 moveRight 方法,实现右移多列的功能。代码以下所示:
- public class Cell {
- int row;
- int col;
- //右移一列
- public void moveRight() {
- col++;
- }
- //重载的 moveRight方法:右移多列
- public void moveRight(int d) {
- col += d;
- }
- }
步骤三:测试 moveRight 方法
在CellGame 类的main 方法中添加代码:先建立一个位置为(15,3)的格子,并打印显示;而后调用cell 对象的 moveRight方法,实现格子右移一列,并打印显示;再调用一次 moveRight 方法,实现格子右移多列,并打印显示。代码以下所示:
- public class CellGame{
- public static void main(String[] args) {
- System.out.println("----------绘制Cell----------");
- Cell cell1 = new Cell(15,3);
- printCell(cell1);
- System.out.println("----------右移 1 列----------");
- cell1.moveRight();
- printCell(cell1);
- System.out.println("----------右移 4 列----------");
- cell1.moveRight(4);
- printCell(cell1);
- }
- public static void printCell(Cell cell) {
- int totalRow = 20;
- int totalCol = 10;
- //打印场地
- for (int row = 0; row < totalRow; row++) {
- for (int col = 0; col < totalCol; col++) {
- if (cell.row == row && cell.col == col) {
- //打印指定的格子
- System.out.print("* ");
- } else {
- System.out.print("- ");
- }
- }
- System.out.println();
- }
- }
- }
本案例中,类Cell 的完整代码以下所示:
- public class Cell {
- int row;
- int col;
- //右移一列
- public void moveRight() {
- col++;
- }
- //重载的 moveRight方法:右移多列
- public void moveRight(int d) {
- col += d;
- }
- //其余方法...
- }
类CellGame的完整代码以下所示:
- import java.util.Scanner;
- public class CellGame{
- public static void main(String[] args) {
- System.out.println("----------绘制Cell----------");
- Cell cell1 = new Cell(15,3);
- printCell(cell1);
- System.out.println("----------右移 1 列----------");
- cell1.moveRight();
- printCell(cell1);
- System.out.println("----------右移 4 列----------");
- cell1.moveRight(4);
- printCell(cell1);
- }
- public static void printCell(Cell cell) {
- int totalRow = 20;
- int totalCol = 10;
- //打印场地
- for (int row = 0; row < totalRow; row++) {
- for (int col = 0; col < totalCol; col++) {
- if (cell.row == row && cell.col == col) {
- //打印指定的格子
- System.out.print("* ");
- } else {
- System.out.print("- ");
- }
- }
- System.out.println();
- }
- }
- }
5 完成CellGame(提升题,选做)
本案例要求完成 CellGame,用户能够在控制台上操做格子的下落、左移和右移。
游戏刚开始,将在界面上显示一个格子,界面效果如图-2上左图中的蓝色圈内所示,用户能够在控制台选择输入各类操做:1表示下落一行,2表示左移一列,3表示右移一列,0表示退出。若是用户录入1,则格子下落一行,并从新打印显示,界面效果如图-2上右图中的蓝色圈内所示:

图-2
若是用户录入2,则格子左移一列,并从新打印显示,界面效果如图-3上左图中蓝色圈内所示;若是用户录入3,则格子右移一列,并从新打印显示,界面如图-3上右图中蓝色圈内所示:

图-3
若是用户录入0,则游戏结束,界面效果如图-4所示:

图-4
参考答案
前面的案例中,已经实现了格子的下落、左移和右移的功能,此案例中,只须要实现游戏的主要规则便可。
实现此案例须要按照以下步骤进行。
步骤一:定义类及 main 方法
首先定义一个名为 CellGame的类,并在类中定义Java 应用程序的入口方法main ,代码以下所示:
- public class CellGame {
- public static void main(String[] args) {
- }
- }
步骤二:初始化 Cell并打印
在 main 方法中添加代码,建立一个位置为(3,3)的格子,并打印显示。代码以下所示:
- public class CellGame {
- public static void main(String[] args) {
- //建立格子并打印
- Cell cell1 = new Cell(3,3);
- printCell(cell1);
- }
- }
步骤三:输入
在main方法中,实例化Scanner类,并调用Scanner类的nextInt()方法接收用户从控制台输入的表示操做的数值。代码以下所示:
- import java.util.Scanner;
- public class CellGame {
- public static void main(String[] args) {
- //建立格子并打印
- Cell cell1 = new Cell(3,3);
- printCell(cell1);
- //游戏控制
- System.out.println("1 —— 下落,2——向左,3——向右,0 —— 退出");
- Scanner sc = new Scanner(System.in);
- int cmd = sc.nextInt();
- }
- }
注意:此步骤中,须要导入java.util包下的Scanner类。
步骤四:判断输入
判断用户的输入,以实现格子的下落、左移或者右移。直到用户录入0,则表示游戏结束。代码以下所示:
- import java.util.Scanner;
- public class CellGame {
- public static void main(String[] args) {
- //建立格子并打印
- Cell cell1 = new Cell(3,3);
- printCell(cell1);
- //游戏控制
- System.out.println("1 —— 下落,2——向左,3——向右,0 —— 退出");
- Scanner sc = new Scanner(System.in);
- int cmd = sc.nextInt();
- while (cmd != 0) {
- switch(cmd) {
- case 1:
- cell1.drop();
- break;
- case 2:
- cell1.moveLeft();
- break;
- case 3:
- cell1.moveRight();
- break;
- }
- printCell(cell1);
- System.out.println("1 —— 下落,2——向左,3——向右,0 —— 退出");
- cmd = sc.nextInt();
- }
- System.out.println("游戏结束");
- sc.close();
- }
- }
注意:使用完毕后,关闭 Scanner 对象。
本案例中,类Cell 的完整代码以下所示:
- public class Cell {
- int row;
- int col;
- public Cell(int row, int col) {
- this.row = row;
- this.col = col;
- }
- public Cell() {
- this(0, 0);
- }
- public Cell(Cell cell) {
- this(cell.row, cell.col);
- }
- public void drop() {
- row++;
- }
- public void moveLeft(int d) {
- col -= d;
- }
- public String getCellInfo() {
- return row + "," + col;
- }
- public void moveRight(int d) {
- col += d;
- }
- public void drop(int d) {
- row += d;
- }
- public void moveLeft() {
- col--;
- }
- public void moveRight() {
- col++;
- }
- }
类CellGame的完整代码以下所示:
- import java.util.Scanner;
- public class CellGame {
- public static void main(String[] args) {
- //建立格子并打印
- Cell cell1 = new Cell(3,3);
- printCell(cell1);
- //游戏控制
- System.out.println("1 —— 下落,2——向左,3——向右,0 —— 退出");
- Scanner sc = new Scanner(System.in);
- int cmd = sc.nextInt();
- while (cmd != 0) {
- switch(cmd) {
- case 1:
- cell1.drop();
- break;
- case 2:
- cell1.moveLeft();
- break;
- case 3:
- cell1.moveRight();
- break;
- }
- printCell(cell1);
- System.out.println("1 —— 下落,2——向左,3——向右,0 —— 退出");
- cmd = sc.nextInt();
- }
- System.out.println("游戏结束");
- sc.close();
- }
- public static void printCell(Cell cell) {
- int totalRow = 20;
- int totalCol = 10;
- //打印格子的位置
- System.out.println("Cell的位置为:(" + cell.getCellInfo() + ")");
- for (int row = 0; row < totalRow; row++) {
- for (int col = 0; col < totalCol; col++) {
- if (cell.row == row && cell.col == col) {
- System.out.print("* ");
- } else {
- System.out.print("- ");
- }
- }
- System.out.println();
- }
- }
- }