我怎么能运行一定的条件,直到它变得虚假?

时间:2018-02-17 18:47:44

标签: java android

这是登录程序,其中用户输入用户名和密码并被导向另一个活动,其中toast“登录成功”,但即使用户输入正确的用户名和密码,循环运行吐司4次(数组长度),有一次它显示“登录成功”,有时它显示“登录失败”

我希望这个程序只显示“登录成功”一次。

如何运行if部分直到它变为false,然后运行else部分?

String names[] = {"Pa" , "An", "Du", "De"};
String pass[] = {"P", "A", "D", "S"};
String gender[] = {"Female" , "Female" , "Female" , "Male"};


Button button;
EditText n,p;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_account);

    button= (Button)findViewById(R.id.login);
    n = (EditText)findViewById(R.id.name);

    p = (EditText)findViewById(R.id.pass);

    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            String nn = n.getText().toString();
            String pp = p.getText().toString();


            int index = 0;
                for (int i = 0; i < names.length; i++) {

                    if (nn.equals(names[i])) {
                            index = i;

                            if (pp.equals(pass[index])) {


                                Context context = getApplicationContext();
                                CharSequence text = "Login Successful!";
                                int duration = Toast.LENGTH_SHORT;

                                Toast toast = Toast.makeText(context, text, duration);
                                toast.show();


                                Intent intent = new Intent(Account.this, MainPage.class);
                                startActivity(intent);

                            }
                            if (!(pp.equals(pass[index]))) {

                                Context context = getApplicationContext();
                                CharSequence text = "Incorrect Password";

                                int duration = Toast.LENGTH_SHORT;

                                Toast toast = Toast.makeText(context, text, duration);
                                toast.show();
                            }
                        }


                else {
                        Context context = getApplicationContext();
                        CharSequence text = "Incorrect Username!";
                        int duration = Toast.LENGTH_SHORT;

                        Toast toast = Toast.makeText(context, text, duration);
                        toast.show();
                    }

                    }

                }
        });
    }
}

3 个答案:

答案 0 :(得分:0)

只需在break成功

后添加for loop关键字即可
if (pp.equals(pass[index])) {
     Context context = getApplicationContext();
     CharSequence text = "Login Successful!";
     int duration = Toast.LENGTH_SHORT;
     Toast toast = Toast.makeText(context, text, duration);
     toast.show();
     Intent intent = new Intent(Account.this, MainPage.class);
     startActivity(intent);
     break;
  }

答案 1 :(得分:0)

我在用户名匹配时添加了一个中断,并将其他部分移出循环

因此,如果names数组中的所有用户都已用完,那么它将打印Incorrect Username!

  String names[] = {"Pa", "An", "Du", "De"};
  String pass[] = {"P", "A", "D", "S"};
  String gender[] = {"Female", "Female", "Female", "Male"};


  Button button;
  EditText n, p;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_account);

    button = (Button) findViewById(R.id.login);
    n = (EditText) findViewById(R.id.name);

    p = (EditText) findViewById(R.id.pass);

    button.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View view) {
        String nn = n.getText().toString();
        String pp = p.getText().toString();

        int index = 0;
        int i;
        for (i = 0; i < names.length; i++) {

          if (nn.equals(names[i])) {
            index = i;

            if (pp.equals(pass[index])) {

              Context context = getApplicationContext();
              CharSequence text = "Login Successful!";
              int duration = Toast.LENGTH_SHORT;

              Toast toast = Toast.makeText(context, text, duration);
              toast.show();

              Intent intent = new Intent(Account.this, MainPage.class);
              startActivity(intent);

            }
            if (!(pp.equals(pass[index]))) {

              Context context = getApplicationContext();
              CharSequence text = "Incorrect Password";

              int duration = Toast.LENGTH_SHORT;

              Toast toast = Toast.makeText(context, text, duration);
              toast.show();
            }
            break; //add break here
          }

        }
        if (i == names.length) {
          Context context = getApplicationContext();
          CharSequence text = "Incorrect Username!";
          int duration = Toast.LENGTH_SHORT;

          Toast toast = Toast.makeText(context, text, duration);
          toast.show();
        }

      }
    });
  }

答案 2 :(得分:0)

你可以这样做:

Boolean userNameFound = false;
int userNameIndex = -1;
for (int i = 0; i < names.length; i++) {
    String expectedName = names[i];
    if (nn.equals(expectedName)) {
        userNameFound = true;
        userNameIndex = i;
        break;
    }
}

String message = null;
if (userNameFound) {
    String expectedPassowrd = pass[userNameIndex];
    if (pp.equals(expectedPassowrd)) {
        message = "Login Successful!";
    } else {
        message = "Incorrect Password!";
    }
} else {
    nessage = "Incorrect Username!"
}

Toast toast = Toast.makeText(context, message, duration);
toast.show();