android中使用java的乘法表

时间:2016-04-15 08:32:07

标签: java android multiplication

在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) {

            }
        }
    });
}

1 个答案:

答案 0 :(得分:1)

更改此部分:

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循环中