前幾天寫得要死要活的東西…
雖然根本沒人會想看,但是花那麼久時間寫的東西總該拿來餵一餵小水滴,要不然牠真的要餓死了吧我想。
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];
}
}
}
