【发布时间】:2015-11-09 16:11:48
【问题描述】:
我从文本文件中获得了这行和弦。例如,
String chordLine = "C G Am C";
String transposedChordLine;
接下来,我需要使用下面的类将chordLine 转置为新的transposedChordLine,使用两个参数,一个String 和弦和转置的整数增量。例如,transpose("C", 2) 将返回 D。
public class Transposer{
private int inc;
private static ArrayList<String> keysSharp;
private static ArrayList<String> keysFlat;
Transposer(){
keysSharp = new ArrayList<String>(Arrays.asList("C", "C#", "D", "D#","E", "F","F#", "G","G#", "A","A#", "B"));
keysFlat = new ArrayList<String>(Arrays.asList("C", "Db", "D", "Eb","E", "F","Gb", "G","Ab", "A","Bb", "B"));
}
public String transpose(String chord,int inc){
this.inc = inc;
String newChord;
if(chord.contains("/")){
String[] split = chord.split("/");
newChord = transposeKey(split[0]) + "/" + transposeKey(split[1]);
}else
newChord = transposeKey(chord);
return newChord;
}
private String transposeKey(String key){ // C#m/D# must pass C#m or D#
String nKey, tempKey;
if(key.length()>1){
nKey = key.substring(0, 2);
}
else{ nKey = key; }
int oldIndex, newIndex;
if(key.contains("b")){
oldIndex = (keysFlat.indexOf(nKey)>-1) ? keysFlat.indexOf(nKey) : keysFlat.indexOf(similarKey(nKey));
newIndex = (oldIndex + inc + keysFlat.size())%keysFlat.size();
tempKey = keysFlat.get(newIndex);
nKey = (key.length() < 3) ? tempKey : key.replace(nKey, tempKey);
//(nKey + key.substring(nKey.length(), key.length()));
}
else if(key.contains("#")){
oldIndex = (keysSharp.indexOf(nKey)>-1) ? keysSharp.indexOf(nKey) : keysSharp.indexOf(similarKey(nKey));
newIndex = (oldIndex + inc + keysSharp.size())%keysSharp.size();
tempKey = keysSharp.get(newIndex);
nKey = (key.length() < 3) ? tempKey : key.replace(nKey, tempKey);
}
else{
nKey = nKey.substring(0, 1);
oldIndex = (keysSharp.indexOf(nKey)>-1) ? keysSharp.indexOf(nKey) : keysSharp.indexOf(similarKey(nKey));
newIndex = (oldIndex + inc + keysSharp.size())%keysSharp.size();
tempKey = keysSharp.get(newIndex);
nKey = (key.length() < 2) ? tempKey : key.replace(nKey, tempKey);
}
return nKey;
}
private String similarKey(String nKey) {
String newKey;
switch(nKey){
case "Cb":
newKey = "B";
break;
case "Fb":
newKey = "E";
break;
case "E#":
newKey = "F";
break;
case "B#":
newKey = "c";
break;
default:
newKey = null;
}
return newKey;
}
}
如何在不丢失空格的情况下替换 chordLine?
增加 2 应该有 transposedChordLine="D A Bm D"
这是我目前的尝试:
public static void main(String[] args) {
String chordLine = "C G Am C";
String transposedChordLine;
String normalize = chordLine.replaceAll("\\s+", " ");
String[] split = normalize.split(" ");
//System.out.println(normalize);
Transposer tran = new Transposer();
String[] temp = new String[split.length];
for(int i=0 ; i<split.length ; i++){
temp[i] = tran.transpose(split[i], 2);
//System.out.println(split[i]);
System.out.print(temp[i]);
}
transposedChordLine = chordLine.replaceAll([split], temp); //which is wrong
}
【问题讨论】:
-
许多潜在的解决方案。例如:创建一个对象
ChordLine,它将实例化一条和弦线。在此对象的构造函数中,您可以显式跟踪和弦n_i和n_i+1之间的空白数量。然后,在转置时,您可以简单地替换输出中的空白ChordLine -
对不起,这不是关于你的问题,而是你的代码真的很糟糕。不要在构造函数中初始化
static成员。将这两个静态变量更改为List<String>,并直接使用Arrays.asList对其进行初始化。此外,不要使用实例字段将参数传递给私有方法。如果inc在调用transpose时可能不同,则将其作为参数传递给私有方法。如果inc始终相同,则将其传递给构造函数,而不是transpose方法。 -
大约一年前我开始使用 Java。感谢您的评论。