当前位置: 代码迷 >> 综合 >> P77:城堡游戏1、null的赋值、in.nextLine、split
  详细解决方案

P77:城堡游戏1、null的赋值、in.nextLine、split

热度:68   发布时间:2023-11-25 14:56:53.0

**

P77(城堡游戏1、null的赋值、in.nextLine、split):

**
前言:城堡游戏是mooc浙江大学面向对象编程第五周的代码,要在翁恺老师的带领下修改这些代码,使代码具有可扩展性。所以在改造代码之前,必须要深入理解代码,才能看懂后面的改造
先上城堡的地图:
在这里插入图片描述
要先根据代码画出地图,才能测试改造后的代码是否正确。

理解代码要先从程序的入口main函数开始,一条一条的写上注释
Game类

package castlegamea;import java.util.Scanner;//Game类
public class Game {
    private Room currentRoom;//定义了一个Room的对象currentRoom作为Game的成员函数public Game() {
    createRooms();}private void createRooms(){
    Room outside, lobby, pub, study, bedroom;// 制造房间,并添加描述outside = new Room("城堡外");lobby = new Room("大堂");pub = new Room("小酒吧");study = new Room("书房");bedroom = new Room("卧室");//初始化房间的出口,创建城堡地图outside.setExits(null, lobby, study, pub);lobby.setExits(null, null, null, outside);pub.setExits(null, outside, null, null);study.setExits(outside, bedroom, null, null);bedroom.setExits(null, null, null, study);currentRoom = outside;  // 从城堡门外开始}private void printWelcome() {
    //欢迎函数//欢迎部分System.out.println();System.out.println("欢迎来到城堡!");System.out.println("这是一个超级无聊的游戏。");System.out.println("如果需要帮助,请输入 'help' 。");System.out.println();System.out.println("现在你在" + currentRoom);System.out.print("出口有:");if(currentRoom.northExit != null)//城堡北方不为空,输出north+空格System.out.print("north ");if(currentRoom.eastExit != null)//城堡东方不为空,输出east+空格System.out.print("east ");if(currentRoom.southExit != null)//城堡南方不为空,输出south+空格System.out.print("south ");if(currentRoom.westExit != null)//城堡西方不为空,输出west+空格System.out.print("west ");System.out.println();}// 以下为用户命令private void printHelp() {
    System.out.print("迷路了吗?你可以做的命令有:go bye help");System.out.println("如:\tgo east");// \t是补全当前字符串长度到8的整数倍,最少1个最多8个空格,补多少要看你\t前字符串长度。
// 比如当前字符串长度10,那么\t后长度是16,也就是补6个空格。
// 如果当前字符串长度12,此时\t后长度是16,补4个空格}private void goRoom(String direction) {
    Room nextRoom = null;//定义一个新的Room变量储存变化之后的位置if(direction.equals("north")) {
    nextRoom = currentRoom.northExit;//向北走}if(direction.equals("east")) {
    nextRoom = currentRoom.eastExit;//向东走}if(direction.equals("south")) {
    nextRoom = currentRoom.southExit;//向南走}if(direction.equals("west")) {
    nextRoom = currentRoom.westExit;//向西走}if (nextRoom == null) {
    System.out.println("那里没有门!");//没有房间}else {
    currentRoom = nextRoom;//有房间,当前位置发生更爱System.out.println("你在" + currentRoom);//toString函数,直接输出特定返回值System.out.print("出口有: ");//计算出口if(currentRoom.northExit != null)System.out.print("north ");if(currentRoom.eastExit != null)System.out.print("east ");if(currentRoom.southExit != null)System.out.print("south ");if(currentRoom.westExit != null)System.out.print("west ");System.out.println();}}public static void main(String[] args) {
    Scanner in = new Scanner(System.in);Game game = new Game();//创建一个Game的对象game.printWelcome();//调用Game 的printWelcome 函数while ( true ) {
    String line = in.nextLine();//定义字符串接收命令行,in.nextLine();表示读入一整行String[] words = line.split(" ");//以空格为间隔返回数组//split方法,以括号的内容为间隔返回一个数组//特殊字符需要加斜杠作为转义字符(如* ^ . #)//在引号内要写成这样,\\* \\^ \\. \\#/*案例:* String[] aa = "aaafbbbfccc".split("f"); * for (int i = 0 ; i <aa.length ; i++ ) * {* System.out.println("--"+aa[i]); * }* 输出结果:* --aaa* --bbb* --ccc*/if ( words[0].equals("help") ) {
    game.printHelp();//第一个字符是help调用help函数 } else if (words[0].equals("go") ) {
    game.goRoom(words[1]);//第一个字符是go调用goRoom函数 } else if ( words[0].equals("bye") ) {
    break;//第一个字符是bye,离开循环}}System.out.println("感谢您的光临。再见!");in.close();}}

Room类

package castlegamea;//Room类
public class Room {
    //成员变量public String description;//描述,初始化对象时候的位置//定义四个Room类型的成员变量用来储存方位public Room northExit;//北public Room southExit;//南public Room eastExit;//东public Room westExit;//西//构造函数,初始化对象的时候,声明位置public Room(String description) {
    this.description = description;}//成员函数,传入四个Room变量public void setExits(Room north, Room east, Room south, Room west) {
    if(north != null)northExit = north;if(east != null)eastExit = east;if(south != null)southExit = south;if(west != null)westExit = west;}@Overridepublic String toString(){
    return description;}
}
  • 想有 in.nextLine(); ,要有 Scanner in = new Scanner(System.in);

  • Java 中,null 是一个关键字,用来标识一个不确定的对象。因此可以将 null 赋给引用类型变量,但不可以将 null 赋给基本类型变量。
    比如:int a = null;是错误的。Ojbect o = null 是正确的。

  • 基本类型, 共有8种,即int, short, long, byte, float, double, boolean, char(注意,并没有string的基本类型)

  相关解决方案