我的if/else语句始终在执行else(android) [英] My if/else statement is always executing else (android)

查看:87
本文介绍了我的if/else语句始终在执行else(android)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个可以帮助我解决问题的应用程序(很难解释它是什么),但它基本上是从EditText中获取一个4字母的String并在TextView中输出另一个String.但是,即使if语句为true,if/else语句也始终执行else.我将我的代码放在下面.

I am making an app to help me with something (It's quite hard to explain what it is) but it basically takes a 4 letter String from an EditText and outputs another String in the TextView. However the if/else statement is always executing the else even if the if statement is true. I will put my code below.

public class egkk extends ActionBarActivity {
        EditText editText;
        TextView textView;
        Button button;
        String aerodrome;
        String firstTwo;
        String firstThree;
        String first;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_egkk);
        button = (Button)findViewById(R.id.gen);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                editText = (EditText)findViewById(R.id.egkk_edit);
                textView = (TextView)findViewById(R.id.s_view);

                aerodrome = editText.getText().toString().toLowerCase();
                firstTwo = aerodrome.substring(0, 1);
                firstThree = aerodrome.substring(0, 2);
                first = aerodrome.substring(0, 0);

                if (aerodrome.length() == 4){
                    if (aerodrome.equals("egkk")){
                        textView.setText("3750 - 3763: Gatwick APC general usage" + "3764 - 3767: Gatwick ADC circuit traffic and transits");
                    }
                    if (firstThree.equals("egj")){
                        textView.setText("Squawk is 7760 – 7775");
                    }
                    if (firstTwo.equals("eg")){
                        textView.setText("Sqauwk is 4301 - 4377");
                    }
                    if (firstTwo.equals("ei")){
                        textView.setText("Squawk is 4430 – 4477");
                    }
                    if (firstTwo.equals("lf")){
                        textView.setText("Squawk is 2201 – 2277");
                    }
                    if (firstTwo.equals("le")){
                        textView.setText("Squawk is 2201 – 2277");
                    }
                    if (firstTwo.equals("lp")){
                        textView.setText("Squawk is 2201 – 2277");
                    }
                    if (firstTwo.equals("fa")){
                        textView.setText("Squawk is 2201 – 2277");
                    }
                    if (firstTwo.equals("eh")){
                        textView.setText("Squawk is 7310 – 7677");
                    }
                    if (firstTwo.equals("eh")){
                        textView.setText("Squawk is 7310 – 7677");
                    }
                    if (first.equals("k")){
                        textView.setText("Squawk is 7610 – 7677");
                    }
                    if (first.equals("c")){
                        textView.setText("Squawk is 7610 – 7677");
                    }
                    if (firstTwo.equals("mu")){
                        textView.setText("Squawk is 7610 – 7677");
                    }
                    if (firstTwo.equals("my")){
                        textView.setText("Squawk is 7610 – 7677");
                    }
                    if (firstTwo.equals("mk")){
                        textView.setText("Squawk is 7610 – 7677");
                    }
                    if (firstTwo.equals("mt")){
                        textView.setText("Squawk is 7610 – 7677");
                    }
                    if (firstTwo.equals("md")){
                        textView.setText("Squawk is 7610 – 7677");
                    }
                    if (firstTwo.equals("tj")){
                        textView.setText("Squawk is 7610 – 7677");
                    }
                    if (firstTwo.equals("mu")){
                        textView.setText("Squawk is 7610 – 7677");
                    }
                    else {
                        textView.setText("Squawk is 1140 – 1177");
                    }
                }
                else {
                    textView.setText("Error");
                }
        }
    });



    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_egkk, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

`

推荐答案

这是因为您的情况:

if (aerodrome.length() == 4){

从不匹配.这意味着,在这里,当您获取文本时,永远不会是长度为4 length 的字符串.

Never matches. This means, here, when you get the text, is never a String of 4 length.

aerodrome = editText.getText().toString().toLowerCase();

这篇关于我的if/else语句始终在执行else(android)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆