2013-01-09 23 views
-1

它應該是一個詞猜猜遊戲,給5個機會進入一個輔音,然後猜猜這個單詞還沒有完成,但我必須知道這部分程序是否運行良好。我認爲這是給我找麻煩的變量是輔音,元音字母,數字下面是我的代碼:PS IM很新的Java我有錯誤:找不到符號,但我認爲我的變量都被聲明和初始化

public class julia1 { 

public static void main(String[] args) { 

    System.out.print("enter text to guess: "); 
    String w = Keyboard.readString(); 

    String asterix = ""; 

    for(int c = 0; c < w.length(); c++){ 
     if(w.charAt(c)==(' ')) asterix = asterix + " "; 
     else asterix = asterix + "*"; 
     } 
    System.out.println(asterix); 


    for (int trys = 0; trys <=5; trys++){ 
     String temp=""; 
     System.out.print("enter a consonant: "); 
     char c1 = Keyboard.readChar(); 
    for (int i = 0; i < w.length(); i++) 
    { 
     boolean character = false, vowel = false, consonant =false,     number= false; 
     if (w.charAt(i) >= 'a' &&w.charAt(i)<='z') 
     character = true; 

     if (w.charAt(i) >= 'A' && w.charAt(i)<='Z') 
     character = true; 

     if (character == true){ 
      switch (w.charAt(i)){ 
       case 'a': case 'A': case 'o': case 'O': 
       case 'e': case 'E': 
       case 'i': case 'I': 
       case 'u': case 'U': vowel = true; break; 

     if (c1 >= '0' && c1 <='9') 
      number=true;   
       default : consonant = true; 

      } 
     } 
    } 
     for(int c = 0; c < w.length(); c++){ 
      if((w.charAt(c)==c1) && (consonant == true)) 
      temp = temp + c1; 
      else if (vowel==true) 
        {temp = temp + asterix.charAt(c); 
        System.out.println("this is a vowel not consonant"); 
        } 
       else 
        temp = temp + asterix.charAt(c)&& number==true; 
        System.out.println("this is not a valid letter");} 

     asterix = temp; 
     System.out.println(asterix) ; 
} 



    } 
} 
+3

在這行你得到這個錯誤?在這裏發佈編譯器錯誤。 –

+0

哪一行是錯誤? – RNJ

+0

如果((w.charAt(C)== C1)&&(輔音== TRUE)) ^ 符號:可變輔音 位置:類julia1 E:\ julia1 \ SRC \ julia1.java:47:錯誤:無法找到符號 否則,如果(元音==真) ^ 符號:變量元音 位置:類julia1 E:\ julia1的\ src \ julia1.java:52:錯誤:無法找到符號 \t溫度=溫度+星號.charAt(c)&& number == true; \t^ 符號:變量號碼 位置:class julia1 3錯誤 –

回答

0

一個問題是,您已經在一個for循環內聲明consonant,然後嘗試在另一個循環內使用它。這是不允許的,因爲consonant的範圍在您聲明的for循環結束時結束。

6

你聲明的變量boolean character = false, vowel = false, consonant = false, number = false;for循環,並嘗試使用他們這個循環外,在其他for循環內。這是編譯錯誤的原因。

+0

非常感謝tnx。我會嘗試別的... –

+2

@JuliaCaruana,不要嘗試'別的'。 (int i = 0; i Andremoniy

+1

PS不幸的是,這不是你代碼中的最後一個錯誤,但是這將解決這個第一個問題。 – Andremoniy

4

這些變量:

boolean character = false, vowel = false, consonant = false, number = false; 

是內聲明的for循環,這意味着他們的範圍僅限於for循環。當您嘗試在下一個for循環中重用它們時,它們不再存在。

要麼能夠知道在前一個循環結束時它們的最終值是什麼,在這種情況下,您需要通過在第一個for循環之前聲明來增加它們的範圍。或者你沒有,你可以在第二個循環內重新聲明它們。

0

1)

boolean character = false, vowel = false, consonant =false, 

你是用結束,但它應該是;

2)定義你的字符,元音,輔音在for循環外面,否則它的範圍僅限於for循環。

如果將它們定義爲類變量,則不需要初始化爲false,並且其默認值爲false。

public class julia1 { 

boolean character, vowel , consonant ; 

public static void main(String[] args) { 

3)temp = temp + asterix.charAt(c)&& number==true;行是無效的

相關問題