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

2016-09-09  本文已影响0人  Honour_Lee

作业:
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系统:
/**

*/
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("欢迎再来!");
}

}

五子棋:
/**

*/
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]);
    }
}

}

上一篇下一篇

猜你喜欢

热点阅读