简体   繁体   English

数独解决方案在android中检查

[英]sudoku solution check in android

I'm very new to Android and Java development. 我对Android和Java开发非常陌生。 I'm trying to create a very basic 4x4 sudoku app. 我正在尝试创建一个非常基本的4x4数独应用。 However, the UI crashes when I run the code. 但是,运行代码时UI崩溃。 I'm not sure which part of the code is incorrect. 我不确定代码的哪一部分不正确。

    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.level1);
    findviewbyidfunc();

    checksol.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) 
        {
        setmatrix();    
        ans=check(mat);
        dispsol();      
        }
    });
}

public void findviewbyidfunc()
{
            checksol=(Button)findViewById(R.id.checksol1);
    r11=(EditText)findViewById(R.id.r1c1);
    r21=(EditText)findViewById(R.id.r2c1);
    r31=(EditText)findViewById(R.id.r3c1);
    r41=(EditText)findViewById(R.id.r4c1);

    r12=(EditText)findViewById(R.id.r1c2);
    r22=(EditText)findViewById(R.id.r2c2);
    r32=(EditText)findViewById(R.id.r3c2);
    r42=(EditText)findViewById(R.id.r4c2);

    r13=(EditText)findViewById(R.id.r1c3);
    r23=(EditText)findViewById(R.id.r2c3);
    r33=(EditText)findViewById(R.id.r3c3);
    r43=(EditText)findViewById(R.id.r4c3);

    r14=(EditText)findViewById(R.id.r1c4);
    r24=(EditText)findViewById(R.id.r2c4);
    r34=(EditText)findViewById(R.id.r3c4);
    r44=(EditText)findViewById(R.id.r4c4);

    displayanswer=(EditText)findViewById(R.id.answer);
}

Code to set the matrix. 设置矩阵的代码。

public void setmatrix()
{
    //Column one
    mat[1][1]=Integer.parseInt(r11.getText().toString());
    mat[2][1]=Integer.parseInt(r21.getText().toString());
    mat[3][1]=Integer.parseInt(r31.getText().toString());
    mat[4][1]=Integer.parseInt(r41.getText().toString());
    //Column two
    mat[1][2]=Integer.parseInt(r12.getText().toString());
    mat[2][2]=Integer.parseInt(r22.getText().toString());
    mat[3][2]=Integer.parseInt(r32.getText().toString());
    mat[4][2]=Integer.parseInt(r42.getText().toString());
    //Column three
    mat[1][3]=Integer.parseInt(r13.getText().toString());
    mat[2][3]=Integer.parseInt(r23.getText().toString());
    mat[3][3]=Integer.parseInt(r33.getText().toString());
    mat[4][3]=Integer.parseInt(r43.getText().toString());
    //Column four
    mat[1][4]=Integer.parseInt(r14.getText().toString());
    mat[2][4]=Integer.parseInt(r24.getText().toString());
    mat[3][4]=Integer.parseInt(r34.getText().toString());
    mat[4][4]=Integer.parseInt(r44.getText().toString());

}

Code to validate the matrix. 验证矩阵的代码。

public boolean check(Integer arr[][])
{
    Integer[] count={0,0,0,0,0};
    Integer[] count1={0,0,0,0,0};
    Boolean b=true;
    for(int i=1;i<4;i++)
    {
        for(int j=1;j<4;j++)
        {
            if(count[arr[j][i]]>i)
            {
                b=false;
                return b;
            }
            if(count1[arr[i][j]]>i)
            {
                b=false;
                return b;
            }
            count1[arr[i][j]]++;
            count[arr[j][i]]++;     
        }
    }
    return b;
}

If I remove the listener code and its associated function, the interface works fine. 如果我删除了侦听器代码及其相关功能,则该接口可以正常工作。 Not sure what's wrong. 不知道怎么了。

One or more of your EditText widgets are being declared as TextView in level1.xml . 您的一个或多个EditText小部件在level1.xml中被声明为TextView Probably displayanswer . 可能显示答案

in my opinion use "try-catch" in "setmatrix" method, for example: 我认为在“ setmatrix”方法中使用“ try-catch”,例如:

try
{
mat[1][1]=Integer.parseInt(r11.getText().toString());
}
catch(Exception ex)
{
mat[1][1]=0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM