2017-03-28 1 views
0

저는 Java에 처음 접했고 (Stackoverflow에 대한 첫 번째 질문도 마찬가지입니다) 지금은 정의 된 변수 값을 전달하는 방법에 대해 고심하고 있습니다. 방법에서 다른 것에.다른 방법으로 변수 값을 얻는 방법 - 진행 방법에 대해 혼란스러워합니다.

전역 변수, ArrayList, HashMap 등과 같은 항목에 대한 여러 검색을 수행했지만 검색중인 항목 ( Global variables in Java) 만 처리하는 방법에 대해 좀 더 혼란스럽게합니다.

I했습니다 그것을 위해 ArrayList를 사용하려고하지만, 그것이 작동하지 않은 - 여기

내 코드입니다 ... 그리고 나는 어쨌든 하루에 원하는 것을 위해 그것을 사용할 수 있는지 모르겠어요 : 은 "onPreCreationGuildeCommand"방법에서 무엇인가 내가하고 싶은

public static void creationGuilde(String[] args, Player player, String playerName) 
{ 
    String nomGuilde = args[2]; 
    String message1 = "Votre nouvelle guilde se nommera " + nomGuilde + "."; 
    TextComponent confirmer = new TextComponent("Cliquez ici pour confirmer"); 
    TextComponent annuler = new TextComponent("cliquez ici pour annuler"); 
    String message2 = confirmer + "OU" + annuler + "."; 
    player.sendMessage(message1); 
    player.sendMessage(message2); 
    confirmer.setClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/creationGuildeConfirmer")); 
    annuler.setClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/creationGuildeAnnuler")); 
} 

private void onPreCreationGuildeCommand(PlayerCommandPreprocessEvent event) 
{ 
    if (event.getMessage().equals("creationGuildeConfirmer")) 
    { 
     String guilde = CommandeGuilde.creationGuilde(nomGuilde); 
     event.getPlayer().sendMessage("Félicitations! Vous venez de créer la guilde " +guilde); // <-- Here, trying to get the value of 'guilde' in the 'creationGuilde' method... 
    } 
} 

, 나는 내 마지막 sendMessage 첨부에 넣어하려면 "creationGuilde"방법에서 'nomGuilde'값을 얻을 싶어요.

제 질문이 충분히 분명하기를 바랍니다. 이 일을 도와 주셔서 감사합니다.

+0

정상적으로 작동해야하는 것으로 보입니다. 문제를 편집하고이 결과물과 예상 한 결과를 구체적으로 기술하십시오. 또한'print' 문을 추가하여'guilde'의 값에 무언가가 있는지 확인하십시오. – nhouser9

+0

시도해보기 위해 프로젝트를 빌드 할 수 없습니다. IDE에서이'String guilde = CommandeGuilde.creationGuilde (nomGuilde);에 대한 오류를 제공하고 있습니다. 말하는 'nomruilde'심볼을 해결할 수 없다 " – Vellric

답변

0

가장 쉽고 가장 좋은 해결책은 nonGuilde를 전역 변수로 정의하는 것입니다.
코드는 다음과 같아야합니다

class YourClassName{ 
    //Must be null 
    //otherwise if you call onPreCreationGuildeCommand before creationGuilde 
    //you would get error because variable hasn't been initialized 
    private String nomGuilde = null; 

    public static void creationGuilde(String[] args, Player player, String playerName){ 
     //set the value 
     //value will persist outside the method because variable is global 
     nomGuilde = args[2]; 
    } 

    private void onPreCreationGuildeCommand(PlayerCommandPreprocessEvent event){ 
     // Here you can do anything with variable, for example: 
     System.out.println(nomGuilde); 
    } 
} 

또한 당신에게 내 조언은 변수에 대한 비트 및 그 범위 (특정 변수가 표시되는 코드 블록)과 그 수명을 읽는 것입니다 (변수가 얼마나 오래 존재 메모리)

0

변수 nonGuilde를 메소드 전역 변수로 정의 할 수 있습니다. 전역 변수를 정의 할 때이 클래스의 모든 메소드에서이 varibale을 "쓰거나"가져올 수 있습니다. 두 번째 해결책은 createGuilde 메소드에서 nonGuilde를 반환하는 것입니다.

+0

정확하게 이해한다면, 내 두 메소드의 부모 클래스에서'nomGuilde' 변수를 정의하고 그것을'creationGuilde'one에 쓰면 얻을 수 있습니다. 다른 방법에서? – Vellric

+0

네,하지만 어쩌면이 변수는 정적이어야합니다. 변수를 사용하기 위해 객체를 만들고 싶지 않습니다. 클래스의 본문에서 정적으로 정의하고 모든 메소드에서 사용할 수 있습니다 :) –

+0

제가 도움을 주 었는지 알려주십시오. 너! –