為響應政府網路分級制度,本站全部內容為限制級。未滿十八歲的小朋友趕快關掉網頁去玩皮卡丘打排球吧。
顯示具有 [計概] 標籤的文章。 顯示所有文章
顯示具有 [計概] 標籤的文章。 顯示所有文章

[計概]第二次作業

前幾天寫得要死要活的東西…

雖然根本沒人會想看,但是花那麼久時間寫的東西總該拿來餵一餵小水滴,要不然牠真的要餓死了吧我想。

 

public class Homework2{
public static void main(String args[]){

    /* 變數宣告 */
    int i = 0, j = 0;
    String cal[][] = new String[5][9];
    for(j = 0;j <= 8;j++){for(i = 0; i <= 4; i++) cal[i][j] = (" ");}

    /* 首次列印行事曆 */
    System.out.println (" | 一 | 二 | 三 | 四 | 五 |");
    for(j = 0;j <= 8;j++){
        System.out.println ((j+1) + "|" + cal[0][j] + "|" + cal[1][j] + "|" + cal[2][j] + "|" + cal[3][j] + "|" +cal[4][j] + "|");
    }

    /* 使用說明 */
    System.out.println ("指令一覽:add,[行程時間],[行程內容] - 增加行程");
    System.out.println (" copy,[來源時間],[目標時間] - 複製行程");
    System.out.println (" del,[行程時間] - 刪除行程");
    System.out.println (" exit - 離開本系統");
    System.out.println ("注意事項:行程內容長度限定為4個字元。\n");

    /* 首次摘取輸入選項 */
    System.out.print("請輸入指令:");
    String input = ConsoleIn.readLine();
    String parts[] = input.split(",");
    String op = parts[0];

    while(op.equals("exit") == false || parts.length != 1){
        /* 檢查指令格式是否有誤 */
        if( op.equals("add") == false && op.equals("del") == false  && op.equals("copy") == false){
            System.out.println("\n輸入狀況有誤,請重新輸入。");
        }
        else

        /* 判斷指令選項並執行 */

        /* add,新增行程 */

        if(op.equals("add") == true ){

            /* 檢查格式是否錯誤 */

            /* 檢查逗號是否正確 */
            if(parts.length != 3)System.out.println("\n輸入狀況有誤,請重新輸入。");
            else

            /* 檢查時間格式是否錯誤 */
            if(parts[1].matches("[1-5]{1}[1-9]{1}") == false && parts[1].matches("[1-5]{1}[1-9]{1}-[1-5]{1}[1-9]{1}") == false)System.out.println("\n輸入狀況有誤,請重新輸入。");
            else
            /* 檢查行程字數是否符合格式 */
            if(parts[2].matches("[a-zA-z0-9]{4}") == false && parts[2].matches("[^a-zA-z0-9]{1}[a-zA-z0-9]{2}") == false && parts[2].matches("[a-zA-z0-9]{2}[^a-zA-z0-9]{1}") == false && parts[2].matches("[^a-zA-z0-9]{2}") == false && parts[2].matches("[a-zA-z0-9]{1}[^a-zA-z0-9]{1}[a-zA-z0-9]{1}") == false )System.out.println("\n輸入狀況有誤,請重新輸入。");

            else

            /* 檢查輸入的時間格式:單一時間或是一列時間 */
            /* 選擇輸入「一整列的時間」的場合 */
            if (parts[1].length() == 5){
            String[] time = parts[1].split("-");

                /* 檢查格式是否正確 */
                if( (Integer.parseInt(time[0])/10) != (Integer.parseInt(time[1])/10) || (Integer.parseInt(time[0]) - 10*(Integer.parseInt(time[0])/10)) >= (Integer.parseInt(time[1]) - 10*(Integer.parseInt(time[1])/10)) )System.out.println("\n輸入狀況有誤,請重新輸入。");
                else { 
                    i = (Integer.parseInt(time[0])/10) - 1;
                    j = (Integer.parseInt(time[0]) - 10*(Integer.parseInt(time[0])/10)) - 1;
                    while(j <= ((Integer.parseInt(time[1]) - 10*(Integer.parseInt(time[1])/10)) - 1) ){
                        if(cal[i][j].equals(" ") == false)j = 123;
                        else j++;
                    }

                    if (j == 123)System.out.println("\n輸入狀況有誤,請重新輸入。");

                    else {                       
                        for (j=(Integer.parseInt(time[0])-10*(Integer.parseInt(time[0])/10))-1; j <= (Integer.parseInt(time[1])-10*(Integer.parseInt(time[1])/10))-1;j++)cal[i][j] = parts[2];
                        System.out.println ("\n | 一 | 二 | 三 | 四 | 五 |");
                        for(j = 0;j <= 8;j++)System.out.println ((j+1) + "|" + cal[0][j] + "|" + cal[1][j] + "|" + cal[2][j] + "|" + cal[3][j] + "|" +cal[4][j] + "|");
                    }
                 }
            }
            /* 選擇輸入「單一時間」的場合 */
            else {
            i = (Integer.parseInt(parts[1])/10) - 1;
            j = (Integer.parseInt(parts[1]) - 10*(Integer.parseInt(parts[1])/10)) - 1;
            /* 檢查是否該時段已有安排 */
            if(cal[i][j].equals(" ") == false)System.out.println("\n輸入狀況有誤,請重新輸入。");
            else cal[i][j] = parts[2];
            System.out.println ("\n | 一 | 二 | 三 | 四 | 五 |");
            for(j = 0;j <= 8;j++)System.out.println ((j+1) + "|" + cal[0][j] + "|" + cal[1][j] + "|" + cal[2][j] + "|" + cal[3][j] + "|" +cal[4][j] + "|");
            }
        }

        /* del,刪除行程 */
        if(op.equals("del") == true ){

            /* 檢查格式是否正確 */
            /* 檢查逗號是否正確 */
            if(parts.length != 2)System.out.println("\n輸入狀況有誤,請重新輸入。");
            else

            /* 檢查時間格式是否錯誤 */
            if(parts[1].matches("[1-5]{1}[1-9]{1}") == false && parts[1].matches("[1-5]{1}[1-9]{1}-[1-5]{1}[1-9]{1}") == false)System.out.println("\n輸入狀況有誤,請重新輸入。");
            else
            /* 檢查輸入的時間格式:單一時間或是一列時間 */
            /* 選擇刪除「一整列的時間」的場合 */
            if (parts[1].length() == 5){
            String[] time = parts[1].split("-");
                /* 檢查格式是否正確 */               
                if( (Integer.parseInt(time[0])/10) != (Integer.parseInt(time[1])/10) || (Integer.parseInt(time[0]) - 10*(Integer.parseInt(time[0])/10)) >= (Integer.parseInt(time[1]) - 10*(Integer.parseInt(time[1])/10)) )System.out.println("\n輸入狀況有誤,請重新輸入。");
                else { 
                    i = (Integer.parseInt(time[0])/10) - 1;
                    j = (Integer.parseInt(time[0]) - 10*(Integer.parseInt(time[0])/10)) - 1;

                    for (j=(Integer.parseInt(time[0])-10*(Integer.parseInt(time[0])/10))-1; j <= (Integer.parseInt(time[1])-10*(Integer.parseInt(time[1])/10))-1;j++)cal[i][j] = (" ");
                    System.out.println ("\n | 一 | 二 | 三 | 四 | 五 |");
            for(j = 0;j <= 8;j++)System.out.println ((j+1) + "|" + cal[0][j] + "|" + cal[1][j] + "|" + cal[2][j] + "|" + cal[3][j] + "|" +cal[4][j] + "|");
                 }
            }
            /* 選擇刪除「單一時間」的場合 */
            else {
            i = (Integer.parseInt(parts[1])/10) - 1;
            j = (Integer.parseInt(parts[1]) - 10*(Integer.parseInt(parts[1])/10)) - 1;
            cal[i][j] = (" ");
            System.out.println ("\n | 一 | 二 | 三 | 四 | 五 |");
            for(j = 0;j <= 8;j++)System.out.println ((j+1) + "|" + cal[0][j] + "|" + cal[1][j] + "|" + cal[2][j] + "|" + cal[3][j] + "|" +cal[4][j] + "|");
            }
        }

        /* copy,複製行程 */

        if(op.equals("copy") == true ){

            /* 檢查格式是否錯誤 */

            /* 檢查逗號是否正確 */
            if(parts.length != 3)System.out.println("\n輸入狀況有誤,請重新輸入。");
            else

            /* 檢查時間格式是否錯誤 */
            if(parts[1].matches("[1-5]{1}[1-9]{1}") == false && parts[1].matches("[1-5]{1}[1-9]{1}-[1-5]{1}[1-9]{1}") == false)System.out.println("\n輸入狀況有誤,請重新輸入。");
            else
            if(parts[2].matches("[1-5]{1}[1-9]{1}") == false && parts[2].matches("[1-5]{1}[1-9]{1}-[1-5]{1}[1-9]{1}") == false)System.out.println("\n輸入狀況有誤,請重新輸入。");
            else

            /* 檢查被複製的區塊和貼上的區塊類別(單一時間或是一列時間)是否一致 */

            if (parts[1].length() != parts[2].length())System.out.println("\n輸入狀況有誤,請重新輸入。");
            else

            /* 檢查輸入的時間格式:單一時間或是一列時間 */
            /* 選擇複製「一整列的時間」的場合 */
            if (parts[1].length() == 5){
            String[] time = parts[1].split("-");
            String[] time2 = parts[2].split("-");

                /* 檢查格式是否正確 */
                if( (Integer.parseInt(time[0])/10) != (Integer.parseInt(time[1])/10) || (Integer.parseInt(time[0]) - 10*(Integer.parseInt(time[0])/10)) >= (Integer.parseInt(time[1]) - 10*(Integer.parseInt(time[1])/10)) )System.out.println("\n輸入狀況有誤,請重新輸入。");
                else
                if( (Integer.parseInt(time2[0])/10) != (Integer.parseInt(time2[1])/10) || (Integer.parseInt(time2[0]) - 10*(Integer.parseInt(time2[0])/10)) >= (Integer.parseInt(time2[1]) - 10*(Integer.parseInt(time2[1])/10)) )System.out.println("\n輸入狀況有誤,請重新輸入。");
                else
                     { 
                    i = (Integer.parseInt(time2[0])/10) - 1;
                    j = (Integer.parseInt(time2[0]) - 10*(Integer.parseInt(time2[0])/10)) - 1;
                    while(j <= ((Integer.parseInt(time2[1]) - 10*(Integer.parseInt(time2[1])/10)) - 1) ){
                        if(cal[i][j].equals(" ") == false)j = 123;
                        else j++;
                    }

                    if (j == 123)System.out.println("\n輸入狀況有誤,請重新輸入。");
                    else
                    if ( (Integer.parseInt(time[1])-Integer.parseInt(time[0])) != (Integer.parseInt(time2[1])-Integer.parseInt(time2[0])))System.out.println("\n輸入狀況有誤,請重新輸入。");
                    else {   
                        int k = (Integer.parseInt(time2[0]) - 10*(Integer.parseInt(time2[0])/10)) - 1;                   
                        for (j=(Integer.parseInt(time[0])-10*(Integer.parseInt(time[0])/10))-1; j <= (Integer.parseInt(time[1])-10*(Integer.parseInt(time[1])/10))-1;j++){
                        cal[(Integer.parseInt(time2[0])/10) - 1][k] = cal[(Integer.parseInt(time[0])/10) - 1][j];
                        k++;
                        }
                        System.out.println ("\n | 一 | 二 | 三 | 四 | 五 |");
                        for(j = 0;j <= 8;j++)System.out.println ((j+1) + "|" + cal[0][j] + "|" + cal[1][j] + "|" + cal[2][j] + "|" + cal[3][j] + "|" +cal[4][j] + "|");
                    }
                 }
            }
            /* 選擇複製「單一時間」的場合 */
            else {
            i = (Integer.parseInt(parts[1])/10) - 1;
            j = (Integer.parseInt(parts[1]) - 10*(Integer.parseInt(parts[1])/10)) - 1;
            /* 檢查是否該時段已有安排 */
            if(cal[(Integer.parseInt(parts[2])/10 - 1)][(Integer.parseInt(parts[2]) - 10*(Integer.parseInt(parts[2])/10)) - 1].equals(" ") == false)System.out.println("\n輸入狀況有誤,請重新輸入。");
            else cal[(Integer.parseInt(parts[2])/10 - 1)][(Integer.parseInt(parts[2]) - 10*(Integer.parseInt(parts[2])/10)) - 1] = cal[i][j];
            System.out.println ("\n | 一 | 二 | 三 | 四 | 五 |");
            for(j = 0;j <= 8;j++)System.out.println ((j+1) + "|" + cal[0][j] + "|" + cal[1][j] + "|" + cal[2][j] + "|" + cal[3][j] + "|" +cal[4][j] + "|");
            }
        }

    System.out.print("請輸入指令:");

    input = ConsoleIn.readLine();
    parts = input.split(",");
    op = parts[0];
    }

}
}       

[計概]第一次作業

因為不小心上傳錯,所以倒置本次作業只有五分同情分…為了哀悼,特別把它的遺體po上來,讓大家瞻仰一下它的遺容…

--

public class HomeWork{
    public static void main(String args[]){

    /* 主菜類 */
    String manu[] = new String[4];
    manu[0] = ("豬肉絲");
    manu[1] = ("清燉牛肉");
    manu[2] = ("紅燒牛肉");
    manu[3] = ("牛雜");

    int price[] = new int[4];
    price[0] = 50;
    price[1] = 110;
    price[2] = 90;
    price[3] = 90;

    /* 副菜類 */
    String submanu[] = new String[4];
    submanu[0] = ("燙青菜");
    submanu[1] = ("手工水餃");
    submanu[2] = ("白飯");
    submanu[3] = ("冷飲");

    int subprice[] = new int[4];
    subprice[0] = 30;
    subprice[1] = 3;
    subprice[2] = 10;
    subprice[3] = 12;

    /* 記數用 */
    int i = 0;
    int j = 0;
    int k = 0;
    /* 顯示菜單 */
    System.out.println ("歡迎光臨點餐諮詢系統,以下是本系統所有的餐點。\n\n主菜類:");
        while(i <= 3){
            System.out.println (manu[i] + " 價錢:" + price[i]);
            i++;
        }

        if (i == 4){i = 0;}

    System.out.println ("\n副菜類:");

        while(j <= 3){
            System.out.println (submanu[j] + " 價錢:" + subprice[j]);
            j++;
        }

        if (j == 4){j = 0;}

    System.out.print ("\n請輸入您的用餐預算:");
    int budget = ConsoleIn.readLineInt();

    /* 金額輸入確認 */
    System.out.print ("\n您的用餐預算是:" + budget + "元。確認嗎?(Y/N):");
    char op = ConsoleIn.readLineNonwhiteChar();   

    while ((int)op != 89) {
        while ((int)op != 78 && (int)op != 89) {
            System.out.print ("請輸入Y或N(皆為大寫):");
            op = ConsoleIn.readLineNonwhiteChar();
        }

        while ((int)op == 78) {   
            System.out.print ("請重新輸入您的用餐預算:");
            budget = ConsoleIn.readLineInt();
            System.out.print ("\n您的用餐預算是:" + budget + "元。確認嗎?(Y/N)");
            op = ConsoleIn.readLineNonwhiteChar();
        }
    }
    System.out.println ("用餐預算:" + budget + "元,已確認。");

    /* 檢查符合預算的餐點 */

    /* 未到最低標準 */
    if (budget < 50){
    System.out.println ("\n抱歉,沒有符合您預算的主餐。");
    }

    else{
        System.out.println ("\n以下是符合預算的餐點:\n");
        while(i <= 3){
            if( budget >= price[i]){
                System.out.println (manu[i] + " 價錢:" + price[i] + "元");
                System.out.println ("尚有餘額:" + (budget - price[i]) + "元");
                while(j <= 3){
                    if((budget - price[i]) >= subprice[j]){
                        if(k != 0){
                            System.out.print ("或是,");
                        }
                        if(k == 0){
                            System.out.println ("除此之外還夠買以下的副菜:");
                            k++;
                        }
                    System.out.println (submanu[j] + " " + ((budget - price[i]) / subprice[j]) + "份。");
                    }
                j++;
                }
        if (k != 0){k = 0;}
        System.out.println (" ");
        if (j == 4){j = 0;}

        }
        i++;
    }

    if (i == 4){i = 0;}

    }
    }

}

已經到底了,不要再拉了!