2013-06-03 44 views
-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*/ 
+0

你確定這是一個錯誤,而不是警告?您的'while'循環不保證至少運行一次。如果它運行它永遠不會停止,因爲你不更新'performanceChoice'。 –

+0

是啊,這就是我的想法太讓我喜歡它擰帶病運行也無妨,但即使我改變它做到這一點不會改變其作爲unrecognition對象或者方法,它實際上是一個錯誤(63,17) – KamikazeStyle

+0

。它似乎很簡單,雖然。我甚至試圖改變每個波段=新的業績「」 ......以性能等級=新的業績「」,但無濟於事:( – KamikazeStyle

回答

2

變化:?

Performance band; 

收件人:

Performance band = null; 

出現這種情況,因爲編譯器是不知道的變量曾經分配一個值(如果同時不會在首位通過)。儘管您可以使用空值(在某些情況下導致NPE),但您無法使用未分配的值。

回想一下,有一個全局變量自動分配給空,而不是局部變量。

注意,這是正確的,即使在邏輯上必須分配,例如在try-catch塊,你必須初始化try之前定義(可能null),爲了使用它,因爲編譯器不確定它會在try中被分配,而是捕獲一個異常。即使您在catch區塊中撥打System.exit(1)也是如此。這意味着,在邏輯上它必須被定義(否則程序將終止),但編譯器不是所以智能;)

+0

完美。非常感謝你 – KamikazeStyle

+0

我複製它從課本多數民衆贊成想教給的方法來計劃的權利。猜猜它不太好。 – KamikazeStyle