【发布时间】:2016-04-04 01:29:00
【问题描述】:
我必须传递 Guitar 构造函数中的值,然后将它们传递给 generateSong 方法。有一个问题。生成歌曲方法不能带参数,因为值在同一个类中,它们应该能够从构造函数中获取值。
当我尝试在该方法中实现值时,无法访问这些值。我该如何解决这个问题?
public Guitar(int chord, int numOfStrings) {
//pass these values to the generateSong method
System.out.println("Guitar () generated a guitar with: " + numOfStrings + "." + "Song length is " +chord);
// declare the array for which the song is stored
// store the values of the last row i (highest index of the array)
double[] max = null;
Guitar.generateSong();
// start the generate song method
public static void generateSong () {
double [] [] song = new double [numOfStrings] [chord];
double[] max;
int findmax =0;
int sum =0;
for (int i =0; i<song.length; i++) {
for (int j =0; j<song.length; j++ ) {
sum = (int) (sum +song[i][j]);
}
for (int e=0; e<song.length; e++) {
if (song[e]!=song[findmax] ) {
max[e] =e;
}
for(int k=0; k<song.length; k++) {
for (int l=0; k<song[k].length; k++)
// assign note values to the array
song [k] [l] = 27.5 + (Math.random()* 4186);
// print out the proper value
System.out.printf("8.2%f", song);
for( int m=0; m<max.length; m++)
max[m]= 1 +( Math.random() *3);
System.out.printf("8.2%f", max);
}
}
}
}
int chord 和 int numOfStrings 的值来自 main 方法中的命令行参数,并通过以下对象传递给该方法:
Guitar guitar = new Guitar (numOfStrings, chord);
【问题讨论】:
标签: java methods constructor