美文网首页
day06(数组练习、五子棋、Hotle)

day06(数组练习、五子棋、Hotle)

作者: Honour_Lee | 来源:发表于2016-09-09 22:41 被阅读0次

作业:
public class HomeWork05 {
public static void main(String[] args) {
printPoem();
}

/**
 * 打印诗篇
 */
public static void printPoem()
{
    String[][] poem={
            {"床","前","明","月","光"},
            {"疑","似","地","上","霜"},
            {"举","头","望","明","月"},
            {"低","头","思","故","乡"},
    };
    //诗的行;
    for(int i=0;i<poem.length;i++)
    {
        //诗的列
        for(int j=0;j<poem[i].length;j++)
        {
            System.out.print(poem[i][j]);   
        }
        System.out.println();
    }
}

/**
 * 定义一个长度为10的一维整型数组  通过控制台输入完成数组的初始化(赋值)
 */
public static void initValueMethod() {
    Scanner sc = new Scanner(System.in);
    int[] arr = new int[10];
    for(int  i = 0;  i< 10 ; i++)
    {
        arr[i] = sc.nextInt();
    }
    
    //使用之前 先要判空!
    if(arr.length>0)
    {
        for(int j = 0 ; j < arr.length ; j++)
        {
            System.out.println(arr[j]);
        }
    }
    else {
        System.out.println("数组为空");
    }   
}

}

Hotel系统:
/**

  • 某酒店有10层 每层12个房间
  • 每个房间的房间号 通过层数+客房号组成
  • 例如
  • 三楼的第四个房间   0304
    
  • 十楼的第是一个房间 1011
    
  • 该系统有以下功能
  • 1 系统运行后展示菜单
  • 2 提供用户选择功能项
  • -----------菜单---------
  • 1 查询所有房间状态
  • 2 入住
  • 3 退房
  • 4 退出系统

  • 3 如果用户选择1
  • 列出所有房间状态
    
  • 如果没有人入住 显示 空
    
  • 如果有人入住 显示  入住者姓名
    
  • 0101 0102 0103 0104 
    
  •  空         空        张三     李四
    
  • ..........
    
  • 4 如果用户选择2
  • 控制台输出:
  •    请输入入住人姓名:xxx
    
  •    请输入要入住的房间号:xxxx
    
  •    如果该房间为空 则显示入住成功  
    
  •    如果该房间已有人住 显示 入住失败 请重新选择房间
    
  • 5 如果用户选择3
  • 控制台输出:
  •    请选择要退房的房间号 :xxxx
    
  •    如果房间本身为空  显示退房失败
    
  •    否则显示退房成功
    
  • 6 选择4 再见 欢迎再来!

*/
public class Hotel {
//定义全局的变量 rooms
public static String[][] rooms = new String[10][12];

//全局的控制台输入
public static Scanner sc = new Scanner(System.in);

public static void main(String[] args) {
    //数据初始化 
    initMethod();
    
    boolean running = true;
    while (running) {
        //打印菜单
        printMenuMethod();
        //用户选择项
        int choice = sc.nextInt();
        switch (choice) {
        case 1:
            printRoomState();
            break;
        case 2:
            getInMethod();
            break;
        case 3:
            outOfRoomMethod();
            break;
        case 4:
            outOfSystem();
            break;
        default:
            System.out.println("没有该功能!");
            break;
        }
        
    }
    
}
/**
 * 初始化房间状态
 */
public static void initMethod()
{
    for(int i = 0 ; i < rooms.length ; i++)
    {
        for(int j = 0; j < rooms[i].length ; j ++)
        {
            rooms[i][j] = "空房";
        }
    }
}
/**
 * 打印功能菜单
 */
public static void printMenuMethod()
{
    System.out.println("-----菜单-------");
    System.out.println("1-查询所有房间状态");
    System.out.println("2-入住");
    System.out.println("3-退房");
    System.out.println("4-退出系统");
    System.out.println("---------------");
}
/**
 * 打印房间状态
 */
public static void printRoomState()
{
    //控制楼层
    for(int i = 0; i < rooms.length ; i++)
    {
        //控制房间号
        for(int j = 0; j < rooms[i].length ; j++)
        {
            //完成房间号 不满足两位数时的操作
            //String+任意类型 = String
            String roomNo = "";
            if(i<9)
            {
                roomNo = roomNo+"0"+(i+1);
            }
            else 
            {
                roomNo = roomNo + (i+1);
            }
            if(j<9)
            {
                roomNo = roomNo + "0" + (j+1);
            }
            else
            {
                roomNo = roomNo + (j+1);
            }
            System.out.print(roomNo+"\t");
        }
        System.out.println();
        
        //打印房间状态
        for(int k = 0 ; k < rooms[i].length ; k++)
        {
            System.out.print(rooms[i][k]+"\t");
        }
        System.out.println();
    }
}

/**
 * 入住
 */
public static void getInMethod()
{
    //无限循环 直到某次成功为止
    while (true) {
        
        System.out.println("请输入要入住的房间号:");
        int roomNo = sc.nextInt();
        //求楼层
        int i = roomNo/100 -1;
        //求客房号
        int j = roomNo%100 -1;
        
        //equals 比较string类型的数值 返回结果是:true false
        if("空房".equals(rooms[i][j]))
        {
            //是空房间 要输入入住者姓名
            System.out.println("请输入入住者姓名:");
            String nameString = sc.next();
            //修改房间状态 改为入住者姓名
            rooms[i][j] = nameString;
            System.out.println("入住成功!");
            break;
        }
        System.out.println("该房间已有人入住!");
    }
}

/**
 * 退房
 */
public static void outOfRoomMethod()
{
    while(true)
    {
        System.out.println("请输入要退房的房间号");
        int roomNo = sc.nextInt();
        //求楼层
        int i = roomNo/100 -1;
        //求客房号
        int j = roomNo%100 -1;
        
        if(!"空房".equals(rooms[i][j]))
        {
            //说明不是空房间
            rooms[i][j] = "空房";
            System.out.println("退房成功!");
            break;
        }
        System.out.println("该房间为空!请重新输入要退房的房间号");
    }
    
}
/**
 * 退出系统
 */
public static void outOfSystem()
{
    System.out.println("欢迎再来!");
}

}

五子棋:
/**

  • 五子棋
  • @author Administrator

*/
public class Test03 {

public static String[][] chessesStrings = new String[16][16];

public static void main(String[] args) {
    initMethod();
    
    
    Scanner sc = new Scanner(System.in);
    String[] who = {"黑方","白方"};
    String[] whoChess = {"&","%"};
    int whoCount = 0;
    while (true) {
        printMethod();
        //判断谁落子
        System.out.println("请"+who[whoCount%2]+"落子");
        String x = sc.next();
        String y = sc.next();
        
        //十六进制转十进制
        int tmpx = Integer.valueOf(x,16);
        int tmpy = Integer.valueOf(y,16);
        
        //& 黑方    %白方
        if("*".equals(chessesStrings[tmpx][tmpy]))
        {
            chessesStrings[tmpx][tmpy] = whoChess[whoCount%2];
            //判断输赢
            if(checkMethod(tmpx, tmpy))
            {
                System.out.println(who[whoCount%2]+"获胜,game over");
                break;
            }
            //如果还没结束 切换落子方
            whoCount++;
            
        }
        else 
        {
            System.out.println("该位置已有棋子 不能落子");
        }
        
        
    }
    
}
/**
 * 初始化棋盘
 */
public static void initMethod()
{
    for(int i = 0 ; i < chessesStrings.length; i++)
    {
        for(int j = 0 ; j < chessesStrings[i].length ; j++)
        {
            chessesStrings[i][j]= "*";
        }
    }
}
/**
 * 打印棋盘
 */
public static void printMethod()
{
    System.out.print("  ");
    for(int i = 0 ; i < 16 ; i ++)
    {
        //十进制转十六进制
        System.out.print(Integer.toHexString(i)+" ");

    }
    System.out.println();
    for(int i = 0;i<chessesStrings.length; i++)
    {
        System.out.print(Integer.toHexString(i)+" ");
        for(int j = 0;j<chessesStrings[i].length; j++)
        {
            System.out.print(chessesStrings[i][j]+" ");
        }
        System.out.println("");
    }
}
/**
 * 判断输赢
 */
public static boolean checkMethod(int x,int y)
{
    if(checkVerticalMethod(x,y)>=5)
    {
        return true;
    }
    else if(checkHorizonalMethod(x, y)>=5)
    {
        return true;
    }
    else if(checkDeclineMethod(x, y)>=5)
    {
        return true;
    }
    else if(checkOppositeDeclineMethod(x, y)>=5){
        return true;
    }
    else {
        return false;
    }
    
}
/**
 * 判断竖直方向
 */
public static int checkVerticalMethod(int x,int y)
{
    //定义的临时棋子
    String chess = chessesStrings[x][y];
    int count = 1;
    int x1 = x-1;
    int y1 = y;
    
    //以落子点为原点 向上搜索
    //chess.equals(chessesStrings[x1][y1]) 
    //与chessesStrings[x][y].equals(chessesStrings[x1][y1])相同
    //比较两个下标下的值是否相同
    while(x1>=0 && chess.equals(chessesStrings[x1][y1]))
    {
        count ++ ;
        x1 -- ;
    }
    
    int x2 = x + 1;
    int y2 = y;
    //以落子点为原点 向下搜索
    while (x2 < 16 && chess.equals(chessesStrings[x2][y2])) {
        count ++;
        x2 ++;
    }
    return count;
}
/**
 * 判断水平方向
 */
public static int checkHorizonalMethod(int x,int y)
{
    return 0;
}
/**
 * 判断正斜方向
 *   /
 */
public static int checkDeclineMethod(int x,int y)
{
    return 0;
}
/**
 * 判断反斜方向
 *   \
 */
public static int checkOppositeDeclineMethod(int x,int y)
{
    return 0;
}

}

Test01:
public class Test01 {
public static void main(String[] args) {
int a = 10;
int[] arr = {1,2,3,4,56,7,8};
int index = 1;
// int[] b = add(arr, a);
// printMethod(add(arr, a));
// printMethod(delete(arr, index));
printMethod(insert(arr, a, index));
}
/**
* 向一个数组末尾添加一个元素
*/
public static int[] add(int[] arr,int value)
{
int[] newarr = new int[arr.length+1];
for(int i = 0 ; i < arr.length ; i ++)
{
newarr[i] = arr[i];
}

    newarr[arr.length] = value;
    return newarr;

// return 0;
}
/**
* 删除arr数组下标位index的元素
*/
public static int[] delete(int[] arr,int index)
{
int[] newarr = new int[arr.length-1];
for(int i = 0 ; i < arr.length-1 ; i ++)
{
if(i<index)
{
newarr[i] = arr[i];
// continue;
}
else {
newarr[i] = arr[i+1];
}

    }
    return arr;
}
/**
 * 数组arr中在指定位置插入一个元素
 * index 下标
 */
public static int[] insert(int[] arr,int value, int index)
{
    int[] newarr = new int[arr.length+1];
    for(int i = 0 ; i < arr.length; i++)
    {
        if(i < index)
        {
            newarr[i] = arr[i];
            continue;
        }
        newarr[i+1] = arr[i];
    }
    newarr[index] = value;
    return newarr;
}
/**
 * 控制台打印数组
 */
public static void printMethod(int[] tmparr)
{
    for(int i = 0 ; i < tmparr.length; i ++)
    {
        System.out.println(tmparr[i]);
    }
}

}

相关文章

  • day06(数组练习、五子棋、Hotle)

    作业:public class HomeWork05 {public static void main(Strin...

  • Day06 数组

    数组的概念及定义 一组一般情况下相同类型的数据。除了 Object 类型之外,Array 类型是ECMAScrip...

  • 2019-04-16 数组(splice)、数组去重、练习-省市

    数组 数组去重 二维数组 练习-省市联动(数组方法) 练习-省市联动(json方法) DOM(节点) 父节点小练习...

  • Java数据结构与算法分析 | 稀疏数组

    五子棋游戏的存取需求 在介绍稀疏数组前我们先来引入一个需求,下面是一个五子棋的棋盘(15 * 15),玩到中途时想...

  • 稀疏数组

    二维数组转成稀疏数,案例(五子棋),思路:1.获取二维数组中有效数据的个数.2.稀疏数组的列数为3,行数通过有效数...

  • 自律给我自由—Day006

    【叶子姑娘的自律100天挑战 Day06】 2019.01.19 Day06/100 【早起】周末也不要赖床。早起...

  • kotlin练习 ---- 数组练习

    kotlin练习 ---- 数组练习 数组创建 使用arrayOf()函数:这种方式无需显示指定数组的长度,但需要...

  • day06 对象和数组

    1. 类是对象的抽象,对象是类的实例。 2.js中的对象? js语言中一切皆为对象,比如数字、字符串、数组、Ma...

  • 稀疏数组 & 环形队列

    一、稀疏数组 1、是什么?比如有一个 11 * 11 的五子棋盘,我们要用程序模拟,那肯定就是二维数组。然后用1表...

  • canvas 五子棋游戏

    效果 代码 思路 创建数组用于保存五子棋的位置即可。输赢判断使用遍历即可重复落棋用判断保存的数组的位置是否已经有棋...

网友评论

      本文标题:day06(数组练习、五子棋、Hotle)

      本文链接:https://www.haomeiwen.com/subject/kmcyettx.html