在android中实现一个简单的乘法表。此代码无助于显示结果。我要去哪里错了?

@Override
protected void onCreate(Bundle savedInstanceState) {
    final int a;
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    // final TextView finalet = null;
    final TextView finale = (TextView) findViewById(R.id.finale);
    final EditText et = (EditText) findViewById(R.id.et);
    final String fromEt = et.getText().toString();

    Button gen = (Button) findViewById(R.id.gen);
    gen.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            try {

                int a = Integer.parseInt(fromEt);
                for (int voo = 0; voo <= 11; voo++) {
                    Integer sam = a * voo;
                    // finalet.setText(a*v);
                    // finale.setText(String.valueOf(sam));
                    finale.setText(sam);
                }
            } catch (NumberFormatException e) {

            }
        }
    });
}

最佳答案

更改此部分:

int a = Integer.parseInt(fromEt);




int a = Integer.parseInt(et.getText().toString());


像这样:

public void onClick(View v) {
            try {

                int a = Integer.parseInt(et.getText().toString());
                for (int voo = 0; voo <= 11; voo++) {
                    Integer sam = a * voo;
                    finale.setText(String.valueOf(sam));
                }
            } catch (NumberFormatException e) {

            }
        }


但是根据您的代码,您只会看到最后一个the number you entered * 11

如果要显示所有内容,则应执行以下操作:

finale.append(String.valueOf(sam) + " ");


在for循环内

09-26 04:09