day06(數組練習、五子棋、Hotle)

作業:
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]);
    }
}

}

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • 【程序1】 題目:古典問題:有一對兔子,從出生后第3個月起每個月都生一對兔子,小兔子長到第三個月后每個月又生一對兔...
    葉總韓閱讀 5,167評論 0 41
  • Java經典問題算法大全 /*【程序1】 題目:古典問題:有一對兔子,從出生后第3個月起每個月都生一對兔子,小兔子...
    趙宇_阿特奇閱讀 1,916評論 0 2
  • 1. Java基礎部分 基礎部分的順序:基本語法,類相關的語法,內部類的語法,繼承相關的語法,異常的語法,線程的語...
    子非魚_t_閱讀 31,778評論 18 399
  • 一、 1、請用Java寫一個冒泡排序方法 【參考答案】 public static void Bubble(int...
    獨云閱讀 1,421評論 0 6
  • 回溯算法 回溯法:也稱為試探法,它并不考慮問題規模的大小,而是從問題的最明顯的最小規模開始逐步求解出可能的答案,并...
    fredal閱讀 13,748評論 0 89