2013-04-02 3 views
-1

변수를 호출 할 때 Java에서 문제가되는 부분을 찾는 데 어려움이 있습니다. 나는 간단한 chatbot을 만드는거야 이것은 내가 지금까지 무엇을 가지고 :부울 표현식에서 변수를 회수하지 않습니다.

public class Chatbot { 
    public static void main(String[] args) { 
     String name = JOptionPane.showInputDialog("Hi! How are you? My name is Chatbot! What is yours? "); 
     if (name.compareTo("a")<0){ 
      String city = JOptionPane.showInputDialog("Nice to meet you! Where are you from, "+name); 
     } 
     else 
     { 
      String city = JOptionPane.showInputDialog("Huh. That's a strange name. Where are you from,"+name); 
     } 


     if (!city.equals("Seattle")){ 

     } 

    } 
} 

내 문제는 자바의 경우 다른 문에서 변수 도시를 인식하고 그래서 도시가 해결되지 말한다 없다는 것입니다. 부울 식 안에서 객체를 인식하도록 Java를 얻으려면 어떻게해야합니까? 내가 도대체 ​​뭘 잘못하고있는 겁니까?

+0

상태를 선언하기 전의 상태. – JSS

+0

변수'city'는 if-else 블록에만 국한되어 있습니다. –

+1

http://www.java-made-easy.com/variable-scope.html "Loop Scope"로 스크롤 –

답변

3

현재 city의 범위는 if 또는 else 블록에만 국한됩니다. 메서드 수준에서 선언하여 블록을 로컬 변수로 변경하여 범위를 확장하십시오.

공공 정적 무효 메인 (문자열 []에 args) {

String name = JOptionPane.showInputDialog("Hi! How are you? My name is Chatbot! What is yours? "); 
String city=""; 
if (name.compareTo("a")<0){ 
    city = JOptionPane.showInputDialog("Nice to meet you! Where are you from, "+name); 
} 
else 
    { 
    city = JOptionPane.showInputDialog("Huh. That's a strange name. Where are you from,"+name); 
    } 
2

위에 Decalre String city = null. 사용하십시오. 그것은 if else 블록 밖으로 있어야합니다.

public static void main(String[] args) { 
    String name = JOptionPane.showInputDialog("Hi! How are you? My name is Chatbot! What is yours?"); 
    String city = null; 
    if (name.compareTo("a")<0){ 
     city = JOptionPane.showInputDialog("Nice to meet you! Where are you from, "+name); 
    } 
     else 
    { 
     city = JOptionPane.showInputDialog("Huh. That's a strange name. Where are you from,"+name); 
    } 
    if (!city.equals("Seattle")){ 

    } 

} 
+0

전역적일 필요는 없습니다. 지역보다 조금 덜 로컬합니다 (실제로는 city라는 두 개의 다른 변수가 있고, 하나는'if'에 있고 다른 하나는'else'에 있습니다). –

+1

@ KlasLindbäck은 방금 깨닫고 편집했습니다. –

0

으로 이미이처럼 경우 - 다른 블록 이외의 도시를 선언해야했다.

public static void main(String[] args) 
     { 

      String name = JOptionPane.showInputDialog("Hi! How are you? My name is Chatbot! What is yours? "); 
      String city = ""; 
      if (name.compareTo("a") < 0) 
      { 

       city = JOptionPane.showInputDialog("Nice to meet you! Where are you from, " + name); 
      } 
      else 
      { 
       city = JOptionPane.showInputDialog("Huh. That's a strange name. Where are you from," + name); 
      } 

      if (!city.equals("Seattle")) 
      { 

      } 

     } 
0

시도는 도움이되기를 바랍니다 다음

String city=null; 
    String name = JOptionPane.showInputDialog("Hi! How are you? My name is Chatbot! What is yours? "); 
if (name.compareTo("a")<0){ 
      city = JOptionPane.showInputDialog("Nice to meet you! Where are you from, "+name); 
     } 
     else 
     { 
      city = JOptionPane.showInputDialog("Huh. That's a strange name. Where are you from,"+name); 
     } 
관련 문제