0
我必须传递构造函数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
在主方法来自命令行参数,并通过下面的对象被传递给该方法:
Guitar guitar = new Guitar (numOfStrings, chord);
'第二,你不能在其他方法里面定义方法。“不同意。您可以。匿名,lambda也是函数。 – dosdebug
@ssc:lambda是一个函数(或者更好,但是对匿名类很薄)。这不是一种方法。 – Makoto
我的不好。我经常交替使用这些术语,但我理解并明白你的观点。谢谢。 – dosdebug