/** 性能类的预期方法调用似乎有问题。该对象被识别为乐队,但由于某种原因,当我调用 band.begin() (乐队是新的表演对象并开始其方法)乐队未被识别并且它建议将其初始化为变量?*/
import java.util.Scanner;
public class Music {
public static Instrument assignInstrument () {
String instrumentChoice;
String name;
Scanner input = new Scanner (System.in);
System.out.println("Select an instrument for the band member. ");
System.out.print("Vocals, piccolo, Clarinet, Cymbal, or Drum: ");
instrumentChoice = input.nextLine();
System.out.print("Enter the band member's name: ");
name = input.nextLine();
if (instrumentChoice.equalsIgnoreCase("v")) {
return (new Vocal(name));
} else if (instrumentChoice.equalsIgnoreCase("p")) {
return (new Piccolo(name));
} else if (instrumentChoice.equalsIgnoreCase("cl")) {
return (new Clarinet(name));
} else if (instrumentChoice.equalsIgnoreCase("cy")) {
return (new Cymbal(name));
} else {
return (new Drum(name));
}
}
/** I instantiated a "band" performance object but it is not recognizing it when i call its method... */
public static void main (String [] args){
Performance band; // band object instantiated
Instrument bandmember1, bandmember2, bandmember3, bandmember4;
Scanner input = new Scanner (System.in);
String performanceChoice;
bandmember1 = assignInstrument();
bandmember2 = assignInstrument();
bandmember3 = assignInstrument();
bandmember4 = assignInstrument();
System.out.println(bandmember1 + " " + bandmember2 + " " + bandmember3 + " " + bandmember4 + "\n");
System.out.print("Would you like to hear a solo, duet, trio, quartet, or leave?: ");
performanceChoice = input.nextLine();
while (!performanceChoice.equalsIgnoreCase("l")) { // choosing the correct form of object
if (performanceChoice.equalsIgnoreCase("s")) {
band = new Performance (bandmember1);
} else if ( performanceChoice.equalsIgnoreCase("d")) {
band = new Performance (bandmember1, bandmember2);
} else if ( performanceChoice.equalsIgnoreCase("q")) {
band = new Performance (bandmember1, bandmember2, bandmember3);
} else {
band = new Performance (bandmember1, bandmember2, bandmember3, bandmember4);
}
}
band.Begin(); // error message: "variable band might not have been initialized".?
}
}
public class Performance {
private String arrangement;
private Instrument solo;
private Instrument duet_1, duet_2;
private Instrument trio_1, trio_2, trio_3;
private Instrument quart_1, quart_2, quart_3, quart_4;
public Performance (Instrument s) {
solo = s;
arrangement = solo.makeSound();
}
public Performance (Instrument d1, Instrument d2){
duet_1 = d1;
duet_2 = d2;
arrangement = duet_1.makeSound() + duet_2.makeSound();
}
public Performance (Instrument t1, Instrument t2, Instrument t3){
trio_1 = t1;
trio_2 = t2;
trio_3 = t3;
arrangement = trio_1.makeSound() + trio_2.makeSound() + trio_3.makeSound();
}
public Performance (Instrument q1, Instrument q2, Instrument q3, Instrument q4){
quart_1 = q1;
quart_2 = q2;
quart_3 = q3;
quart_4 = q4;
arrangement = quart_1.makeSound() + quart_2.makeSound() + quart_3.makeSound() + quart_4.makeSound();
}
public void Begin() {
System.out.print(arrangement);
}
public String toString(){
String program = "The performance includes ";
program += arrangement;
return program;
}
}
/** Any suggestions? Thanks in advance*/