【问题标题】:How to compare two int variables in a if cycle如何在if循环中比较两个int变量
【发布时间】:2014-05-09 13:10:35
【问题描述】:

我的代码有点问题。我想在 if 循环中compare 2 int variables。我知道logical operator == and method .equals();之间有一些区别

我的代码是:

问题出在的部分代码:

问题是result02==var。我没有收到来自 Eclipse logcat 的任何错误。

我尝试使用result02.equals(var),但没有运行。

else{
         if (conferma_text.getText().toString().equals(password_text.getText().toString())){
            SQLiteDatabase db = dbLogin.getWritableDatabase();
            int result02 = verifyL.verify(db, login_text.getText().toString(), password_text.getText().toString());
            int var =-1;
            if(result02==var){

检查我的完整代码:

package com.example.prenotazione_esame;

import android.app.Activity;
import android.app.AlertDialog;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.view.View;
import android.widget.Toast;
import android.database.sqlite.SQLiteDatabase;
import android.content.ContentValues;
import android.content.DialogInterface;

public class CreateAccountActivity extends Activity {

    private LoginDataBase dbLogin;
    VerifyLogin verifyL = new VerifyLogin();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        dbLogin = new LoginDataBase(this);
        setContentView(R.layout.createaccount);
        Button button_crea02 = (Button) findViewById(R.id.Create02);
        button_crea02.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                executeCreate();
            }
        });
    }

    //Per prima cosa si verifica che tutte le EdiText siano compilate, in caso negativo
    //una notifica toast avvisa l'utente dell'errore.
    private void executeCreate(){
       EditText login_text = (EditText) findViewById(R.id.UserName02);
       EditText password_text = (EditText) findViewById(R.id.Password02);
       EditText conferma_text = (EditText) findViewById(R.id.Conferma);
       EmptyField emptyf = new EmptyField();
       String result=emptyf.CreateEmpty(login_text, password_text, conferma_text);
       if (result.equals("Empty")){
            Toast toast = Toast.makeText(this,"Fornire i dati richiesti", Toast.LENGTH_SHORT);
            toast.show();
       }
       //Se le EditText sono compilate si verifica che le credenziali di accesso non
       //siano già associate ad un altro utente, in tal caso si avvisa l'utente con una
       //notifica toast. Se le credenziali sono corrette si procede al loro inserimento
       //nel DB.
       else{
         if (conferma_text.getText().toString().equals(password_text.getText().toString())){
            SQLiteDatabase db = dbLogin.getWritableDatabase();
            int result02 = verifyL.verify(db, login_text.getText().toString(), password_text.getText().toString());
            int var =-1;
            if(result02==var){

                 ContentValues values = new ContentValues();
                 values.put("USERNAME",login_text.getText().toString());
                 values.put("PASSWORD",password_text.getText().toString());      
                 long id = db.insert("T_LOGIN", null, values);
                 if (id != -1){
                     Toast toast = Toast.makeText(this,"Record creato", Toast.LENGTH_SHORT);
                     toast.show();
                 }
             }
            else{
                //Se username e password esistono già viene lanciata un AlertDialog. Premendo Ok
                //si puliscono le EditText
                AlertDialog.Builder builder = new AlertDialog.Builder(this);
                builder.setTitle("Attenzione");
                builder.setMessage("Username e Password invalidi");
                builder.setCancelable(false);
                builder.setPositiveButton("Ok", new DialogInterface.OnClickListener(){
                    @Override
                    public void onClick(DialogInterface dialog, int which){
                        EditText login_text = (EditText) findViewById(R.id.UserName02);
                        EditText password_text = (EditText) findViewById(R.id.Password02);
                        EditText conferma_text = (EditText) findViewById(R.id.Conferma);
                        login_text.setText("");
                        password_text.setText("");
                        conferma_text.setText("");
                        dialog.cancel();                        
                    }
                });
                AlertDialog alert = builder.create();
            }
         }
         //Se la password inserita e quella di conferma non corrispondono si avvisa l'utente
         else{
            Toast toast = Toast.makeText(this,"Le password non corrispondono", Toast.LENGTH_SHORT);
            toast.show();
         }
       }
    }
}

【问题讨论】:

  • equals() 用于对象,== 用于原始数据类型(int、float、char ...)。
  • 那么我的脚本哪里出了问题?我的 if 循环不运行。谢谢。
  • 你有什么错误吗?
  • 我猜这正是你所期望的,但它包含的价值是什么?您可能会获得另一个价值。
  • 这个问题似乎离题了,因为 OP 对基本调试过程缺乏最基本的了解。请参考帮助部分和ericlippert.com/2014/03/05/how-to-debug-small-programs

标签: java android if-statement int equals


【解决方案1】:

对于 int、boolean、long 等原语,您应该使用 ==

基元不是对象,因此您不能对它们调用方法。

【讨论】:

    【解决方案2】:

    首先检查它是否真的进入了里面

     if (conferma_text.getText().toString().equals(password_text.getText().toString())){
    
    //Use log here to check 
    
    }
    

    【讨论】:

    • 什么??我不明白。
    • 可能是如果 (conferma_text.getText().toString().equals(password_text.getText().toString())) 返回 false 并且您没有进入这种情况
    • 哦,好的。我尝试了这种情况,它运行良好。问题是第二个如果。
    【解决方案3】:

    基元不使用 .equals()。在他们身上使用你的 ==。如果您希望使用 .equals() 方法,则必须将它们放入包装类(即 Integer)中。然后你可以使用 .equals()。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-11
      • 1970-01-01
      • 2022-10-20
      • 1970-01-01
      相关资源
      最近更新 更多