JAVAのお勉強。 その2
まぁ、前回から日数がかなり空いているのは
言うまでもなく長めの "夏休み" だったということで…。Σ(´∀`;)w
前回に続き、私の練習問題に対する解答を残しておきたいと思います。
今回は第3章。
import java.io.*; public class test { public static void main(String[] args) { int number; char a[] = {'A','B','C','D','E','H','G','H','I','J'}; System.out.println("0~9の数字を入力して下さい。"); number = readNumber(); // 例外処理 if( number < 0 && number > 9 ) System.out.println("エラー"); System.out.println( a[number]); } // キーボードから数字を入力するメソッド public static int readNumber(){ byte b[] = new byte[100]; try{ System.in.read(b); return Integer.parseInt((new String(b)).trim()); }catch(Exception e){ return 0; } } }
import java.io.*; public class test { public static void main(String[] args) { int i = 0; byte name[][] = new byte [4][]; while( i <= 3){ System.out.println((i+1)+"人目の名前を入力して下さい。"); name[i] = readName(); i = i + 1; } int j = 0; while( j <= 3){ System.out.print((j+1)+"人目に入力された名前は、"); System.out.print(new String(name[j])+"です。\n"); j = j + 1; } } // 名前を読み込んで、byte配列として返す public static byte[] readName(){ byte b[] = new byte[20]; try{ System.in.read(b); }catch(Exception e){} return (new String(b)).trim().getBytes(); } }
4.に関しては、もう少し例外処理を綺麗にかけたらいいのになっという感じ。
5.はfor文を使えばいいところを、何故かwhile文を使って見難くなってしまっている感じですね。
ちょっとペースがのんびり過ぎるので、ペースアップしたいところ。
明日も更新するぞ。()