-1

/** 性能类的预期方法调用似乎有问题。该对象被识别为乐队,但由于某种原因,当我调用 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*/
4

1 回答 1

2

改变:

Performance band;

到:

Performance band = null;

发生这种情况,因为编译器不确定变量是否被赋值(如果 while 不会首先传递)。尽管您可以使用空值(在某些情况下会导致 NPE),但您不能使用未分配的值。

回想一下,全局变量会自动分配给 null,而不是局部变量。

请注意,即使在逻辑上必须分配它也是如此,例如在一个try-catch块中,您必须在 , 之前初始化和定义(可能null)才能try使用它,因为编译器不确定它是否会被分配,try而是捕获异常。即使你System.exit(1)catch街区打电话。这意味着,从逻辑上讲,它必须被定义(否则程序将终止)但编译器并不那么聪明;)

于 2013-06-03T00:45:09.117 回答