13.有一三維陣列,表示R、G、B
{{{18, 172, 127}, {89, 133, 136}, {175, 8, 43}},
{{234, 224, 113}, {128, 151, 40}, {34, 48, 75}},
{{141, 28, 192}, {127, 40, 234}, {131, 228, 98}}}
(a)請以一個三維陣列顯示
(b)將畫素中的r值加30,若加30後的值超過255,則以255取代之
(c)將畫素中的g值減30,若減30後的值小於0,則以0取代之
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 | public class app6 { int array[][][] = {{{18, 172, 127}, {89, 133, 136}, {175, 8, 43}}, {{234, 224, 113}, {128, 151, 40}, {34, 48, 75}}, {{141, 28, 192}, {127, 40, 234}, {131, 228, 98}}}; public static void main(String[] args) { app6 app = new app6(); app.print(); System.out.println("--------------------"); app.addr(); System.out.println("--------------------"); app.addg(); } private void print() { for (int i = 0; i < array.length; i++) { System.out.print("{"); for (int j = 0; j < array[i].length; j++) { System.out.print("{"); for (int k = 0; k < array[i][j].length; k++) { System.out.print(array[i][j][k] + ","); } System.out.print("\b}"); } System.out.print("},\n"); } } private void addr() { int sum[] = {0, 0, 0}; for (int i = 0; i < array.length; i++) { System.out.print("{"); for (int j = 0; j < array[i].length; j++) { System.out.print("{"); for (int k = 0; k < array[i][j].length; k++) { sum[k] = array[i][j][k]; if (k == 0) { //r的值要+30 sum[0] = array[i][j][k] + 30; if (sum[0] > 255) { sum[0] = 255; } } System.out.print(sum[k] + ","); } System.out.print("\b}"); } System.out.print("},\n"); } } private void addg() { int sum[] = {0, 0, 0}; for (int i = 0; i < array.length; i++) { System.out.print("{"); for (int j = 0; j < array[i].length; j++) { System.out.print("{"); for (int k = 0; k < array[i][j].length; k++) { sum[k] = array[i][j][k]; if (k == 1) { sum[1] = array[i][j][k] - 30; if (sum[1] <0) { sum[1] =0; } } System.out.print(sum[k] + ","); } System.out.print("\b}"); } System.out.print("},\n"); } } } |
沒有留言:
張貼留言