我已經創建了我的聲音類的用戶界面。它看起來應該工作得很好,因爲聲音類完美地工作。我不知道爲什麼,但當我嘗試運行READ命令時,出現錯誤,說沒有這樣的文件或目錄存在。該文件確實存在,並且與程序位於同一文件夾中。繼承人的代碼:用戶界面不讀取文件
public class SoundUI
{
public static void main(String[] args){
Scanner input = new Scanner(System.in);
Sound s = new Sound();
final String MENU = "R)ead , W)rite , B)ackwards , L)engthen , S)horten , I)ncrease Vol. ," +
"D)ecrease Vol. , Q)uit";
char cmd = ' ';
System.out.println(MENU);
//User interface commands for sound
do{
cmd = input.next().charAt(0);
if(cmd == 'R'){ //read
System.out.println("Enter file name with quotes around it");
String fileName = input.next();
s.wavRead(fileName);
System.out.println(MENU);
cmd = input.next().charAt(0);
}
else if(cmd == 'W'){ //save
System.out.println("Name of file you want to save as");
String fileName = input.next();
s.wavSave(fileName);
System.out.println(MENU);
cmd = input.next().charAt(0);
}
else if(cmd == 'B'){ //reverse
s.reverse();
System.out.println(MENU);
cmd = input.next().charAt(0);
}
else if(cmd == 'L'){ //lengthen
s.lengthen();
System.out.println(MENU);
cmd = input.next().charAt(0);
}
else if(cmd == 'S'){ //shorten
s.shorten();
System.out.println(MENU);
cmd = input.next().charAt(0);
}
else if(cmd == 'I'){ //increase volume
System.out.println("Enter percent");
double percent = input.nextDouble();
s.increaseVol(percent);
System.out.println(MENU);
cmd = input.next().charAt(0);
}
else if(cmd == 'D'){ //decrease volume
System.out.println("Enter percent");
double percent = input.nextDouble();
s.reduceVol(percent);
System.out.println(MENU);
cmd = input.next().charAt(0);
}
} while(cmd !='Q'); //quit
}
}
「附帶引號」部分聽起來對我很可疑。你真的在用引號嗎?如果是這樣,爲什麼?那麼'wavRead'方法是做什麼的?你已經發布你的菜單代碼,但這不是代碼失敗,可能是... –
@JonSkeet我懷疑它是;特別是輸入問題。海報應該在打印出文件名之前先閱讀文件名,看看它是否有用。 – chrylis
public void wavRead(String fileName)this.samples = WavIO.read(fileName); } – user3511198