2014-07-03 7 views
10

मैं मूल रूप से जावा शुरुआती लोगों के लिए संदर्भ पुस्तक से एक परीक्षण कोड संकलित करने, पूरा करने और कोशिश करने की कोशिश कर रहा हूं। इसका उद्देश्य एक अनुमान लगाने वाला गेम बनाना है जिसमें लक्ष्य 3 निरंतर कोशिकाओं में स्थित है (मैं एक सरणी में स्थान धारण कर रहा हूं) और उपयोगकर्ता सेल नंबर का अनुमान लगाता है। सेल द्वारा लक्ष्य सेल को नष्ट करने के लिए।जावा त्रुटि: अभिव्यक्ति की अवैध शुरुआत

मैंने एक ही त्रुटि पर आधे दर्जन पदों की जांच की, लेकिन मुझे पता नहीं चला कि क्या गलत हो रहा था।

यह मेरी त्रुटि है:

test.java:5: error: illegal start of expression 
public int[] locations={1,2,3}; 
^ 
1 error 

और मेरे कोड है:

public class test{ 

     public static void main(String[] args){ 

      test dot=new test(); 
      public int[] locations={1,2,3}; 

      dot.setLocationCells(locations); 

      String userGuess="2"; 
      String result = dot.checkYourself(userGuess); 
      String testResult="failed"; 

      if(result.equals("hit")){ 
       testResult="passed"; 
      } 


      System.out.println(testResult); 
     } 

public String checkYourself(String stringGuess){ 
     int guess=Integer.parseInt(stringGuess); 
     String result="miss"; 
     int numOfHits=0; 

     for(int cell:locations){ 
      if(guess==cell){ 
       result="hit"; 
       numOfHits++; 
       break; 
       } 
      } 

     if(numOfHits==locations.length){ 
      result="kill"; 
      } 

     System.out.println(result); 
     return result; 
    } 


public void setLocationCells(int[] locations){ 
     int[] locns; 
     locns=locations; 
     } 

} 
+0

कभी-कभी एनोटेशन के साथ समस्या होती है उदा। @Import {xxx, xxx,} एक अतिरिक्त है, बंद होने वाले ब्रैकेट –

उत्तर

14

तरीके केवल स्थानीय चर घोषित कर सकते हैं। जब आप इसे सार्वजनिक रूप से घोषित करने का प्रयास करते हैं तो संकलक एक त्रुटि की रिपोर्ट करता है।

स्थानीय चर के मामले में आप किसी भी प्रकार के एक्सेसर (सार्वजनिक, संरक्षित या निजी) का उपयोग नहीं कर सकते हैं।

आपको यह भी ध्यान रखना चाहिए कि किस स्थिर कुंजी शब्द का अर्थ है। विधि checkYourself में, आप locations की घोषणा का उपयोग करते हैं।

स्थैतिक कुंजी शब्द ऑब्जेक्ट सृजन के साथ सुलभ तत्वों को अलग करता है। वहां वस्तु का हिस्सा नहीं है।

public class Test { //Capitalized name for classes are used in Java 
    private final ini[] locations; //key final mean that, is must be assigned before object is constructed and can not be changed later. 

    public Test(int[] locations) { 
     this.locations = locations;//To access to class member, when method argument has the same name use `this` key word. 
    } 

    public boolean ckeckYourSelf(int value) { //This method is accessed only from a object. 
     for(int location : locations) { 
     if(location == value) { 
      return true; //When you use key word return insied of loop you exit from it. In this case you exit also from whole method. 
     } 
     } 
     return false; //Method should be simple and perform one task. So you can ge more flexibility. 
    } 
    public static int[] locations = {1,2,3};//This is static array that is not part of object, but can be used in it. 

    public static void main(String[] args) { //This is declaration of public method that is not part of create object. It can be accessed from every place. 
     Test test = new Test(Test.locations); //We declare variable test, and create new instance (obect) of class Test. 
     String result; 
     if(test.checkYourSelf(2)) {//We moved outsie the string 
     result = "Hurray";   
     } else { 
     result = "Try agian" 
     } 
     System.out.println(result); //We have only one place where write is done. Easy to change in future. 
    } 
} 
+0

मेरा कंपाइलर एक त्रुटि फेंक नहीं रहा है (मैंने बस सार्वजनिक स्थिर int [] स्थानों को मुख्य के बाहर घोषित किया है)। क्या आप सुनिश्चित हैं कि विधियां केवल स्थानीय चर घोषित कर सकती हैं? – AnujaPL

+0

हां। जब आप मुख्य के बाहर घोषणा को स्थानांतरित करते हैं तो अब विधि के दायरे में नहीं बल्कि कक्षा के दायरे में है। और फिर आप इसे सार्वजनिक घोषित कर सकते हैं। –

+0

@AnujaPL, मैंने उस कक्षा को दोबारा लिखा है जिसे आपने कुछ स्पष्टीकरणों के साथ डिजाइन किया है जो भविष्य में सहायक हो सकते हैं। –

1

मुख्य विधि के public static int[] locations={1,2,3}; बाहर घोषित।

+0

से पहले काम किया। क्या आप मुझे बता सकते हैं कि मुझे ऐसा करने की ज़रूरत क्यों है? क्या सार्वजनिक कीवर्ड स्वयं यह सुनिश्चित नहीं करेगा कि मेरे सभी वर्ग और तरीके मुख्य रूप से अंदर और बाहर हो सकते हैं? – AnujaPL

+1

आप किसी विधि के भीतर सार्वजनिक का उपयोग नहीं कर सकते हैं। यदि आप अन्य विधियों/वर्गों को 'स्थानों 'तक पहुंचने में सक्षम होना चाहते हैं, तो इसे आपकी कक्षा में सार्वजनिक घोषित करने की आवश्यकता है। – user1071777

+0

आपको डेटा प्रवाह अवधारणाओं के लिए सार्वजनिक स्थैतिक आवंटन का उपयोग नहीं करना चाहिए। यह मामला हल करता है लेकिन एक डिजाइन समस्या पैदा करने की तरह है। –

5

int[] locations={1,2,3}; से कीवर्ड हटाएं। एक विधि के अंदर एक एक्सेस संशोधक की अनुमति नहीं है, क्योंकि इसकी पहुंच क्षमता को इसके विधि के दायरे से परिभाषित किया गया है।

यदि आपका लक्ष्य इस विधि का उपयोग कई तरीकों से करना है, तो आप विधि के बाहर घोषणा को स्थानांतरित करना चाहेंगे।

2
public static int [] locations={1,2,3}; 

public static test dot=new test(); 

मुख्य विधि ऊपर ऊपर चर घोषित करें और कोड ठीक संकलित करता है।

public static void main(String[] args){ 
+0

वह काम किया .. धन्यवाद :) – AnujaPL

संबंधित मुद्दे