ここでは繰り返す入力を試してみます。
> java Equal
上記を実行すると
> Equal ?:
と表示されますので 調べたい平均律数を入力して「Enter」キーを押します。 例えば「12」と入力してみます。
> Equal ?: 12 1 440.0 2 466.16376 3 493.8833 4 523.25116 5 554.36523 6 587.3295 7 622.25397 8 659.2551 9 698.4565 10 739.98883 11 783.99084 12 830.6094 13 880.0 > Equal ?:
するとオクターブを
12に分割した周波数が表示されて再び入力を待ちます。
「quit」を入力するか数値以外の入力で終了します。
import java.io.*;
class Equal {
static int Pitch = 440; /* A49キーのピッチ周波数です */
public static void main(String[] argv) throws IOException {
BufferedReader in =
new BufferedReader(new InputStreamReader(System.in));
for (;;) {
System.out.print("Equal ?: ");
String line = in.readLine();
if ((line == null) || line.equals("quit"))
break;
try {
int equal = Integer.parseInt(line);
for (int i = 0; i <= equal; i++)
System.out.println((i+1)+" "+
(float)getFreq(i+49, equal));
} catch (Exception e) {
errorExit("Input error.");
} /* 入力文字を数値に変換します */
}
}
public static double getFreq(int key, double equal) {
return Pitch*Math.pow(2, (key-49)/equal);
}
public static void errorExit(String str) {
System.out.println(str);
System.out.println("Usage: java equal");
System.out.println(" [等分平均律の数 | quit (終了)]");
System.exit(0);
} /* エラーメッセージを表示して終了します */
}