Java 韩元';t从布尔表达式中调用变量

Java 韩元';t从布尔表达式中调用变量,java,boolean,Java,Boolean,我很难找到java在调用变量时遇到的问题。我正在创建一个简单的聊天机器人,这就是我目前所拥有的: 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

我很难找到java在调用变量时遇到的问题。我正在创建一个简单的聊天机器人,这就是我目前所拥有的:

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")){

        }

    }
}
公共类聊天机器人{
公共静态void main(字符串[]args){
String name=JOptionPane.showInputDialog(“嗨!你好吗?我叫Chatbot!你的名字是什么?”);

if(name.compareTo(“a”)当前
city
的作用域仅限于if或else块。通过在方法级别声明使其成为局部变量来增加其作用域

公共静态void main(字符串[]args){

String name=JOptionPane.showInputDialog(“嗨!你好吗?我的名字是Chatbot!你的名字是什么?”);
字符串城市=”;

if(name.compareTo(“a”)顶部的贴花
字符串city=null
。然后使用它。它必须在
if-else
块之外

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);  
        }
String city=null;
String name=JOptionPane.showInputDialog(“嗨!你好吗?我叫Chatbot!你的名字是什么?”);

if(name.compareTo(“a”)如前所述,您需要在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")){

    }

}
publicstaticvoidmain(字符串[]args){
String name=JOptionPane.showInputDialog(“嗨!你好吗?我叫Chatbot!你的名字是什么?”);
字符串city=null;

如果(name.compareTo(“a”)尝试以下操作,希望对您有所帮助

 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" ) )
            {

            }

        }
publicstaticvoidmain(字符串[]args)
{
String name=JOptionPane.showInputDialog(“嗨!你好吗?我叫Chatbot!你的名字是什么?”);
字符串城市=”;
如果(名称比较(“a”)<0)
{
city=JOptionPane.showInputDialog(“很高兴认识你!你从哪里来,”+name);
}
其他的
{
city=JOptionPane.showInputDialog(“哈,这是个奇怪的名字,你从哪里来,”+name);
}
如果(!city.equals(“西雅图”))
{
}
}

在条件之前声明city。变量
city
仅对if-else块是局部的。向下滚动到“Loop Scope”上读,因为Java使用块作用域。它不需要是全局的,只是局部性比它们小一点(实际上有两个不同的变量称为city,一个在
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 = "";
            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" ) )
            {

            }

        }