Java 音程到音符的转换方法,遇到数字格式异常
我正在研究一种方法,其参数是一串音乐间隔和一串根音符。例如,音乐中的小调音阶为以下音程:Java 音程到音符的转换方法,遇到数字格式异常,java,methods,type-conversion,numberformatexception,Java,Methods,Type Conversion,Numberformatexception,我正在研究一种方法,其参数是一串音乐间隔和一串根音符。例如,音乐中的小调音阶为以下音程: 1 2 b3 4 5 b6 b7 8给定根注释C,输出应为: C4 D4 Eb4 F4 G4 Ab4 Bb4 C5 我还试图包括所有改变的音程,例如:b9、#9、b5、b11、b13。基本倍频程假定为4。如果音程大于一个八度音程,则应将它跨越的八度音程增加到4。理论上,这种方法应该能够将非常大的间隔处理成音符,例如1400的间隔,这将超过人类的听力范围 我是怎么做到的?我创建了一个局部变量字符串“inte
1 2 b3 4 5 b6 b7 8
给定根注释C,输出应为:C4 D4 Eb4 F4 G4 Ab4 Bb4 C5
我还试图包括所有改变的音程,例如:b9、#9、b5、b11、b13。
基本倍频程假定为4。如果音程大于一个八度音程,则应将它跨越的八度音程增加到4。理论上,这种方法应该能够将非常大的间隔处理成音符,例如1400的间隔,这将超过人类的听力范围
我是怎么做到的?我创建了一个局部变量字符串“interval”来单独存储每个间隔以进行处理,一个局部变量“noteValue”以半步保存间隔值,以及一个局部变量“notes”来包含转换后的注释。我获取所有间隔的输入字符串,并为for循环中的每个位置I创建一个子字符串“c”。我测试该子字符串中的字符是数字0-9、“b”(平号)还是“#”(锐号)。否则,假定遇到了一个空格,局部变量“interval”将继续处理
如果字符串c的字符是数字0-9,则会将其添加到字符串间隔中。如果字符串c的字符为“b”,则noteValue递减,如果为“#”,则noteValue递增,每递增一个
例如,如果输入了b13间隔,则应处理“b”,并将noteValue减量1。然后应将“1”添加到名称间隔的字符串中。然后读取“3”并将其添加到名称间隔字符串中。在子字符串c中的字符不是数字“b”或“#”之后,值为“13”的字符串间隔将继续转换为半步。它被解析为
整数,并根据主刻度的半步值进行转换(所有间隔都与之进行比较)。b13的半阶值应为八度(12)+b6(8)=20
假设根注释是C,b13应该返回Ab。这是通过使用一系列if语句来查找正确的根注释来完成的,每个语句都包含一个开关。通过应用%12操作从0到11个可能的半步位置查找注释值,确定注释值并将其添加到注释字符串中。处理完整个intervalString参数后,应返回notes字符串
我在第38行遇到了NumberFormatException问题,我试图将“interval”字符串解析为整数。我放置了一个println()语句来查看发生了什么,字符串间隔似乎没有正确更新。不太确定从这里到哪里去,任何帮助都将不胜感激
这是我的密码:
public class testIntervals {
public static void main(String[] args) {
new testIntervals();
}
public testIntervals() {
String intervals = "1 2 b3 4 5 b6 b7 8";
String rootNote = "C";
String notes = intervalsToNotes(intervals, rootNote);
System.out.println(notes);
}
public String intervalsToNotes(String intervalString, String rootNote) {
int noteValue = 0;
int octave = 4;
String interval = "";
String notes = "";
for (int i = 0; i < intervalString.length(); i++) {
String c = intervalString.substring(i);
System.out.println(c);
if (c.charAt(0) >= '0' && c.charAt(0) <= '9')
interval = interval.concat(c);
else if (c.charAt(0) == 'b')
noteValue--;
else if (c.charAt(0) == '#')
noteValue++;
else {
System.out.println(interval + c);
int process = Integer.parseInt(interval);
interval = "";
for (int j = 1; i <= process; i++) {
if (j%7 == 2 || j%7 == 3 || j%7 == 5 || j%7 == 6 || j%7 == 0) {
noteValue += 2;
}
else if (j%7 == 1 || j%7 == 4) {
noteValue++;
}
}
octave += (noteValue / 12);
if (rootNote.equals("Ab") || rootNote.equals("G#")) {
switch (noteValue % 12) {
case 0: notes = notes.concat(rootNote + octave + " ");break;
case 1: notes = notes.concat("A" + octave + " ");break;
case 2: notes = notes.concat("Bb" + octave + " ");break;
case 3: notes = notes.concat("B" + octave + " ");break;
case 4: notes = notes.concat("C" + octave + " ");break;
case 5: notes = notes.concat("Db" + octave + " ");break;
case 6: notes = notes.concat("D" + octave + " ");break;
case 7: notes = notes.concat("Eb" + octave + " ");break;
case 8: notes = notes.concat("E" + octave + " ");break;
case 9: notes = notes.concat("F" + octave + " ");break;
case 10: notes = notes.concat("Gb" + octave + " ");break;
case 11: notes = notes.concat("G" + octave + " ");break;
}
}
else if (rootNote.equals("A")) {
switch (noteValue % 12) {
case 0: notes = notes.concat(rootNote + octave + " ");break;
case 1: notes = notes.concat("Bb" + octave + " ");break;
case 2: notes = notes.concat("B" + octave + " ");break;
case 3: notes = notes.concat("C" + octave + " ");break;
case 4: notes = notes.concat("C#" + octave + " ");break;
case 5: notes = notes.concat("D" + octave + " ");break;
case 6: notes = notes.concat("D#" + octave + " ");break;
case 7: notes = notes.concat("E" + octave + " ");break;
case 8: notes = notes.concat("F" + octave + " ");break;
case 9: notes = notes.concat("F#" + octave + " ");break;
case 10: notes = notes.concat("G" + octave + " ");break;
case 11: notes = notes.concat("G#" + octave + " ");break;
}
}
else if (rootNote.equals("A#") || rootNote.equals("Bb")) {
switch (noteValue % 12) {
case 0: notes = notes.concat(rootNote + octave + " ");break;
case 1: notes = notes.concat("B" + octave + " ");break;
case 2: notes = notes.concat("C" + octave + " ");break;
case 3: notes = notes.concat("Db" + octave + " ");break;
case 4: notes = notes.concat("D" + octave + " ");break;
case 5: notes = notes.concat("Eb" + octave + " ");break;
case 6: notes = notes.concat("E" + octave + " ");break;
case 7: notes = notes.concat("F" + octave + " ");break;
case 8: notes = notes.concat("Gb" + octave + " ");break;
case 9: notes = notes.concat("G" + octave + " ");break;
case 10: notes = notes.concat("Ab" + octave + " ");break;
case 11: notes = notes.concat("A" + octave + " ");break;
}
}
else if (rootNote.equals("B") || rootNote.equals("Cb")) {
switch (noteValue % 12) {
case 0: notes = notes.concat(rootNote + octave + " ");break;
case 1: notes = notes.concat("C" + octave + " ");break;
case 2: notes = notes.concat("C#" + octave + " ");break;
case 3: notes = notes.concat("D" + octave + " ");break;
case 4: notes = notes.concat("D#" + octave + " ");break;
case 5: notes = notes.concat("E" + octave + " ");break;
case 6: notes = notes.concat("F" + octave + " ");break;
case 7: notes = notes.concat("F#" + octave + " ");break;
case 8: notes = notes.concat("G" + octave + " ");break;
case 9: notes = notes.concat("G#" + octave + " ");break;
case 10: notes = notes.concat("A" + octave + " ");break;
case 11: notes = notes.concat("A#" + octave + " ");break;
}
}
else if (rootNote.equals("B#") || rootNote.equals("C")) {
switch (noteValue % 12) {
case 0: notes = notes.concat(rootNote + octave + " ");break;
case 1: notes = notes.concat("C#" + octave + " ");break;
case 2: notes = notes.concat("D" + octave + " ");break;
case 3: notes = notes.concat("D#" + octave + " ");break;
case 4: notes = notes.concat("E" + octave + " ");break;
case 5: notes = notes.concat("F" + octave + " ");break;
case 6: notes = notes.concat("F#" + octave + " ");break;
case 7: notes = notes.concat("G" + octave + " ");break;
case 8: notes = notes.concat("G#" + octave + " ");break;
case 9: notes = notes.concat("A" + octave + " ");break;
case 10: notes = notes.concat("A#" + octave + " ");break;
case 11: notes = notes.concat("B" + octave + " ");break;
}
}
else if (rootNote.equals("C#") || rootNote.equals("Db")) {
switch (noteValue % 12) {
case 0: notes = notes.concat(rootNote + octave + " ");break;
case 1: notes = notes.concat("D" + octave + " ");break;
case 2: notes = notes.concat("D#" + octave + " ");break;
case 3: notes = notes.concat("E" + octave + " ");break;
case 4: notes = notes.concat("F" + octave + " ");break;
case 5: notes = notes.concat("F#" + octave + " ");break;
case 6: notes = notes.concat("G" + octave + " ");break;
case 7: notes = notes.concat("G#" + octave + " ");break;
case 8: notes = notes.concat("A" + octave + " ");break;
case 9: notes = notes.concat("A#" + octave + " ");break;
case 10: notes = notes.concat("B" + octave + " ");break;
case 11: notes = notes.concat("C" + octave + " ");break;
}
}
else if (rootNote.equals("D")) {
switch (noteValue % 12) {
case 0: notes = notes.concat(rootNote + octave + " ");break;
case 1: notes = notes.concat("D#" + octave + " ");break;
case 2: notes = notes.concat("E" + octave + " ");break;
case 3: notes = notes.concat("F" + octave + " ");break;
case 4: notes = notes.concat("F#" + octave + " ");break;
case 5: notes = notes.concat("G" + octave + " ");break;
case 6: notes = notes.concat("G#" + octave + " ");break;
case 7: notes = notes.concat("A" + octave + " ");break;
case 8: notes = notes.concat("A#" + octave + " ");break;
case 9: notes = notes.concat("B" + octave + " ");break;
case 10: notes = notes.concat("C" + octave + " ");break;
case 11: notes = notes.concat("C#" + octave + " ");break;
}
}
else if (rootNote.equals("D#") || rootNote.equals("Eb")) {
switch (noteValue % 12) {
case 0: notes = notes.concat(rootNote + octave + " ");break;
case 1: notes = notes.concat("E" + octave + " ");break;
case 2: notes = notes.concat("F" + octave + " ");break;
case 3: notes = notes.concat("F#" + octave + " ");break;
case 4: notes = notes.concat("G" + octave + " ");break;
case 5: notes = notes.concat("G#" + octave + " ");break;
case 6: notes = notes.concat("A" + octave + " ");break;
case 7: notes = notes.concat("A#" + octave + " ");break;
case 8: notes = notes.concat("B" + octave + " ");break;
case 9: notes = notes.concat("C" + octave + " ");break;
case 10: notes = notes.concat("C#" + octave + " ");break;
case 11: notes = notes.concat("D" + octave + " ");break;
}
}
else if (rootNote.equals("E") || rootNote.equals("Fb")) {
switch (noteValue % 12) {
case 0: notes = notes.concat(rootNote + octave + " ");break;
case 1: notes = notes.concat("F" + octave + " ");break;
case 2: notes = notes.concat("F#" + octave + " ");break;
case 3: notes = notes.concat("G" + octave + " ");break;
case 4: notes = notes.concat("G#" + octave + " ");break;
case 5: notes = notes.concat("A" + octave + " ");break;
case 6: notes = notes.concat("A#" + octave + " ");break;
case 7: notes = notes.concat("B" + octave + " ");break;
case 8: notes = notes.concat("C" + octave + " ");break;
case 9: notes = notes.concat("C#" + octave + " ");break;
case 10: notes = notes.concat("D" + octave + " ");break;
case 11: notes = notes.concat("D#" + octave + " ");break;
}
}
else if (rootNote.equals("E#") || rootNote.equals("F")) {
switch (noteValue % 12) {
case 0: notes = notes.concat(rootNote + octave + " ");break;
case 1: notes = notes.concat("F#" + octave + " ");break;
case 2: notes = notes.concat("G" + octave + " ");break;
case 3: notes = notes.concat("G#" + octave + " ");break;
case 4: notes = notes.concat("A" + octave + " ");break;
case 5: notes = notes.concat("A#" + octave + " ");break;
case 6: notes = notes.concat("B" + octave + " ");break;
case 7: notes = notes.concat("C" + octave + " ");break;
case 8: notes = notes.concat("C#" + octave + " ");break;
case 9: notes = notes.concat("D" + octave + " ");break;
case 10: notes = notes.concat("D#" + octave + " ");break;
case 11: notes = notes.concat("E" + octave + " ");break;
}
}
else if (rootNote.equals("F#") || rootNote.equals("Gb")) {
switch (noteValue % 12) {
case 0: notes = notes.concat(rootNote + octave + " ");break;
case 1: notes = notes.concat("G" + octave + " ");break;
case 2: notes = notes.concat("G#" + octave + " ");break;
case 3: notes = notes.concat("A" + octave + " ");break;
case 4: notes = notes.concat("A#" + octave + " ");break;
case 5: notes = notes.concat("B" + octave + " ");break;
case 6: notes = notes.concat("C" + octave + " ");break;
case 7: notes = notes.concat("C#" + octave + " ");break;
case 8: notes = notes.concat("D" + octave + " ");break;
case 9: notes = notes.concat("D#" + octave + " ");break;
case 10: notes = notes.concat("E" + octave + " ");break;
case 11: notes = notes.concat("F" + octave + " ");break;
}
}
else if (rootNote.equals("G")) {
switch (noteValue % 12) {
case 0: notes = notes.concat(rootNote + octave + " ");break;
case 1: notes = notes.concat("G#" + octave + " ");break;
case 2: notes = notes.concat("A" + octave + " ");break;
case 3: notes = notes.concat("A#" + octave + " ");break;
case 4: notes = notes.concat("B" + octave + " ");break;
case 5: notes = notes.concat("C" + octave + " ");break;
case 6: notes = notes.concat("C#" + octave + " ");break;
case 7: notes = notes.concat("D" + octave + " ");break;
case 8: notes = notes.concat("D#" + octave + " ");break;
case 9: notes = notes.concat("E" + octave + " ");break;
case 10: notes = notes.concat("F" + octave + " ");break;
case 11: notes = notes.concat("F#" + octave + " ");break;
}
}
}
}
return notes;
}
公共类测试间隔{
公共静态void main(字符串[]args){
新的睾丸间期();
}
公开作证次数(){
字符串间隔=“1 2 b3 4 5 b6 b7 8”;
字符串rootNote=“C”;
字符串注释=间隔注释(间隔,根注释);
系统输出打印项次(备注);
}
公共字符串intervalsToNotes(字符串intervalString,字符串rootNote){
int noteValue=0;
int倍频程=4;
字符串间隔=”;
弦乐=”;
对于(int i=0;i 如果(c.charAt(0)>='0'&&c.charAt(0)您意外地连接了一个不可转换的字符串:interval=interval.concat(c);
(第31行)。我认为应该是interval=interval.concat(c.charAt(0));
您意外连接了一个不可转换的字符串:interval=interval.concat(c);
(第31行)我认为应该是interval=interval.concat(c.charAt(0));